TS:泛型工具类型

  1. Partial<Type>
  2. Readonly<Type>
  3. Pick<Type, Keys>
  4. Record<Keys, Type>

Partial<Type>

用来构造 (创建) 一个类型, 将 Type 的所有属性设置为可选

interface Props{
    id:string
    children:number[]
}
type PartialProps=Partial<Props>

Readonly<Type>

用来构造一个类型, 将 Type 的所有属性都设置为 readonly (只读 )

interface Props{
    id:string
    children:number[]
}
type ReadonlyProps=Readonly<Props>

构造出来的结构完全相同,但所有属性为只读

Pick<Type, Keys>

从 Type 中选择一组属性来构造新类型

interface Props{
    id:string
    children:number[]
}
type PickProps=Pick<Props,'id'>
//想当于又声明了 一个{id:string}类型

Record<Keys,Type>

构造一个对象类型,属性键为 Keys ,属性类型为 Type。

type Recordtype = Record<'id'|'name',string>
//想当于
interface Recordtype{
    id:string
    name:string
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容