We had an Issue in redux-starter-kit where a user of the library had strictNullChecks disabled and one of our type tests was short-cicuiting, returning types for a different case.
This test should return the True-Parameter or the False-Parameter depending if the P parameter contains undefined or not.
This is the current code:
type IfMaybeUndefined<P, True, False> = [undefined] extends [P] ? True : False;
So I'm expecting both of these to return "yes":
IfMayBeUndefined<number, "no", "yes">
IfMayBeUndefined<number|undefined, "yes", "no">
Now I'm asking myself if there is another way to test this, even with strictNullChecks: false
I do not believe this is possible.
The name "strictNullChecks" is slightly misleading as it can suggest that it is merely instructing the TypeScript compiler to perform additional checks and emit additional warnings. However, it actually changes the underlying behavior of the type checker and the semantics of the null
and undefined
types. Per the TypeScript Guide,
In strict null checking mode, the null and undefined values are not in the domain of every type and are only assignable to themselves and any (the one exception being that undefined is also assignable to void). So, whereas T and T | undefined are considered synonymous in regular type checking mode (because undefined is considered a subtype of any T), they are different types in strict type checking mode, and only T | undefined permits undefined values. The same is true for the relationship of T to T | null.