美文网首页
typescript 类型练习之重新构造

typescript 类型练习之重新构造

作者: McDu | 来源:发表于2022-03-21 19:09 被阅读0次
    type MapType<T> = {
        [
            Key in keyof T 
                as `${Key & string}hhh${Key & number}${Key & string}`
        ]: [T[Key], T[Key], T[Key]]
    }
    
    type res = MapType<{a: 1, b: 2, c: 3}>;
    
    type obj = {
        readonly name: string;
        age?: number;
        gender: boolean
    }
    
    type Mapping<Obj> = {
        [Key in keyof Obj]: Obj[Key]
    }
    
    type res2 = Mapping<{name: 'sss', gender: 1, ss: '3'}>
    
    type UppercaseKey<Obj extends object> = {
        [Key in keyof Obj as Uppercase<Key & string>]: Obj[Key]
    }
    
    type res3 = UppercaseKey<{name: '333'}>
    
    type UppercasKey<Obj extends Record<string, boolean>> = {
        [Key in keyof Obj as Uppercase<Key & string>]: Obj[Key]
    }
    
    type res4 = UppercasKey<{age: true}>
    
    type ToPartial<T> = {
        [Key in keyof T]?: T[Key]
    }
    
    type res5 = ToPartial<{
        name: string;
        age: number;
    }>
    
    type ToMutable<T> = {
        -readonly [Key in keyof T]: T[Key]
    }
    
    type res6 = ToMutable<{
        readonly name: string;
        age: number;
    }>
    
    type ToRequired<T> = {
        [Key in keyof T]-?: T[Key]
    }
    
    type res7 = ToRequired<{
        name?: string;
        age: number;
    }>
    
    type FilterByValueType<Obj extends Record<string, any>, ValueType> = {
        [Key in keyof Obj  as ValueType extends Obj[Key] ? Key : never] : Obj[Key]
    }
    
    type res8 = FilterByValueType<{num: string, age: number}, string>
    

    相关文章

      网友评论

          本文标题:typescript 类型练习之重新构造

          本文链接:https://www.haomeiwen.com/subject/kxvtjrtx.html