I'm reading data from a file, which may have more data in it than my code cares about. I have a typescript type for each object type I want to process. But in the case of some unsupported object type, I want do something with that object.
The problem is that typescript thinks I have exhausted all possibilities and that my default clause is impossible to get to.
// Untyped example data source.
function loadFromSomeFile(): any {
return [{ objType: "A", a: 1 }, { objType: "B", b: 2 }] as any
}
// Union type of supported data from data source.
type A = { objType: "A", a: number }
type B = { objType: "B", b: number }
type ObjTypes = A | B
// Load the data.
const arr: ObjTypes[] = loadFromSomeFile()
// Switch on the type of each object.
for (const obj of arr) {
switch (obj.objType) {
case "A":
console.log('A', obj.a)
break
case "B":
console.log('B', obj.b)
break
default:
// Fall though case for unsupported objType
console.log('unkown objType: ' + obj.objType)
// ^ TS Error: objType does not exist on type 'never'
}
}
Error on Typescript Playground
I thought of trying to add a third option to the union like:
type X = { objType: string } // unknown
type ObjTypes = A | B | X
But now when obj.objType === 'A' typescript can't tell if it's an A or an X since it's a valid type for both.
How can I tell typescript that there may also be unknown and unhandled values, and that my list is not exhaustive?