Based on Scott Wlaschin examples in F#, I'm trying to design a functional domain model using Typescript.
But, in order to have more strictly defined types for optional params, I've noticed that typescript is not validating properties I expected it would.
For example, for this types:
type ObjectOne = {
propertyOfOneMustBeNumber: number
}
type ObjectTwo = {
propertyOfTwoMustBeNumber: number
}
type Options = ObjectOne | ObjectTwo
I have the following use cases:
// valid
const test1: Options = {
propertyOfOneMustBeNumber: 1,
propertyOfTwoMustBeNumber: 2
}
// invalid:
// Object literal may only specify known properties,
// and 'unrecognized_property' does not exist in type 'Options'.
const test2: Options = {
propertyOfOneMustBeNumber: 1,
unrecognized_property: 'string'
}
// valid
const test3: Options = {
propertyOfOneMustBeNumber: 1,
propertyOfTwoMustBeNumber: 'string'
}
I understand why there was an error for a test2
variable,
but why there are no erros for test3
variable,
even though propertyOfTwoMustBeNumber
is a string and not a number?
This is also valid in Flow type system.