Can someone help me understand the behavior of this sample code?
type A = {
field: string
}
type B = {
field: number
}
//I expect A | B is equivalent to field: A["field"] | B["field"]
type AORB = A | B
type AORBField = {
field: A["field"] | B["field"]
}
type BaseTypes = {
field: number | string
}
//Toggle between AORB and AORBField
type AB = AORB;
//type AB = AORBField;
//type AB = BaseTypes
const isA = (t: AB): t is A => {
return typeof t.field === "string";
}
const isB = (t: AB): t is B => {
return typeof t.field === "number";
}
const test = (x: AB) => {}
const testA = (x: A) => {}
const testB = (x: B) => {}
const testString = (x: string) => {}
const testNumber = (x: number) => {}
const getField = () => {
const val = Math.random();
return Math.random() % 2 ? val.toString(): val
}
const getDummyObject = () => {
const val = Math.random();
return { field: Math.random() % 2 ? val.toString(): val }
}
//Why error?
const ab: AB = {
//field: 1 //This will pass for AORB
//field: "string" //This will pass for AORB
field: getField() //This will pass for AORBFields
}
//Why error?
const abc: AB = getDummyObject();
if (isA(ab)){
const a: A = ab;
testString(a.field)
testNumber(a.field) //Expected err
testA(a)
test(ab)
}
else
{
const b: B = ab; //This will fail for AORBField and BaseTypes, but that is expected since else statement cannot figure out main type
testString(b.field) //Expected err
testNumber(b.field)
testB(b)
test(ab)
}
I'm puzzled by the TypeScript errors on ab and abc assignments. I thought AORB = AORBField = BaseTypes would allow similar assignments. Can anyone shed some light on this issue?