I am working with multiple interfaces, each containing a property called `type`.
interface Type1 { type: 'key1', name: string }
interface Type2 { type: 'key2', description: string, name: string }
interface Type3 { type: 'key3', cost: number }
In addition to the interfaces, I have a mapping interface:
interface TypeMap {
key1: Type1,
key2: Type2,
key3: Type3
}
There is also a union type:
type TypeElement = Type1 | Type2 | Type3
I have created a map object that uses records of each type separately, similar to the mapping interface:
const allTypes: { [key in keyof TypeMap]: (record: TypeMap[key]) => void } = {
key1: (record) => {},
key2: (record) => {},
key3: (record) => {},
}
However, when passing a variable of type `TypeElement` into a callback from `allTypes`, an error occurs:
const func = (record: TypeElement) => {
const cb = allTypes[record.type]
return cb(record)
}
The error message states:
Argument of type 'TypeElement' is not assignable to parameter of type 'never'. The intersection 'Type1 & Type2 & Type3' was reduced to 'never' because property 'type' has conflicting types in some constituents. Type 'Type1' is not assignable to type 'never'.
Why does this error occur? What is the issue here? How can I pass a record to `cb` without encountering this error? Using `record as never` or `as any` is not the solution I am seeking!