interface Test {
on(event: 'a', listener: (stats: string) => void)
on(event: 'b' | 'c', listener: (stats: string) => void)
}
const test: Test = {
on(event, listener) {}
}
type Events = 'a' | 'b' | 'c'
const arr: Events[] = ['a', 'b', 'c']
arr.forEach(e => {
test.on(e, () => { })
})
When writing TypeScript code like this to bind events to the 'test' object, an error may occur with a message such as:
'Argument of type 'Events' is not assignable to parameter of type '"b" | "c"'. Type '"a"' is not assignable to type '"b" | "c"'. What can be done to prevent this kind of error?