// number类型
const n: number = 1;
// string类型
const s: string = '';
// boolean类型
const b: boolean = true;
// array类型
const arr: string[] = [];
// tuple类型
const tuple: [boolean, string] = [true, 'true'];
// enum类型
enum Color {
red = 'red',
blue = 'blue ',
}
const c: Color = Color.red;
// any类型
// any 类型不会做做检查 也不会有提示
let a: any = 1;
a = 's';
// unknown类型
// unknown类型是会做检查的
let unKnownEle: unknown;
unKnownEle = "马松松"
unKnownEle = 12345;
unKnownEle.toFixed(); // 会报错 类型“unknown”上不存在属性“toFixed”。
// 函数
const func1: (name: string, old: number) => string = (name, old) => {
return `${name}:${old + 1}`;
};
const func2 = (name: string, old?: number) => {
if (old) {
return old;
} else {
return name;
}
};
// void不是类型 只做函数返回值 当函数没有返回值是使用
const func3: (name: string) => void = (name) => {
console.log(name);
};
// 当函数永远不返回时为never
const func4: (name: string) => never = (name: string) => {
while (true) {
console.log(name);
}
};
// 函数的重载
function Test(): string;
function Test(name: string, old: number): string;
function Test(name?: string, old?: number) {
if (name && old) {
return `${name}:${old}`;
}
return 'none';
}
Test(); // 'none'
Test('zwq'); // 没有需要 1 参数的重载,但存在需要 0 或 2 参数的重载
Test('zwq', 29); // 'zwq:29'
interface A {
(): string;
(name: string, old: number): string;
}
const a: A = (name?: string, old?: number) => {
if (name && old) {
return `${name}:${old}`;
}
return 'none';
};
a(); // 'none'
a('zwq'); // 没有需要 1 参数的重载,但存在需要 0 或 2 参数的重载
a('zwq', 29); // 'zwq:29'
// 内置类型的使用
const a = {
name: 'zwq',
old: 29,
email: '888888@qq.com',
address: '****路',
hobby: '唱歌',
};
// typeof 获取值的类型
type A = typeof a;
// type A = {
// name: string;
// old: number;
// email: string;
// address: string;
// hobby: string;
// };
// keyof 类型的key
type B = keyof A;
// type B = "name" | "old" | "email" | "address" | "hobby";
// Record 映射
type C = Record<B, string>;
// type C = {
// name: string;
// old: string;
// email: string;
// address: string;
// hobby: string;
// };
// Exclude 排除
type D = Exclude<B, 'hobby' | 'address' | 'email'>;
// type D = "name" | "old";
// Pick 挑拣属性
type E = Pick<A, 'hobby' | 'address' | 'email'>;
// type E = {
// hobby: string;
// address: string;
// email: string;
// };
// Omit 忽略属性
type F = Omit<A, 'name' | 'old'>;
// type F = {
// hobby: string;
// address: string;
// email: string;
// };
// Extract 交集
type G = Extract<B, 'hobby' | 'aaa'>;
// type G = "hobby";
// infer推断
// 通过typeof可以回去值的类型以及keyof获取interface的key
// 如何获取函数的参数的类型呢?
// infer类型推断 就相当与函数的args占位符
type VariadicFn<A extends any[]> = (...args: A) => any;
type ArgsType<T> = T extends VariadicFn<infer A> ? A : never;
type Fn = (a: number, b: string) => string;
type Fn2Args = ArgsType<Fn>; // [number, string]
// interface
// interface 形式与object一样相当于type的子集
interface A {
name: string;
old: number;
[key: string]: any; // 动态扩展使用
}
const a: A = {
name: 'zwq',
old: 1,
};
a.hobby = '唱歌';
// 接口继承
interface B extends A {
hobby: string;
}
const b: B = {
name: 'zwq',
old: 1,
hobby: '唱歌',
};
// 类型断言
interface A {
name: string;
old: number;
hobby: string;
}
// 接口继承
interface B extends Omit<A, 'hobby'> {
addr: string;
}
const a: A = {
name: 'zzz',
old: 29,
hobby: 'sing',
};
const b: B = {
name: 'zzz',
old: 29,
addr: '****',
};
const userInfo = (mes: A | B) => {
if ((mes as B).addr) {
const b = mes as B;
console.log(b.addr);
}
if ((mes as A).hobby) {
const a = mes as A;
console.log(a.hobby);
}
};
参考文档: