2
votes

I need to define a interface for a data which comes from the server where id in a number, but all other properties are strings.

I tried:

interface AnyChartsData {
  id: number;
  [key: string]: string;
}

but i got this error:

[ts] Property 'id' of type 'number' is not assignable to string index type 'string'. [2411]

How can I define this interface properly?

1

1 Answers

3
votes

You have to use a union type since id is a number and not a string e.g.

interface AnyChartsData {
    id: number;
    [key: string]: string | number;
}