1

I need to have an object that can have any property of one type but it also needs to have specific, predefined properties of another type.

For example:

const foo = {
    $id: 'id',
    bar: 123
}

$id is predefined and needs to be a string. bar is not predefined, and needs to be a number.

The type definition, in concept, would look like this:

type Foo = {
    $id: string;
    [key: string]: number;
}

This type does not work. Is there any way to construct the desired type? I have tried

type Foo = {
    [key: string]: number;
} & { $id: string };

but that does not work either.

Lehks
  • 2,582
  • 4
  • 19
  • 50
  • There is unfortunately no such type. Please see the answer to the linked question for workarounds. Note that you have no *mapped type* in this question; perhaps you meant *index signature* instead. – jcalz Jul 19 '22 at 13:16

0 Answers0