4

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?

Nouh Belahcen
  • 774
  • 1
  • 11
  • 36
  • There is no way to combine dynamic and specific keys. https://github.com/microsoft/TypeScript/issues/17867 here you can find some partically worked approaches. – Mike Kokadii Oct 14 '21 at 14:25
  • Does this answer your question? [How to define Typescript type as a dictionary of strings but with one numeric "id" property](https://stackoverflow.com/questions/61431397/how-to-define-typescript-type-as-a-dictionary-of-strings-but-with-one-numeric-i) – Mike Kokadii Oct 14 '21 at 14:26

0 Answers0