I'm pretty new to typescript and working on converting some javascript into typescript. I have a lot of nearly-json-schema objects. I'm working on building out types so the schemas produce types and the schemas themselves are type checked. Which has me running into this bizarre behavior on unions.
interface Foo {
type: StringConstructor,
foo: string
}
interface Bar {
type: BooleanConstructor,
bar?: string
}
type TestType = Foo | Bar
const t1 : TestType = { type: String, foo: "hello" }
const t2 : TestType = { type: String } //Properly emits error here, foo is missing
const t3 : TestType = { type: Boolean, foo: "hello" } //Missing error here, foo does not exist in Bar
const t4 : Bar = { type: Boolean, foo: "hello" } //Properly produces error
I am truly baffled by type union behavior. Why does t3 not emit an error? The object it's being assigned is not shaped like a Foo nor a Bar. Attempting to assign the same object shape to a Bar will emit an error. VSCode's autocomplete also suggests foo in t3 as a valid property. Clearly I am missing something.