Skip to content

feat: add 'Nullable' type #39523

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/lib.es5.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1508,6 +1508,11 @@ type Omit<T, K extends keyof any> = Pick<T, Exclude<keyof T, K>>;
*/
type NonNullable<T> = T extends null | undefined ? never : T;

/**
* Include null and undefined with T
*/
type Nullable<T> = T | null | undefined;

/**
* Obtain the parameters of a function type in a tuple
*/
Expand Down
5 changes: 5 additions & 0 deletions src/lib/es5.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1488,6 +1488,11 @@ type Omit<T, K extends keyof any> = Pick<T, Exclude<keyof T, K>>;
*/
type NonNullable<T> = T extends null | undefined ? never : T;

/**
* Include null and undefined with T
*/
type Nullable<T> = T | null | undefined;

/**
* Obtain the parameters of a function type in a tuple
*/
Expand Down
74 changes: 60 additions & 14 deletions tests/baselines/reference/mappedTypes6.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,30 @@ tests/cases/conformance/types/mapped/mappedTypes6.ts(42,5): error TS2322: Type '
tests/cases/conformance/types/mapped/mappedTypes6.ts(43,5): error TS2322: Type 'Partial<T>' is not assignable to type 'Required<T>'.
tests/cases/conformance/types/mapped/mappedTypes6.ts(47,5): error TS2322: Type 'Partial<T>' is not assignable to type 'T'.
'T' could be instantiated with an arbitrary type which could be unrelated to 'Partial<T>'.
tests/cases/conformance/types/mapped/mappedTypes6.ts(56,5): error TS2322: Type '{}' is not assignable to type 'Denullified<T>'.
tests/cases/conformance/types/mapped/mappedTypes6.ts(57,5): error TS2322: Type '{}' is not assignable to type 'Required<T>'.
tests/cases/conformance/types/mapped/mappedTypes6.ts(58,5): error TS2322: Type '{}' is not assignable to type 'T'.
tests/cases/conformance/types/mapped/mappedTypes6.ts(55,5): error TS2322: Type '{}' is not assignable to type 'Denullified<T>'.
tests/cases/conformance/types/mapped/mappedTypes6.ts(56,5): error TS2322: Type '{}' is not assignable to type 'Required<T>'.
tests/cases/conformance/types/mapped/mappedTypes6.ts(57,5): error TS2322: Type '{}' is not assignable to type 'T'.
'T' could be instantiated with an arbitrary type which could be unrelated to '{}'.
tests/cases/conformance/types/mapped/mappedTypes6.ts(92,1): error TS2741: Property 'b' is missing in type '{ a: number; }' but required in type 'Foo'.
tests/cases/conformance/types/mapped/mappedTypes6.ts(104,1): error TS2739: Type '{ a: number; }' is missing the following properties from type 'Required<Foo>': b, c, d
tests/cases/conformance/types/mapped/mappedTypes6.ts(105,1): error TS2739: Type '{ a: number; b: number; }' is missing the following properties from type 'Required<Foo>': c, d
tests/cases/conformance/types/mapped/mappedTypes6.ts(106,1): error TS2741: Property 'd' is missing in type '{ a: number; b: number; c: number; }' but required in type 'Required<Foo>'.
tests/cases/conformance/types/mapped/mappedTypes6.ts(116,4): error TS2540: Cannot assign to 'b' because it is a read-only property.
tests/cases/conformance/types/mapped/mappedTypes6.ts(119,4): error TS2540: Cannot assign to 'a' because it is a read-only property.
tests/cases/conformance/types/mapped/mappedTypes6.ts(120,4): error TS2540: Cannot assign to 'b' because it is a read-only property.
tests/cases/conformance/types/mapped/mappedTypes6.ts(84,5): error TS2322: Type 'Nullified<T>' is not assignable to type 'Required<T>'.
tests/cases/conformance/types/mapped/mappedTypes6.ts(86,5): error TS2322: Type 'T' is not assignable to type 'Required<T>'.
tests/cases/conformance/types/mapped/mappedTypes6.ts(87,5): error TS2322: Type 'Partial<T>' is not assignable to type 'Required<T>'.
tests/cases/conformance/types/mapped/mappedTypes6.ts(88,5): error TS2322: Type 'Nullified<T>' is not assignable to type 'T'.
'T' could be instantiated with an arbitrary type which could be unrelated to 'Nullified<T>'.
tests/cases/conformance/types/mapped/mappedTypes6.ts(91,5): error TS2322: Type 'Partial<T>' is not assignable to type 'T'.
'T' could be instantiated with an arbitrary type which could be unrelated to 'Partial<T>'.
tests/cases/conformance/types/mapped/mappedTypes6.ts(92,5): error TS2322: Type 'Nullified<T>' is not assignable to type 'Partial<T>'.
Type 'Nullable<T[P]>' is not assignable to type 'T[P] | undefined'.
Type 'null' is not assignable to type 'T[P] | undefined'.
tests/cases/conformance/types/mapped/mappedTypes6.ts(112,1): error TS2741: Property 'b' is missing in type '{ a: number; }' but required in type 'Foo'.
tests/cases/conformance/types/mapped/mappedTypes6.ts(124,1): error TS2739: Type '{ a: number; }' is missing the following properties from type 'Required<Foo>': b, c, d
tests/cases/conformance/types/mapped/mappedTypes6.ts(125,1): error TS2739: Type '{ a: number; b: number; }' is missing the following properties from type 'Required<Foo>': c, d
tests/cases/conformance/types/mapped/mappedTypes6.ts(126,1): error TS2741: Property 'd' is missing in type '{ a: number; b: number; c: number; }' but required in type 'Required<Foo>'.
tests/cases/conformance/types/mapped/mappedTypes6.ts(136,4): error TS2540: Cannot assign to 'b' because it is a read-only property.
tests/cases/conformance/types/mapped/mappedTypes6.ts(139,4): error TS2540: Cannot assign to 'a' because it is a read-only property.
tests/cases/conformance/types/mapped/mappedTypes6.ts(140,4): error TS2540: Cannot assign to 'b' because it is a read-only property.


