Acid Coder
Acid Coder

Reputation: 2747

typescript Omit omit all other members if one of the member is { [index: string]: any }

type a = { a: 1, [index: string]: any , name:string, b?:boolean}
type b = Omit<a,'a'> //b is {[index: string]:any}

playground

type b would be {[index: string]:any}, but I expect it to be {[index: string]: any , name:string, b?:boolean}

why is this happening?

Upvotes: 1

Views: 92

Answers (1)

Acid Coder
Acid Coder

Reputation: 2747

I found the solution and explanation in this thread

type a = { a: 1, [index: string]: any , name:string, b?:boolean}

declare type _removeIndexSignature<T> = Pick<
        T,
        {
            [K in keyof T]: string extends K ? never : number extends K ? never : K
        } extends { [_ in keyof T]: infer U }
            ? U
            : never>

type b = _removeIndexSignature<a> //{ a: 1, name:string, b?:boolean}

playground

Upvotes: 1

Related Questions