I'm currently converting a large TypeScript codebase to strict null-checks. The codebase has many types with optional members:
interface MyInterface {
member1?: number;
member2?: string;
}
Furthermore, it uses a type Nullable<T> = T | null
, and has many return null
statements.
Now I'm facing many compiler errors which basically state that T | null
cannot be converted to T | undefined
, or vice versa, like in this example:
interface MyInterface {
member1?: number;
member2?: string;
}
const myFunction = () => {
return null;
}
const item: MyInterface = {};
item.member1 = myFunction(); // <== can't assign null to undefined
I love strict null-checking in TypeScript, but I have no use for the distinction between null
and undefined
. I can appreciate that there are some use cases where it makes sense to distinguish between null
and undefined
, but in this project, it really does not play an important part. These functions that return null
just return nothing - it does not matter whether it is null
or undefined
. Same goes for the optional members - they are either set to a value, or they are not.
I also prefer to not convert the member1?: number;
to member1: Nullable<number>;
, and to leave the return null
as they are
Can I disable the distinction between undefined
and null
, at least in TypeScript?