define an interface or type with dynamic keys and specific key

Viewed 107

I'm trying to define a type with dynamic keys and specific keys.

export type Item = {
    disabled?: boolean,
} & { [k: string]: string | number};

And I'm trying to use it as a Type

function process(t: Item): void {}
process({ iconName: 'person_outline', label: 'Option 2', key: 2, disabled: true })

this is not working because of this error

Property 'disabled' is incompatible with index signature.

Type 'boolean' is not assignable to type 'string | number'.

and it works properly when I do it in this way

export type VerifyT<T> = { disabled?: boolean } & { [K in keyof T]: K extends "disabled" ? unknown : string | number };

function process<Item extends VerifyT<Item >>(t: Item ): void;

function process(t: Item): void {}

process({ label: 'Option 2', key: 2, disabled: true })

and for my case, I want to use it inside an interface, for example,

export interface Test<ItemInterface VerifyT<Item >> {
  items: ItemInterface 
}

this is not working, I got an error Property 'disabled' is incompatible with index signature. is there any way to use it as a type inside interface?

0 Answers
Related