0

I am attempting to define a type which will allow any keys to match with a string value, but reserve one specific key such that its value is a number. Here is a toy example:

    type Type = {
      [key: string]: string;
    } & {
      [key in 'reserved']: number;
    };

    const example: Type = {
      otherField: 'some-string',
      reserved: 0,
    };

I receive a compile error here:

Type '{ otherField: string; reserved: number; }' is not assignable to type 'Type'.
  Type '{ otherField: string; reserved: number; }' is not assignable to type '{ [key: string]: string; }'.
    Property 'reserved' is incompatible with index signature.
      Type 'number' is not assignable to type 'string'

which seems to ignore the declaration I may for the key reserved. Can anyone point me in the right direction on how to resolve this?

I am using Typescript 4.9.5

0 Answers0