Skip to content

Suggestion: Add Nullable<T> and Optional<T> to lib.d.ts #19944

Closed
@jaufgang

Description

@jaufgang

There have been a number of issue threads discussing adding nullable types (most notably #7426 and #7488) and the idea of adding them to the core TypeScript Language was ultimately rejected.

However, it was also suggested in that issue that type mappings could be added to lib.d.ts

type Nullable<T> = T | null;
type Optional<T> = T | undefined.

But then the issue was closed and the suggestion seems to have been dropped. (see #7426 (comment) by @mhegazy )

Nullable<T> is even mentioned in https://www.typescriptlang.org/docs/handbook/advanced-types.html alongside Partial<T>, as well as Readonly<T>, Pick<T, K extends keyof T>, and Record<K extends string, T>. It is confusing when reading that document that Nullable<T> isn't in the core library and needs to be added to your project if you would like to use it.

To me it seems like it should be a logical and uncontroversial choice to add these type mappings. They are tiny additions to the library, and they provide a nicer syntax (IMHO) than explicitly using T | null or T | undefined.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DeclinedThe issue was declined as something which matches the TypeScript visionSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions