7
votes
const test = [
    {
        "a": 1
    },
    {
        "b": 1
    }
]
interface t {
    [key: string]: number
}
const ttt: t[] = test

Property '"b"' is incompatible with index signature. Type 'undefined' is not assignable to type 'number'. it works if I rename the key either b or a both of same key.

1
Remove the type annotation.Aluan Haddad

1 Answers

6
votes

Because test doesn't have a type, it's being inferred to this type:

({ a: number; b?: undefined; } | { b: number; a?: undefined; })[]

test is then assigned to ttt, but it's not compatible to have a possibly undefined b key in the new interface t.

You can fix this by add the type directly to test:

const test: t[] = [
    {
        "a": 1
    },
    {
        "b": 1
    }
]