-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
略读 TypeScript Deep Dive #14
Comments
工具泛型TypesScript 中内置了很多工具泛型,它们在 TypeScript 内置的 lib.es5.d.ts 中都有定义,所以不需要任何依赖都是可以直接使用的。 /**
* Make all properties in T optional
*/
type Partial<T> = {
[P in keyof T]?: T[P];
};
/**
* Make all properties in T required
*/
type Required<T> = {
[P in keyof T]-?: T[P];
}; 与 /**
* Make all properties in T readonly
*/
type Readonly<T> = {
readonly [P in keyof T]: T[P];
}; /**
* Construct a type with a set of properties K of type T
*/
type Record<K extends keyof any, T> = {
[P in K]: T;
};
/**
* Exclude from T those types that are assignable to U
*/
type Exclude<T, U> = T extends U ? never : T;
/**
* Extract from T those types that are assignable to U
*/
type Extract<T, U> = T extends U ? T : never;
interface Worker {
name: string;
age: number;
email: string;
salary: number;
}
interface Student {
name: string;
age: number;
email: string;
grade: number;
}
type CommonKeys = Extract<keyof Worker, keyof Student>; // 'name' | 'age' | 'email'
type SpecialKeys = Exclude<keyof Worker, keyof Student>; // 'salary' /**
* From T, pick a set of properties whose keys are in the union K
*/
type Pick<T, K extends keyof T> = {
[P in K]: T[P];
};
/**
* Construct a type with the properties of T except for those in type K.
*/
type Omit<T, K extends keyof any> = Pick<T, Exclude<keyof T, K>>;
/**
* Exclude null and undefined from T
*/
type NonNullable<T> = T extends null | undefined ? never : T; /**
* Obtain the parameters of a function type in a tuple
*/
type Parameters<T extends (...args: any) => any> = T extends (
...args: infer P
) => any
? P
: never;
/**
* Obtain the parameters of a constructor function type in a tuple
*/
type ConstructorParameters<
T extends new (...args: any) => any
> = T extends new (...args: infer P) => any ? P : never; 用于获取函数/构造函数参数的类型。 /**
* Obtain the return type of a function type
*/
type ReturnType<T extends (...args: any) => any> = T extends (
...args: any
) => infer R
? R
: any;
/**
* Obtain the return type of a constructor function type
*/
type InstanceType<T extends new (...args: any) => any> = T extends new (
...args: any
) => infer R
? R
: any; 用于获取函数/构造函数返回值的类型。 |
TypeScript 是 JavaScript 的类型的超集,本质上是在
JavaScript
上增加一套静态类型系统(编译时进行类型分析)。使用总结
变量申明
基本类型
类型补充
Enum
使用枚举类型可以为一组数值赋予友好的名字
Tuple
允许数组各元素的类型不必相同。 比如,你可以定义一对值分别为 string 和 number 类型的元组
Any
表示任意类型,通常用于不确定内容的类型,比如来自用户输入或第三方代码库
Void
与
any
相反,通常用于函数,表示没有返回值interface
类型契约,跟我们平常调服务端接口要先定义字段一个理
如下例子 point 跟 Point 类型必须一致,多一个少一个也是不被允许的
下面两个是等效的声明, 示例 A 使用内联注解,示例 B 使用接口形式:
函数
函数重载
函数重载(Function Overloading), 允许创建数项名称相同但输入输出类型或个数不同的子程序。
泛型
泛型的意义在于函数的重用性,设计原则希望组件不仅能够支持当前的数据类型,同时也能支持未来的数据类型。
设计泛型的关键目的是在成员之间提供有意义的约束,这些成员可以是:
交叉类型
交叉类型(Intersection Types),将多个类型合并为一个类型
联合类型
联合类型(Union Types),表示一个值可以是几种类型之一。
@types
DefinitelyTyped https://github.com/borisyankov/DefinitelyTyped
vue-next 就使用了 Typescript,后续会继续学习它以加深对 Typescript 的了解。
The text was updated successfully, but these errors were encountered: