Can anyone explain why TypeScript doesn't flag an error in this scenario, where null is assigned to a property expecting number or undefined?
type T1 = {
value: number | null
}
type T2 = {
value?: number
};
const v1: T1 = {
value: null
};
const v2: T2 = {
// error, as expected
value: v1.value
}
const v3 = {
// no error
value: v1.value
} as T2
One would assume that this type assertion should only be possible after asserting it to unknown first.
const v4 = ({
value: v1.value
} as unknown) as T2
The documentation states that
TypeScript only allows type assertions which convert to a more specific or less specific version of a type.
Is the type becoming more specific or less specific?