==== tests/cases/conformance/types/mapped/mappedTypes6.ts (19 errors) ====
==== tests/cases/conformance/types/mapped/mappedTypes6.ts (25 errors) ====
type T00<T> = { [P in keyof T]: T[P] };
type T01<T> = { [P in keyof T]?: T[P] };
type T02<T> = { [P in keyof T]+?: T[P] };
Expand Down Expand Up @@ -104,7 +114,6 @@ tests/cases/conformance/types/mapped/mappedTypes6.ts(120,4): error TS2540: Canno
z = z;
}


function f3<T>(w: Denullified<T>, x: Required<T>, y: T, z: Partial<T>) {
w = {}; // Error
~
Expand Down Expand Up @@ -135,6 +144,43 @@ tests/cases/conformance/types/mapped/mappedTypes6.ts(120,4): error TS2540: Canno
z = z;
}

type Nullified<T> = { [P in keyof T]?: Nullable<T[P]> };

function f11<T>(w: Nullified<T>, x: Required<T>, y: T, z: Partial<T>) {
w = w;
w = x;
w = y;
w = z;
x = w; // Error
~
!!! error TS2322: Type 'Nullified<T>' is not assignable to type 'Required<T>'.
x = x;
x = y; // Error
~
!!! error TS2322: Type 'T' is not assignable to type 'Required<T>'.
x = z; // Error
~
!!! error TS2322: Type 'Partial<T>' is not assignable to type 'Required<T>'.
y = w; // Error
~
!!! error TS2322: Type 'Nullified<T>' is not assignable to type 'T'.
!!! error TS2322: 'T' could be instantiated with an arbitrary type which could be unrelated to 'Nullified<T>'.
y = x;
y = y;
y = z; // Error
~
!!! error TS2322: Type 'Partial<T>' is not assignable to type 'T'.
!!! error TS2322: 'T' could be instantiated with an arbitrary type which could be unrelated to 'Partial<T>'.
z = w;
~
!!! error TS2322: Type 'Nullified<T>' is not assignable to type 'Partial<T>'.
!!! error TS2322: Type 'Nullable<T[P]>' is not assignable to type 'T[P] | undefined'.
!!! error TS2322: Type 'null' is not assignable to type 'T[P] | undefined'.
z = x;
z = y;
z = z;
}

type Foo = {
a: number;
b: number | undefined;
Expand All @@ -152,7 +198,7 @@ tests/cases/conformance/types/mapped/mappedTypes6.ts(120,4): error TS2540: Canno
x1 = { a: 1 }; // Error
~~
!!! error TS2741: Property 'b' is missing in type '{ a: number; }' but required in type 'Foo'.
!!! related TS2728 tests/cases/conformance/types/mapped/mappedTypes6.ts:80:5: 'b' is declared here.
!!! related TS2728 tests/cases/conformance/types/mapped/mappedTypes6.ts:100:5: 'b' is declared here.
x1 = { a: 1, b: 1 };
x1 = { a: 1, b: 1, c: 1 };
x1 = { a: 1, b: 1, c: 1, d: 1 };
Expand All @@ -173,7 +219,7 @@ tests/cases/conformance/types/mapped/mappedTypes6.ts(120,4): error TS2540: Canno
x2 = { a: 1, b: 1, c: 1 }; // Error
~~
!!! error TS2741: Property 'd' is missing in type '{ a: number; b: number; c: number; }' but required in type 'Required<Foo>'.
!!! related TS2728 tests/cases/conformance/types/mapped/mappedTypes6.ts:82:5: 'd' is declared here.
!!! related TS2728 tests/cases/conformance/types/mapped/mappedTypes6.ts:102:5: 'd' is declared here.
x2 = { a: 1, b: 1, c: 1, d: 1 };

type Bar = {
Expand Down
44 changes: 43 additions & 1 deletion tests/baselines/reference/mappedTypes6.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ function f2<T>(w: Denullified<T>, x: Required<T>, y: T, z: Partial<T>) {
z = z;
}


function f3<T>(w: Denullified<T>, x: Required<T>, y: T, z: Partial<T>) {
w = {}; // Error
x = {}; // Error
Expand All @@ -76,6 +75,27 @@ function f10<T>(x: Readonly<T>, y: T, z: Readwrite<T>) {
z = z;
}

type Nullified<T> = { [P in keyof T]?: Nullable<T[P]> };

function f11<T>(w: Nullified<T>, x: Required<T>, y: T, z: Partial<T>) {
w = w;
w = x;
w = y;
w = z;
x = w; // Error
x = x;
x = y; // Error
x = z; // Error
y = w; // Error
y = x;
y = y;
y = z; // Error
z = w;
z = x;
z = y;
z = z;
}

type Foo = {
a: number;
b: number | undefined;
Expand Down Expand Up @@ -173,6 +193,24 @@ function f10(x, y, z) {
z = y;
z = z;
}
function f11(w, x, y, z) {
w = w;
w = x;
w = y;
w = z;
x = w; // Error
x = x;
x = y; // Error
x = z; // Error
y = w; // Error
y = x;
y = y;
y = z; // Error
z = w;
z = x;
z = y;
z = z;
}
x1.a; // number
x1.b; // number | undefined
x1.c; // number | undefined
Expand Down Expand Up @@ -256,6 +294,10 @@ declare type Readwrite<T> = {
-readonly [P in keyof T]: T[P];
};
declare function f10<T>(x: Readonly<T>, y: T, z: Readwrite<T>): void;
declare type Nullified<T> = {
[P in keyof T]?: Nullable<T[P]>;
};
declare function f11<T>(w: Nullified<T>, x: Required<T>, y: T, z: Partial<T>): void;
declare type Foo = {
a: number;
b: number | undefined;
Expand Down
Loading