Is there a way to create an interface for an object defined with the as const
syntax?
The Events
type does not work as intended and causes issues with enforcement.
const events = { // how can I define an interface for the events object?
test: payload => console.log(payload),
another: payload => console.log(payload)
} as const;
type Events = { [key: string]: (payload: Payload) => void }; // this approach does not work for events object
type Payload = { [key: string]: any };
type EventName = keyof typeof events;
const emit = (event: EventName, payload: Payload) => {
events[event]?.(payload);
} // (parameter) event: "test" | "another"
emit('foo', { test: 1 }); // Argument of type '"foo"' is not assignable to parameter of type '"test" | "another"'.