Having a type with a string index signature:
declare var result: {
[key: string]: number;
};
Trying to assign an interface to the type results in an error:
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'.
However, assigning a type alias works fine:
type TData = {
a: number;
b: number;
};
declare var data1: TData;
result = data1; // [SUCCEED] why?
Expecting interfaces and type aliases to behave the same when assigned to a type with an index signature raises questions. Why do they actually exhibit different behaviors?