TypeScript-高级类型

102 阅读3分钟

1.交叉类型--多个类型合并

interface Person{}
interface ID{}
type newAll=Person & ID
const ppp=<Person & ID>{}

2.联合类型-表示类型可以是其中几种类型的一种

interface Brid{}
interface Fish{}
function get():Fish|Brid{}

3.类型保护与区分类型-联合类型只能访问几种类型公有的,如果要访问其中一种类型独有的,使用类型断言

let pp=get()
if((<Fish>pet).xxx){}

4.用户自定义的类型保护 类型谓词is parameterName is Type

function isFish(pp:Brid|Fish):pp is Fish{
    return (<Fish>pp).xxx!===undefined
}

5.接口与类型别名的区别

- 接口可以在任何地方使用,类型别名不能声明在右侧的任何地方
- 类型别名不能被extends和implements

6.字符串字面量类型-允许你指定字符串必须的固定值

7.数字字面量类型

8.keyof 是检查出已有类型对象的键的数组集合

9.映射类型--想要改变旧类型中的类型,创建新类型--只能使用type定义,不能使用接口

interface Person{
    name?:string
}

type Readonly<T>{
    readonly [P in keyof T]:T[P]
}
type Readonly<T>{
    [P in keyof T] ?:T[P]
}

type PersonPartial=Readonly<Person>

10. extends 类型约束 判断类型是否能赋值给另一个类型

type extendsType<T,U>=T extends U?T:never

11.类型映射 in 会遍历指定类型中key或者 遍历联合类型

type inType<T>={
    [P in keyof T]:T
}

12.待推断类型infer 可以直接使用

type parameType<T>=T extends (value:infer P)=>any?P:T
type FunctionType = (value: number, name: string) => void
type Param = ParamType1<FunctionType>
type OtherParam = ParamType1<boolean>
type FunctionTypeTwo = ParamType1<(value: string) => void>

13. 原始类型保护 typeof

function print(name: string | number) {
  if (typeof name === "string") {
    console.log(name.split(","))
  } else {
    console.log(name.toFixed(2))
  }
}

14. 类型保护 instanceof

class Bird {
  fly() {}
}

class Fish {
  swim() {}
}
function start(pet: Bird | Fish) {
  if (pet instanceof Bird) {
    pet.fly()
  } else {
    pet.swim()
  }
}

15. keyof 索引类型查询操作符 keyof T的结果为T上已知的公共属性名的联合 只能返回类型上已知的公共属性名

type PersonProp = keyof Person

16.类型访问操作符 T[K] 返回T 中对应属性的P的类型

type PersonName = Person["name"]

17. unknown 不可预先定义的类型,保留静态检查的能力

const num = 10;
(num as unknown as string).split(",")

18. void 函数没有返回值

function foo() {}
const a = foo()

19. never 没法正常结束返回的类型 不能被赋值

function foo1(): never {
  throw Error("error")
}

20. Record 快速创建一个类型,每一个属性都是必填

type Person = Record<"name" | "nickName" | "age", string>

const obj: Record<string, string> = { name: "111", address: "333" }

21. Partial 将类型定义的所有属性 变为可选

type XiaoMine = Partial<Person>

22.Required 将类型属性变为必填

type XiaoLan = Required<Person>

23. Readonly 将类型定义的属性变为只读

type ZhangSan = Readonly<Person>

24. ReadonlyArray 只读数组

type ReadArray = ReadonlyArray<Person>

25. Pick 选取指定一组属性,返回一个新的类型定义

type ID = Pick<Person, "name" | "nickName">

26. Omit 排除接口中指定的 属性

type House = Omit<Person, "name" | "age">

27. Exclude 排除一个联合类型中指定的子类型

type Card = Exclude<string | number | boolean, boolean>

28. Extract 提取联合类型中指定的类型

type Animal = Extract<string | number, string>

29. NonNullable 过滤掉联合类型中null和undefined

type U = NonNullable<string | undefined | boolean | number>

30. Parameters 获取函数的全部参数类型,以元组的类型返回

type F1 = (a: string, b: number) => void

type FunctionExample = Parameters<F1>

31. ConstructorParameters 获取构造函数的全部参数

interface c1 {
  new (a: string, b: number): void
}
type Constructor = ConstructorParameters<c1>

32. ReturnType 接收函数声明,返回函数的返回值类型,如果有多个类型则以联合类型 方式返回

type ReturnFunction = ReturnType<F1>

33. InstanceType 获取构造函数的返回类型,如果多个,就是联合类型的方式返回

type ReturnConstructor = InstanceType<c1>

34. ThisParameterType 获取函数中this的数据类型,如果没有就返回unknown

interface Foo {
  x: number
}
function fn(this: Foo) {}
type Test = ThisParameterType<typeof fn>

35. OmitThisParameter 移除函数中的this

type NonReturnFn = OmitThisParameter<typeof fn>