类型初见
个人感觉,类型操作,相对于变量操作更为复杂。 它不能debug调试的,没有错误提示,作为元编程的一种,理解起来比较费劲。 它的优势在于开发工具库vue3等,组件库等,白鹭引擎游戏开发等。在普通web开发中,则各有优劣。 TS已经实现了自举,可以参看github.com/microsoft/T…
泛型
泛型无非就是把类型作为变量进行推导。 使用的套路, 在类型、函数,类,接口都可以使用,<>是传参,一般有T、U等。 extends可以用来作为限制。
function identity<Type>(arg: Type): Type {
return arg;
}
let myIdentity: <Type>(arg: Type) => Type = identity;
Keyof
类型中取键操作, 可取接口,类等
interface Person {
name: string;
age: number;
location: string;
}
type K1 = keyof Person; // "name" | "age" | "location"
type K2 = keyof Person[]; // number | "length" | "push" | "concat" | ...
type K3 = keyof { [x: string]: Person }; // string | number
type Mapish = { [k: string]: boolean };
type M = keyof Mapish;
//取出string | number,因为比较默认为number
Typeof
变量空间到类型空间的转发。
Indexed
索引,依赖于对象,数组,取合集union。
Conditional
类型运算中的条件语句 。
SomeType extends OtherType ? TrueType : FalseType;