I have got some strange behavior of TS while work with enums as object keys. I expect TS error, but it is not, and I don't understand why.
enum List {
sm = "sm",
md = "md",
}
export interface Dictionary<T = any> {
[index: string]: T;
}
export type OptionalDictionary<T, K extends string = string> = { [P in K]?: T };
type MessageType = Dictionary<string | null>;
type MessagesType = OptionalDictionary<MessageType, List>;
type Values = { receiver: List; text: string };
const values = { receiver: List.sm, text: "123" };
const { receiver, text } = values;
const data: MessagesType = {
// [receiver]: { text }, // correct
[receiver]: text // wrong, but no error
};
const data2: MessagesType = {};
// data2[receiver] = { text }; // correct
data2[receiver] = text; // wrong, had error
console.log(data[receiver] === data2[receiver]); // true