type 和 interface 的区别
type Size = { size: string }
type Arr = [string, number]
type Num = Number
type Name = 'a' | 'b' | 'c'
type Person = {
[key in Name]: string
}
let person: Person = { a: 'a', b: 'b', c: 'c' }
interface Cat {
name: string
age: number
color: string
}
interface Cat {
onClick: (value: string) => void
}
interface Height {
height: string
}
interface User extends Height {
age: number
}
联合类型
const data:string | number
交叉类型
type Dog = { name: string; age: number; color: string }
type Cat = Dog & { id: string }
const cat1: Cat = {
name: '小白',
age: 2,
color: '白色',
}
const cat2: Cat = {
id: 'abc',
name: '小白',
age: 2,
color: '白色',
}
keyof
type Dog = { name: string; age: number }
const dog: Dog = { name: '大黄', age: 3 }
const A = (key: keyof Dog) => {
console.log(dog[key])
}
A('name')
typeof
const data = {
name: 'his',
age: 22,
};
let Cat: typeof data;
Pick (选择属性)
type Dog = { name: string; age: number; color: string }
type Dog2 = Pick<Dog, 'name' | 'age'>
let dog: Dog2
Record (属性映射)
type Dog = { name: string; age: number; color: string }
const dog: Record<string, string> = {
a: 'a',
b: 2,
}
const dog2: Record<keyof Dog, string> = {
name: 'ww',
a: 'a',
}
in(映射)
type Dog = "name" | "age" | "color"
let dog: { [key in Dog]: string }
Partial (不完全的)
type Dog = { name: string; age: number; color: string }
const a = (value: Dog) => {}
a({ name: 'aa', age: 3, color: 'red' })
const b = (value: Partial<Dog>) => {}
b({})
Required (必须的)
type Dog = { name?: string; age?: number; color?: string }
const a = (value: Required<Dog>) => {}
a({ name: 'aa' })
Readonly (只读)
type Dog = { name: string; age: number; color: string }
const dog: Readonly<Dog> = {}
dog.age = 2
Exclude (排除)
type Dog = 'namne' | 'age' | 'color'
let dog: Exclude<Dog, 'name'>
dog = 'age'
dog = 'name'
Omit (省略)
type Dog = { id:string, name: string; age: number; color: string }
const a = (value: Omit<Dog, 'name'>) => {}
a({ age: 2, color: 'red' })
a({ age: 2, color: 'red', name: '大黄' })
const add =(value:Omit<Dog,"id"> )=>{}
const update =(value:Dog)=>{}
enum (枚举)
enum Direction {
Up,
Down,
Left,
Right,
}
console.log(Direction.Up,Direction.Down);
enum Direction2 {
Up = 2,
Down,
Left,
Right,
}
console.log(Direction2.Up,Direction2.Down);
enum Code {
success = 200,
error = 500,
}
enum Code2 {
success = 'success',
error = 'error',
}
获取枚举value
export enum UserRole {
Admin = 'admin',
Root = 'root',
User = 'user',
}
type Roles = `${UserRole}`
获取枚举key
export enum UserRole {
Admin = 'admin',
Root = 'root',
User = 'user',
}
type Roles =keyof typeof UserRole
泛型
const getData = <T>(data: T): T => {
return data;
};
getData({name:"22"}).name
Parameters(参数)
const getData=(param:{name:string,age:number}) => console.log(param)
type P = typeof getData
let param:Parameters<P>[0] = {name:"yyds", age:24 }
console.log(param.name)
ReturnType(返回值)
const getData = (): { name: string; age: number } => {
return {
name: "yq",
age: 18,
};
};
type T = ReturnType<typeof getData>;
Awaited (promise结果)
type P = Promise<{ name: string; age: number }>;
type T = Awaited<P>;
其他
Exclude<T, U> -- 从T中剔除可以赋值给U的类型。
Extract<T, U> -- 提取T中可以赋值给U的类型。
NonNullable<T> -- 从T中剔除null和undefined。
ReturnType<T> -- 获取函数返回值类型。
InstanceType<T> -- 获取构造函数类型的实例类型。