1

My typescript type starts with an unknown key, followed by two known keys. I tried to use an index signature for the first key, but that seems to affect all following keys as well.

Desired result:

let objOne:MyThing = {"anything":0.534843, label:"hello", confidence:0.94394}
let objTwo:MyThing = {"bleep":0.348989843, label:"heyyy", confidence:0.48394}

I tried to write this type as

interface MyThing {
    [key: string] : string;
    label : string;
    confidence : number;
}

But now confidence also needs to be a string for some reason.

How can I have just the first key to be any string, but the other two keys should be named label and confidence ?

Kokodoko
  • 26,167
  • 33
  • 120
  • 197
  • Typescript will _not_ make this easy for you. If you can change your data structure, you are much better off putting that index signature in its own property `{ a: number, data: { [key: string]: string } }` – Alex Wayne Jun 02 '21 at 18:53
  • Thanks, I missed that post completely – Kokodoko Jun 02 '21 at 18:55

0 Answers0