I've been in the process of upgrading a large TypeScript codebase to enforce strict null-checks. This codebase contains numerous types with optional properties:
interface MyInterface {
member1?: number;
member2?: string;
}
Additionally, it defines a type Nullable<T> = T | null
and includes numerous return null
statements.
Now, I'm encountering numerous compiler errors that highlight the inability to convert T | null
to T | undefined
, and vice versa. For example:
interface MyInterface {
member1?: number;
member2?: string;
}
const myFunction = () => {
return null;
}
const item: MyInterface = {};
item.member1 = myFunction(); // <== cannot assign null to undefined
While I appreciate the benefits of strict null-checking in TypeScript, I find the distinction between null
and undefined
unnecessary in this project. The functions that return null
essentially return nothing - the specific value (null or undefined) is irrelevant. The same applies to the optional properties - they are either assigned a value or remain undefined.
Furthermore, I prefer not to convert member1?: number;
to member1: Nullable<number>;
, or modify the existing return null
statements.
Is there a way to disable the distinction between null
and undefined
, at least in TypeScript?