I've made a PR
Search Terms
Nullable
Suggestion
TypeScript already comes with a NonNullable type, defined in lib.es5.d.ts:
/**
* Exclude null and undefined from T
*/
type NonNullable<T> = T extends null | undefined ? never : T;
I propose that its inverse, Nullable, is added to the built-in type utilities.
I'm aware that your general position in terms of built-in type utilities is that you don't extend them unless strictly necessary, but I do think this is an important addition for the following reason:
The built-in type utilities generally offer inverse types. For example, Partial is the inverse of Required. And, Pick is the inverse of Omit. Because of this, to me it naturally follows that NonNullable should have an inverse called Nullable.
Use Cases
For the following examples, nullable is defined as null or undefined, thereby following TypeScripts definition from the NonNullable utility type.
A few examples that comes to mind:
- Required keys with nullable values.
- Function arguments that may have a nullable value.
Additionally, it may help with simplifying built-in type definitions:
For example in lib.es5.d.ts, the catch and then declarations for the Promise interface can be simplified:
interface Promise<T> {
then<TResult1 = T, TResult2 = never>(onfulfilled?: Nullable<(value: T) => TResult1 | PromiseLike<TResult1>>, onrejected?: Nullable<(reason: any) => TResult2 | PromiseLike<TResult2>>): Promise<TResult1 | TResult2>;
catch<TResult = never>(onrejected?: Nullable<(reason: any) => TResult | PromiseLike<TResult>>): Promise<T | TResult>;
}
There are several types, particularly in lib.dom.d.ts that could also be simplified by leveraging the Nullable type.
Examples
As part of an InterfaceDeclaration:
interface Foo {
prop: Nullable<string>;
}
As a optional parameter to a function that may also accept null:
function validateUserInput (text: Nullable<string>): void {}
Checklist
My suggestion meets these guidelines:
I've made a PR
Search Terms
NullableSuggestion
TypeScript already comes with a
NonNullabletype, defined inlib.es5.d.ts:I propose that its inverse,
Nullable, is added to the built-in type utilities.I'm aware that your general position in terms of built-in type utilities is that you don't extend them unless strictly necessary, but I do think this is an important addition for the following reason:
The built-in type utilities generally offer inverse types. For example,
Partialis the inverse ofRequired. And,Pickis the inverse ofOmit. Because of this, to me it naturally follows thatNonNullableshould have an inverse calledNullable.Use Cases
For the following examples, nullable is defined as
nullorundefined, thereby following TypeScripts definition from theNonNullableutility type.A few examples that comes to mind:
Additionally, it may help with simplifying built-in type definitions:
For example in
lib.es5.d.ts, thecatchandthendeclarations for thePromiseinterface can be simplified:There are several types, particularly in
lib.dom.d.tsthat could also be simplified by leveraging theNullabletype.Examples
As part of an
InterfaceDeclaration:As a optional parameter to a function that may also accept
null:Checklist
My suggestion meets these guidelines: