#Deep Pick array inference on generic argument

3 messages · Page 1 of 1 (latest)

mossy fjord
#

I am creating a generic type DeepPick so it can create new sub types while giving intellisense to users

interface User {
    emailAddress: string;
    verification: {
        verified: boolean;
        verificationCode: string;
    }
    activeApps: { 
        name: string, 
        size: number, 
        stars: 1 | 2 | 3 | 4 | 5, 
        developers: string[], 
        downloads: number 
    }[];
}

type DeepPartial<T> = {
    [P in keyof T]?: T[P] extends Array<infer U>
    ? Array<DeepPartial<U>>
    : T[P] extends ReadonlyArray<infer U>
    ? ReadonlyArray<DeepPartial<U>>
    : DeepPartial<T[P]>
}

type DeepPick<U, T extends DeepPartial<U>> = T

const newUser = {} as DeepPick<User, {
    activeApps: {
        // Need intellisense here to give name, size, stars, developers and downloads
    }[],
}>

it does give intellisense while typing non array fields like email, verification. and also inside verification. but it is not giving inside activeApps which is a array.

lyric turtleBOT
#
Royal_lobster#4070

Preview:ts interface User { emailAddress: string verification: { verified: boolean verificationCode: string } activeApps: { name: string size: number stars: 1 | 2 | 3 | 4 | 5 developers: ...

mossy fjord
#

The DeepPartial also works while creating User object. it is just not working in generic while doing extend