Typescript 学习

interface IUser {
  name: string
  age: number
  department: string
}
type unionKey = keyof IUser

type values = IUser[unionKey]

////////
type People = {
  name: string
  age?: string
  location: string
}

// 义一个对象的 key 和 value 类型
type petsGroup = 'dog' | 'cat' | 'fish'
interface IPetInfo {
  name: string
  age: number
}
type IPets = Record
// Partial: 生成一个新类型,该类型与 T 拥有相同的属性,但是所有属性皆为可选项
type PartialPeople = Partial
// 必填, 生成一个新类型,该类型与 T 拥有相同的属性,但是所有属性皆为必选项
type RequiredPeople = Required
// 只读, 生成一个新类型,T 中的 K 属性是只读的,K 属性是不可修改的。
type ReadonlyPeople = Readonly
// 生成一个新类型,该类型拥有 T 中的 K 属性集 ; 新类型 相当于 T 与 K 的交集
type NamePeople = Pick
// 省略的, 生成一个新类型,该类型拥有 T 中除了 K 属性以外的所有属性
type PersonWithoutLocation = Omit
// 排除, 如果 T 是 U 的子类型则返回 never 不是则返回 T
type AgeExclude = Exclude<'name' | 'age' | 'abd', 'age'>
// 并集
type Fruits = 'apple' | 'banana' | 'peach' | 'orange'
type DislikeFruits = 'apple' | 'banana'
type FloveFruits = Extract
// 从泛型 T 中排除掉 null 和 undefined
type t = NonNullable<'name' | undefined | null>

///////////
const aa: NonNullable = {
  name: '222',
  age: 1,
}
console.log(aa)

/////////
// string | number
type T0 = NonNullable
// string[]
type T1 = NonNullable

const x: T0 = 'coolcou.com'
const y: T0 = 100
const z: T1 = ['coolcou.com', 'baidu.com']

console.log(x)
console.log(y)
console.log(z)
/////////
// 获取方法的参数类型
function updata(str: string, age: number) {
  return { str, age }
}
type ArrType = Parameters
// 获取方法返回的类型
type Return = ReturnType

// 获取构造函数的参数类型
class Dog {
  name: string
  constructor(name: string) {
    this.name = name
  }
}
type xxxx = ConstructorParameters
// 大写类型
type IUppercase = Uppercase<'abc'>
////// 快速取出 Promise 泛型
type DemoType = Promise
type ObtainPromiseResolveType = T extends Promise ? R : never
type TT = ObtainPromiseResolveType // string
////// 快速取出 Array 泛型
type DemoType1 = Array
type ObtainPromiseResolveType1 = T extends Array ? R : never
type TT1 = ObtainPromiseResolveType1 // string
////// 快速取出 K/V 结构中所有 V 的类型
interface MyKVObj {
  a: string
  b: number
  c: boolean
}
type ObjVals = MyKVObj[keyof MyKVObj] // string | number | boolean
//////

export {}
发表评论
留言与评论(共有 0 条评论) “”
   
验证码:

相关文章

推荐文章