Paul Han
Paul Han

Reputation: 409

Why do TypeScript interfaces not support index signatures while type aliases do?

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

Upvotes: 4

Views: 328

Answers (1)

Paul Han
Paul Han

Reputation: 409

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

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

Upvotes: 2

Related Questions