4

I have a type with a string index signature:

declare var result: {
  [key: string]: number;
};

Assigning an interface to the type fails:

interface IData {
  a: number;
  b: number;
}
declare var data: IData;
result = data; // Error: Type 'IData' is not assignable to type '{ [key: string]: number; }'. Index signature for type 'string' is missing in type 'IData'.

But assigning a type alias succeeds:

type TData = {
  a: number;
  b: number;
};
declare var data1: TData;
result = data1; // [SUCCEED] why?

I expected interfaces and type alias to have the same behavior when assigning to a type with an index signature. Why do they actually behave differently?

TS Playground link

0

1 Answer 1

2

redundant, subscribe to this issue, and the spec is out of date

https://github.com/microsoft/TypeScript/issues/15300

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.