TypeScript
TypeScript 工具类型
属性修饰工具类型 Copy
type Partial<T> = {
[P in keyof T]?: T[P];
};
type DeepPartial<T extends object> = {
[K in keyof T]?: T[K] extends…
TypeScript 逻辑运算
条件类型 Copy
type LiteralType<T> = T extends string ? "string" : "other";
type Res1 = LiteralType<"linbudu">; // "string"
type Res2…
TypeScript 类型系统
结构化类型系统 Copy
class Cat {
meow() { }
eat() { } // 去掉 eat 不会报错
}
class Dog {
eat() { }
}
function feedCat(cat: Cat) { }
// 报错!
feedCat…
TypeScript 泛型
类型别名中的泛型 Copy
type Partial<T> = {
[P in keyof T]?: T[P];
};
interface IFoo {
prop1: string;
prop2: number;
prop3: boolean;
prop4…
TypeScript 类型编程
类型别名 抽离一组联合类型:
Copy
type StatusCode = 200 | 301 | 400 | 500 | 502;
type PossibleDataTypes = string | number | (() => unknown);
const status:…
TypeScript 类型基础
原始类型 Copy
const name: string = 'linbudu';
const age: number = 24;
const male: boolean = false;
const undef: undefined = undefined;
const nul:…