typescript语法大全
介绍
这是一篇typescript的语法说明书
安装
1 | npm install -g typescript |
数据类型
1 | // boolean: |
函数
1 | function buildName(firstName: string = 'cat', lastName?:string, ...items: any[]):string{ |
类型断言
1 | interface Cat { |
声明与导出
1 | // 声明全局变量 |
类型别名
1 | type Name = string |
字符串字面量类型
1 | type EventNames = 'click' | 'scroll' | 'mousemove'; |
元祖
1 | let tom: [string, number] = ['tom', 25] |
枚举
1 | // 默认第一个是0,依次递增加一 |
类
1 | // 抽象类的抽象方法,继承的类要具体实现 |
接口
1 | // 接口可以继承接口 |
泛型
1 | // 在函数内部使用泛型变量的时候,由于事先不知道它是哪种类型,所以不能随意的操作它的属性或方法 |
声明合并
1 | // 函数的合并 |
接口的合并
1 | interface Alarm { |
在react中使用ts的一些准备
- 声明
png
,modules.scss
,window
等全局对象1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18declare module '*.png' {
const src: string;
export default src;
}
declare module '*.svg' {
const content: string;
export default content;
}
declare module '*.module.scss' {
const classes: { readonly [key: string]: string };
export default classes;
}
interface Window {
location: Location;
} - 组件的编写
1
2
3
4
5
6
7
8
9
10
11
12
13// 函数式组件
const Home: React.FC<{name: string;}> = (props) => {return <div />}
// 如果你需要定义defaultProps,那么不要使用React.FC
const defaultProps = { name: 'woyao' };
type IProps = { age: number } & typeof defaultProps;
const Home = (props: IProps ) => { return <div /> }
// 类组件
interface IProps {};
interface IState {};
const Home extends React.Component<IProps, IState> {
state: IState = {};
render(){ return <div />; }
} - React相关的类型
1
2
3
4
5
6
7/* ReactNode */
/* 在React中,所有事件(包括FormEvent、KeyboardEvent、MouseEvent等)都是SyntheticEvent的子类型。 */
/* React.FormEvent */
/* React.MouseEvent */
/* React.KeyboardEvent */
/* React.BaseSyntheticEvent*/
/* React.SyntheticEvent */
一些高级类型
Readonly
: 只读类型1
2
3
4
5type Readonly<T> = {
readonly [P in keyof T]: T[P];
}
interface IProps = {name: string;};
const person: Readonly<IProps> = {name: 'woyao' };partial
: 将类型定义的所有属性都修改为可选1
2
3
4
5type Partial<T> = {
[P in keyof T]?: T[P];
}
interface PersonProps = { name: string; age: number; };
const woyao:Partial<PersonProps> = {};Required
: 将类型定义的所有属性都修改为必填1
2
3type Required<T> = { [P in keyof T]-?: T[P] };
interface PersonProps = { name?: string; age: number; };
const woyao:Required<PersonProps> = {name: 'woyao', age: 23};Pick
: 将类型定义的部分属性作为一个新类型1
2
3
4
5
6// k extends keyof T === 'props1' | 'props2' | ....
type Pick<T, K extends keyof T> = {
[P in K]: T[P];
}
interface PersonProps = { name: string; age: number; };
const woyao: Pick<PersonProps, "name"> = {name: 'woyao' };Record
: 以 typeof 格式快速创建一个类型,此类型包含一组指定的属性且都是必填。1
2
3
4
5// K 是一个union类型,比如: type PersonProps = 'name' | 'age' | 'sex'
type Record<K extends string, T> = {
[P in K]: T;
}
type PersonProps = Record<'name' | 'sex', string>Exclude
: 排除1
2
3type Exclude<T, U> = T extends U ? never : T;
// type A = 'a'
type A = Exclude<'x' | 'a', 'x' | 'y' | 'z'>Omit
: 省略1
2
3
4
5
6
7
8type Omit<T, K extends keyof any> = Pick<T, Exclude<keyof T, K>>;
interface User {
id: number;
age: number;
name: string;
};
// 相当于: type OmitUser = { age: number; name: string; }
type OmitUser = Omit<User, "id">