diff --git a/lib/lib.es5.d.ts b/lib/lib.es5.d.ts index bbfcb7b93e98e..d88bb34d0305a 100644 --- a/lib/lib.es5.d.ts +++ b/lib/lib.es5.d.ts @@ -1508,6 +1508,11 @@ type Omit = Pick>; */ type NonNullable = T extends null | undefined ? never : T; +/** + * Include null and undefined with T + */ +type Nullable = T | null | undefined; + /** * Obtain the parameters of a function type in a tuple */ diff --git a/src/lib/es5.d.ts b/src/lib/es5.d.ts index e111d36d117ad..e577cd0dcafbd 100644 --- a/src/lib/es5.d.ts +++ b/src/lib/es5.d.ts @@ -1488,6 +1488,11 @@ type Omit = Pick>; */ type NonNullable = T extends null | undefined ? never : T; +/** + * Include null and undefined with T + */ +type Nullable = T | null | undefined; + /** * Obtain the parameters of a function type in a tuple */ diff --git a/tests/baselines/reference/mappedTypes6.errors.txt b/tests/baselines/reference/mappedTypes6.errors.txt index 2429ea8558f10..0aff9020043e4 100644 --- a/tests/baselines/reference/mappedTypes6.errors.txt +++ b/tests/baselines/reference/mappedTypes6.errors.txt @@ -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' is not assignable to type 'Required'. tests/cases/conformance/types/mapped/mappedTypes6.ts(47,5): error TS2322: Type 'Partial' is not assignable to type 'T'. 'T' could be instantiated with an arbitrary type which could be unrelated to 'Partial'. -tests/cases/conformance/types/mapped/mappedTypes6.ts(56,5): error TS2322: Type '{}' is not assignable to type 'Denullified'. -tests/cases/conformance/types/mapped/mappedTypes6.ts(57,5): error TS2322: Type '{}' is not assignable to type 'Required'. -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'. +tests/cases/conformance/types/mapped/mappedTypes6.ts(56,5): error TS2322: Type '{}' is not assignable to type 'Required'. +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': 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': 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'. -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' is not assignable to type 'Required'. +tests/cases/conformance/types/mapped/mappedTypes6.ts(86,5): error TS2322: Type 'T' is not assignable to type 'Required'. +tests/cases/conformance/types/mapped/mappedTypes6.ts(87,5): error TS2322: Type 'Partial' is not assignable to type 'Required'. +tests/cases/conformance/types/mapped/mappedTypes6.ts(88,5): error TS2322: Type 'Nullified' is not assignable to type 'T'. + 'T' could be instantiated with an arbitrary type which could be unrelated to 'Nullified'. +tests/cases/conformance/types/mapped/mappedTypes6.ts(91,5): error TS2322: Type 'Partial' is not assignable to type 'T'. + 'T' could be instantiated with an arbitrary type which could be unrelated to 'Partial'. +tests/cases/conformance/types/mapped/mappedTypes6.ts(92,5): error TS2322: Type 'Nullified' is not assignable to type 'Partial'. + Type 'Nullable' 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': 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': 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'. +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 = { [P in keyof T]: T[P] }; type T01 = { [P in keyof T]?: T[P] }; type T02 = { [P in keyof T]+?: T[P] }; @@ -104,7 +114,6 @@ tests/cases/conformance/types/mapped/mappedTypes6.ts(120,4): error TS2540: Canno z = z; } - function f3(w: Denullified, x: Required, y: T, z: Partial) { w = {}; // Error ~ @@ -135,6 +144,43 @@ tests/cases/conformance/types/mapped/mappedTypes6.ts(120,4): error TS2540: Canno z = z; } + type Nullified = { [P in keyof T]?: Nullable }; + + function f11(w: Nullified, x: Required, y: T, z: Partial) { + w = w; + w = x; + w = y; + w = z; + x = w; // Error + ~ +!!! error TS2322: Type 'Nullified' is not assignable to type 'Required'. + x = x; + x = y; // Error + ~ +!!! error TS2322: Type 'T' is not assignable to type 'Required'. + x = z; // Error + ~ +!!! error TS2322: Type 'Partial' is not assignable to type 'Required'. + y = w; // Error + ~ +!!! error TS2322: Type 'Nullified' is not assignable to type 'T'. +!!! error TS2322: 'T' could be instantiated with an arbitrary type which could be unrelated to 'Nullified'. + y = x; + y = y; + y = z; // Error + ~ +!!! error TS2322: Type 'Partial' is not assignable to type 'T'. +!!! error TS2322: 'T' could be instantiated with an arbitrary type which could be unrelated to 'Partial'. + z = w; + ~ +!!! error TS2322: Type 'Nullified' is not assignable to type 'Partial'. +!!! error TS2322: Type 'Nullable' 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; @@ -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 }; @@ -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'. -!!! 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 = { diff --git a/tests/baselines/reference/mappedTypes6.js b/tests/baselines/reference/mappedTypes6.js index b24d210eb75be..39fbf8ab8fff2 100644 --- a/tests/baselines/reference/mappedTypes6.js +++ b/tests/baselines/reference/mappedTypes6.js @@ -52,7 +52,6 @@ function f2(w: Denullified, x: Required, y: T, z: Partial) { z = z; } - function f3(w: Denullified, x: Required, y: T, z: Partial) { w = {}; // Error x = {}; // Error @@ -76,6 +75,27 @@ function f10(x: Readonly, y: T, z: Readwrite) { z = z; } +type Nullified = { [P in keyof T]?: Nullable }; + +function f11(w: Nullified, x: Required, y: T, z: Partial) { + 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; @@ -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 @@ -256,6 +294,10 @@ declare type Readwrite = { -readonly [P in keyof T]: T[P]; }; declare function f10(x: Readonly, y: T, z: Readwrite): void; +declare type Nullified = { + [P in keyof T]?: Nullable; +}; +declare function f11(w: Nullified, x: Required, y: T, z: Partial): void; declare type Foo = { a: number; b: number | undefined; diff --git a/tests/baselines/reference/mappedTypes6.symbols b/tests/baselines/reference/mappedTypes6.symbols index 4e03fe359b7bb..968c968af01dc 100644 --- a/tests/baselines/reference/mappedTypes6.symbols +++ b/tests/baselines/reference/mappedTypes6.symbols @@ -265,255 +265,343 @@ function f2(w: Denullified, x: Required, y: T, z: Partial) { >z : Symbol(z, Decl(mappedTypes6.ts, 34, 55)) } - function f3(w: Denullified, x: Required, y: T, z: Partial) { >f3 : Symbol(f3, Decl(mappedTypes6.ts, 51, 1)) ->T : Symbol(T, Decl(mappedTypes6.ts, 54, 12)) ->w : Symbol(w, Decl(mappedTypes6.ts, 54, 15)) +>T : Symbol(T, Decl(mappedTypes6.ts, 53, 12)) +>w : Symbol(w, Decl(mappedTypes6.ts, 53, 15)) >Denullified : Symbol(Denullified, Decl(mappedTypes6.ts, 30, 1)) ->T : Symbol(T, Decl(mappedTypes6.ts, 54, 12)) ->x : Symbol(x, Decl(mappedTypes6.ts, 54, 33)) +>T : Symbol(T, Decl(mappedTypes6.ts, 53, 12)) +>x : Symbol(x, Decl(mappedTypes6.ts, 53, 33)) >Required : Symbol(Required, Decl(lib.es5.d.ts, --, --)) ->T : Symbol(T, Decl(mappedTypes6.ts, 54, 12)) ->y : Symbol(y, Decl(mappedTypes6.ts, 54, 49)) ->T : Symbol(T, Decl(mappedTypes6.ts, 54, 12)) ->z : Symbol(z, Decl(mappedTypes6.ts, 54, 55)) +>T : Symbol(T, Decl(mappedTypes6.ts, 53, 12)) +>y : Symbol(y, Decl(mappedTypes6.ts, 53, 49)) +>T : Symbol(T, Decl(mappedTypes6.ts, 53, 12)) +>z : Symbol(z, Decl(mappedTypes6.ts, 53, 55)) >Partial : Symbol(Partial, Decl(lib.es5.d.ts, --, --)) ->T : Symbol(T, Decl(mappedTypes6.ts, 54, 12)) +>T : Symbol(T, Decl(mappedTypes6.ts, 53, 12)) w = {}; // Error ->w : Symbol(w, Decl(mappedTypes6.ts, 54, 15)) +>w : Symbol(w, Decl(mappedTypes6.ts, 53, 15)) x = {}; // Error ->x : Symbol(x, Decl(mappedTypes6.ts, 54, 33)) +>x : Symbol(x, Decl(mappedTypes6.ts, 53, 33)) y = {}; // Error ->y : Symbol(y, Decl(mappedTypes6.ts, 54, 49)) +>y : Symbol(y, Decl(mappedTypes6.ts, 53, 49)) z = {}; ->z : Symbol(z, Decl(mappedTypes6.ts, 54, 55)) +>z : Symbol(z, Decl(mappedTypes6.ts, 53, 55)) } type Readwrite = { ->Readwrite : Symbol(Readwrite, Decl(mappedTypes6.ts, 59, 1)) ->T : Symbol(T, Decl(mappedTypes6.ts, 61, 15)) +>Readwrite : Symbol(Readwrite, Decl(mappedTypes6.ts, 58, 1)) +>T : Symbol(T, Decl(mappedTypes6.ts, 60, 15)) -readonly [P in keyof T]: T[P]; ->P : Symbol(P, Decl(mappedTypes6.ts, 62, 15)) ->T : Symbol(T, Decl(mappedTypes6.ts, 61, 15)) ->T : Symbol(T, Decl(mappedTypes6.ts, 61, 15)) ->P : Symbol(P, Decl(mappedTypes6.ts, 62, 15)) +>P : Symbol(P, Decl(mappedTypes6.ts, 61, 15)) +>T : Symbol(T, Decl(mappedTypes6.ts, 60, 15)) +>T : Symbol(T, Decl(mappedTypes6.ts, 60, 15)) +>P : Symbol(P, Decl(mappedTypes6.ts, 61, 15)) } function f10(x: Readonly, y: T, z: Readwrite) { ->f10 : Symbol(f10, Decl(mappedTypes6.ts, 63, 1)) ->T : Symbol(T, Decl(mappedTypes6.ts, 65, 13)) ->x : Symbol(x, Decl(mappedTypes6.ts, 65, 16)) +>f10 : Symbol(f10, Decl(mappedTypes6.ts, 62, 1)) +>T : Symbol(T, Decl(mappedTypes6.ts, 64, 13)) +>x : Symbol(x, Decl(mappedTypes6.ts, 64, 16)) >Readonly : Symbol(Readonly, Decl(lib.es5.d.ts, --, --)) ->T : Symbol(T, Decl(mappedTypes6.ts, 65, 13)) ->y : Symbol(y, Decl(mappedTypes6.ts, 65, 31)) ->T : Symbol(T, Decl(mappedTypes6.ts, 65, 13)) ->z : Symbol(z, Decl(mappedTypes6.ts, 65, 37)) ->Readwrite : Symbol(Readwrite, Decl(mappedTypes6.ts, 59, 1)) ->T : Symbol(T, Decl(mappedTypes6.ts, 65, 13)) +>T : Symbol(T, Decl(mappedTypes6.ts, 64, 13)) +>y : Symbol(y, Decl(mappedTypes6.ts, 64, 31)) +>T : Symbol(T, Decl(mappedTypes6.ts, 64, 13)) +>z : Symbol(z, Decl(mappedTypes6.ts, 64, 37)) +>Readwrite : Symbol(Readwrite, Decl(mappedTypes6.ts, 58, 1)) +>T : Symbol(T, Decl(mappedTypes6.ts, 64, 13)) x = x; ->x : Symbol(x, Decl(mappedTypes6.ts, 65, 16)) ->x : Symbol(x, Decl(mappedTypes6.ts, 65, 16)) +>x : Symbol(x, Decl(mappedTypes6.ts, 64, 16)) +>x : Symbol(x, Decl(mappedTypes6.ts, 64, 16)) x = y; ->x : Symbol(x, Decl(mappedTypes6.ts, 65, 16)) ->y : Symbol(y, Decl(mappedTypes6.ts, 65, 31)) +>x : Symbol(x, Decl(mappedTypes6.ts, 64, 16)) +>y : Symbol(y, Decl(mappedTypes6.ts, 64, 31)) x = z; ->x : Symbol(x, Decl(mappedTypes6.ts, 65, 16)) ->z : Symbol(z, Decl(mappedTypes6.ts, 65, 37)) +>x : Symbol(x, Decl(mappedTypes6.ts, 64, 16)) +>z : Symbol(z, Decl(mappedTypes6.ts, 64, 37)) y = x; ->y : Symbol(y, Decl(mappedTypes6.ts, 65, 31)) ->x : Symbol(x, Decl(mappedTypes6.ts, 65, 16)) +>y : Symbol(y, Decl(mappedTypes6.ts, 64, 31)) +>x : Symbol(x, Decl(mappedTypes6.ts, 64, 16)) y = y; ->y : Symbol(y, Decl(mappedTypes6.ts, 65, 31)) ->y : Symbol(y, Decl(mappedTypes6.ts, 65, 31)) +>y : Symbol(y, Decl(mappedTypes6.ts, 64, 31)) +>y : Symbol(y, Decl(mappedTypes6.ts, 64, 31)) y = z; ->y : Symbol(y, Decl(mappedTypes6.ts, 65, 31)) ->z : Symbol(z, Decl(mappedTypes6.ts, 65, 37)) +>y : Symbol(y, Decl(mappedTypes6.ts, 64, 31)) +>z : Symbol(z, Decl(mappedTypes6.ts, 64, 37)) + + z = x; +>z : Symbol(z, Decl(mappedTypes6.ts, 64, 37)) +>x : Symbol(x, Decl(mappedTypes6.ts, 64, 16)) + + z = y; +>z : Symbol(z, Decl(mappedTypes6.ts, 64, 37)) +>y : Symbol(y, Decl(mappedTypes6.ts, 64, 31)) + + z = z; +>z : Symbol(z, Decl(mappedTypes6.ts, 64, 37)) +>z : Symbol(z, Decl(mappedTypes6.ts, 64, 37)) +} + +type Nullified = { [P in keyof T]?: Nullable }; +>Nullified : Symbol(Nullified, Decl(mappedTypes6.ts, 74, 1)) +>T : Symbol(T, Decl(mappedTypes6.ts, 76, 15)) +>P : Symbol(P, Decl(mappedTypes6.ts, 76, 23)) +>T : Symbol(T, Decl(mappedTypes6.ts, 76, 15)) +>Nullable : Symbol(Nullable, Decl(lib.es5.d.ts, --, --)) +>T : Symbol(T, Decl(mappedTypes6.ts, 76, 15)) +>P : Symbol(P, Decl(mappedTypes6.ts, 76, 23)) + +function f11(w: Nullified, x: Required, y: T, z: Partial) { +>f11 : Symbol(f11, Decl(mappedTypes6.ts, 76, 56)) +>T : Symbol(T, Decl(mappedTypes6.ts, 78, 13)) +>w : Symbol(w, Decl(mappedTypes6.ts, 78, 16)) +>Nullified : Symbol(Nullified, Decl(mappedTypes6.ts, 74, 1)) +>T : Symbol(T, Decl(mappedTypes6.ts, 78, 13)) +>x : Symbol(x, Decl(mappedTypes6.ts, 78, 32)) +>Required : Symbol(Required, Decl(lib.es5.d.ts, --, --)) +>T : Symbol(T, Decl(mappedTypes6.ts, 78, 13)) +>y : Symbol(y, Decl(mappedTypes6.ts, 78, 48)) +>T : Symbol(T, Decl(mappedTypes6.ts, 78, 13)) +>z : Symbol(z, Decl(mappedTypes6.ts, 78, 54)) +>Partial : Symbol(Partial, Decl(lib.es5.d.ts, --, --)) +>T : Symbol(T, Decl(mappedTypes6.ts, 78, 13)) + + w = w; +>w : Symbol(w, Decl(mappedTypes6.ts, 78, 16)) +>w : Symbol(w, Decl(mappedTypes6.ts, 78, 16)) + + w = x; +>w : Symbol(w, Decl(mappedTypes6.ts, 78, 16)) +>x : Symbol(x, Decl(mappedTypes6.ts, 78, 32)) + + w = y; +>w : Symbol(w, Decl(mappedTypes6.ts, 78, 16)) +>y : Symbol(y, Decl(mappedTypes6.ts, 78, 48)) + + w = z; +>w : Symbol(w, Decl(mappedTypes6.ts, 78, 16)) +>z : Symbol(z, Decl(mappedTypes6.ts, 78, 54)) + + x = w; // Error +>x : Symbol(x, Decl(mappedTypes6.ts, 78, 32)) +>w : Symbol(w, Decl(mappedTypes6.ts, 78, 16)) + + x = x; +>x : Symbol(x, Decl(mappedTypes6.ts, 78, 32)) +>x : Symbol(x, Decl(mappedTypes6.ts, 78, 32)) + + x = y; // Error +>x : Symbol(x, Decl(mappedTypes6.ts, 78, 32)) +>y : Symbol(y, Decl(mappedTypes6.ts, 78, 48)) + + x = z; // Error +>x : Symbol(x, Decl(mappedTypes6.ts, 78, 32)) +>z : Symbol(z, Decl(mappedTypes6.ts, 78, 54)) + + y = w; // Error +>y : Symbol(y, Decl(mappedTypes6.ts, 78, 48)) +>w : Symbol(w, Decl(mappedTypes6.ts, 78, 16)) + + y = x; +>y : Symbol(y, Decl(mappedTypes6.ts, 78, 48)) +>x : Symbol(x, Decl(mappedTypes6.ts, 78, 32)) + + y = y; +>y : Symbol(y, Decl(mappedTypes6.ts, 78, 48)) +>y : Symbol(y, Decl(mappedTypes6.ts, 78, 48)) + + y = z; // Error +>y : Symbol(y, Decl(mappedTypes6.ts, 78, 48)) +>z : Symbol(z, Decl(mappedTypes6.ts, 78, 54)) + + z = w; +>z : Symbol(z, Decl(mappedTypes6.ts, 78, 54)) +>w : Symbol(w, Decl(mappedTypes6.ts, 78, 16)) z = x; ->z : Symbol(z, Decl(mappedTypes6.ts, 65, 37)) ->x : Symbol(x, Decl(mappedTypes6.ts, 65, 16)) +>z : Symbol(z, Decl(mappedTypes6.ts, 78, 54)) +>x : Symbol(x, Decl(mappedTypes6.ts, 78, 32)) z = y; ->z : Symbol(z, Decl(mappedTypes6.ts, 65, 37)) ->y : Symbol(y, Decl(mappedTypes6.ts, 65, 31)) +>z : Symbol(z, Decl(mappedTypes6.ts, 78, 54)) +>y : Symbol(y, Decl(mappedTypes6.ts, 78, 48)) z = z; ->z : Symbol(z, Decl(mappedTypes6.ts, 65, 37)) ->z : Symbol(z, Decl(mappedTypes6.ts, 65, 37)) +>z : Symbol(z, Decl(mappedTypes6.ts, 78, 54)) +>z : Symbol(z, Decl(mappedTypes6.ts, 78, 54)) } type Foo = { ->Foo : Symbol(Foo, Decl(mappedTypes6.ts, 75, 1)) +>Foo : Symbol(Foo, Decl(mappedTypes6.ts, 95, 1)) a: number; ->a : Symbol(a, Decl(mappedTypes6.ts, 77, 12)) +>a : Symbol(a, Decl(mappedTypes6.ts, 97, 12)) b: number | undefined; ->b : Symbol(b, Decl(mappedTypes6.ts, 78, 14)) +>b : Symbol(b, Decl(mappedTypes6.ts, 98, 14)) c?: number; ->c : Symbol(c, Decl(mappedTypes6.ts, 79, 26)) +>c : Symbol(c, Decl(mappedTypes6.ts, 99, 26)) d?: number | undefined; ->d : Symbol(d, Decl(mappedTypes6.ts, 80, 15)) +>d : Symbol(d, Decl(mappedTypes6.ts, 100, 15)) } declare let x1: Foo; ->x1 : Symbol(x1, Decl(mappedTypes6.ts, 84, 11)) ->Foo : Symbol(Foo, Decl(mappedTypes6.ts, 75, 1)) +>x1 : Symbol(x1, Decl(mappedTypes6.ts, 104, 11)) +>Foo : Symbol(Foo, Decl(mappedTypes6.ts, 95, 1)) x1.a; // number ->x1.a : Symbol(a, Decl(mappedTypes6.ts, 77, 12)) ->x1 : Symbol(x1, Decl(mappedTypes6.ts, 84, 11)) ->a : Symbol(a, Decl(mappedTypes6.ts, 77, 12)) +>x1.a : Symbol(a, Decl(mappedTypes6.ts, 97, 12)) +>x1 : Symbol(x1, Decl(mappedTypes6.ts, 104, 11)) +>a : Symbol(a, Decl(mappedTypes6.ts, 97, 12)) x1.b; // number | undefined ->x1.b : Symbol(b, Decl(mappedTypes6.ts, 78, 14)) ->x1 : Symbol(x1, Decl(mappedTypes6.ts, 84, 11)) ->b : Symbol(b, Decl(mappedTypes6.ts, 78, 14)) +>x1.b : Symbol(b, Decl(mappedTypes6.ts, 98, 14)) +>x1 : Symbol(x1, Decl(mappedTypes6.ts, 104, 11)) +>b : Symbol(b, Decl(mappedTypes6.ts, 98, 14)) x1.c; // number | undefined ->x1.c : Symbol(c, Decl(mappedTypes6.ts, 79, 26)) ->x1 : Symbol(x1, Decl(mappedTypes6.ts, 84, 11)) ->c : Symbol(c, Decl(mappedTypes6.ts, 79, 26)) +>x1.c : Symbol(c, Decl(mappedTypes6.ts, 99, 26)) +>x1 : Symbol(x1, Decl(mappedTypes6.ts, 104, 11)) +>c : Symbol(c, Decl(mappedTypes6.ts, 99, 26)) x1.d; // number | undefined ->x1.d : Symbol(d, Decl(mappedTypes6.ts, 80, 15)) ->x1 : Symbol(x1, Decl(mappedTypes6.ts, 84, 11)) ->d : Symbol(d, Decl(mappedTypes6.ts, 80, 15)) +>x1.d : Symbol(d, Decl(mappedTypes6.ts, 100, 15)) +>x1 : Symbol(x1, Decl(mappedTypes6.ts, 104, 11)) +>d : Symbol(d, Decl(mappedTypes6.ts, 100, 15)) x1 = { a: 1 }; // Error ->x1 : Symbol(x1, Decl(mappedTypes6.ts, 84, 11)) ->a : Symbol(a, Decl(mappedTypes6.ts, 91, 6)) +>x1 : Symbol(x1, Decl(mappedTypes6.ts, 104, 11)) +>a : Symbol(a, Decl(mappedTypes6.ts, 111, 6)) x1 = { a: 1, b: 1 }; ->x1 : Symbol(x1, Decl(mappedTypes6.ts, 84, 11)) ->a : Symbol(a, Decl(mappedTypes6.ts, 92, 6)) ->b : Symbol(b, Decl(mappedTypes6.ts, 92, 12)) +>x1 : Symbol(x1, Decl(mappedTypes6.ts, 104, 11)) +>a : Symbol(a, Decl(mappedTypes6.ts, 112, 6)) +>b : Symbol(b, Decl(mappedTypes6.ts, 112, 12)) x1 = { a: 1, b: 1, c: 1 }; ->x1 : Symbol(x1, Decl(mappedTypes6.ts, 84, 11)) ->a : Symbol(a, Decl(mappedTypes6.ts, 93, 6)) ->b : Symbol(b, Decl(mappedTypes6.ts, 93, 12)) ->c : Symbol(c, Decl(mappedTypes6.ts, 93, 18)) +>x1 : Symbol(x1, Decl(mappedTypes6.ts, 104, 11)) +>a : Symbol(a, Decl(mappedTypes6.ts, 113, 6)) +>b : Symbol(b, Decl(mappedTypes6.ts, 113, 12)) +>c : Symbol(c, Decl(mappedTypes6.ts, 113, 18)) x1 = { a: 1, b: 1, c: 1, d: 1 }; ->x1 : Symbol(x1, Decl(mappedTypes6.ts, 84, 11)) ->a : Symbol(a, Decl(mappedTypes6.ts, 94, 6)) ->b : Symbol(b, Decl(mappedTypes6.ts, 94, 12)) ->c : Symbol(c, Decl(mappedTypes6.ts, 94, 18)) ->d : Symbol(d, Decl(mappedTypes6.ts, 94, 24)) +>x1 : Symbol(x1, Decl(mappedTypes6.ts, 104, 11)) +>a : Symbol(a, Decl(mappedTypes6.ts, 114, 6)) +>b : Symbol(b, Decl(mappedTypes6.ts, 114, 12)) +>c : Symbol(c, Decl(mappedTypes6.ts, 114, 18)) +>d : Symbol(d, Decl(mappedTypes6.ts, 114, 24)) declare let x2: Required; ->x2 : Symbol(x2, Decl(mappedTypes6.ts, 96, 11)) +>x2 : Symbol(x2, Decl(mappedTypes6.ts, 116, 11)) >Required : Symbol(Required, Decl(lib.es5.d.ts, --, --)) ->Foo : Symbol(Foo, Decl(mappedTypes6.ts, 75, 1)) +>Foo : Symbol(Foo, Decl(mappedTypes6.ts, 95, 1)) x1.a; // number ->x1.a : Symbol(a, Decl(mappedTypes6.ts, 77, 12)) ->x1 : Symbol(x1, Decl(mappedTypes6.ts, 84, 11)) ->a : Symbol(a, Decl(mappedTypes6.ts, 77, 12)) +>x1.a : Symbol(a, Decl(mappedTypes6.ts, 97, 12)) +>x1 : Symbol(x1, Decl(mappedTypes6.ts, 104, 11)) +>a : Symbol(a, Decl(mappedTypes6.ts, 97, 12)) x1.b; // number | undefined ->x1.b : Symbol(b, Decl(mappedTypes6.ts, 78, 14)) ->x1 : Symbol(x1, Decl(mappedTypes6.ts, 84, 11)) ->b : Symbol(b, Decl(mappedTypes6.ts, 78, 14)) +>x1.b : Symbol(b, Decl(mappedTypes6.ts, 98, 14)) +>x1 : Symbol(x1, Decl(mappedTypes6.ts, 104, 11)) +>b : Symbol(b, Decl(mappedTypes6.ts, 98, 14)) x1.c; // number ->x1.c : Symbol(c, Decl(mappedTypes6.ts, 79, 26)) ->x1 : Symbol(x1, Decl(mappedTypes6.ts, 84, 11)) ->c : Symbol(c, Decl(mappedTypes6.ts, 79, 26)) +>x1.c : Symbol(c, Decl(mappedTypes6.ts, 99, 26)) +>x1 : Symbol(x1, Decl(mappedTypes6.ts, 104, 11)) +>c : Symbol(c, Decl(mappedTypes6.ts, 99, 26)) x1.d; // number ->x1.d : Symbol(d, Decl(mappedTypes6.ts, 80, 15)) ->x1 : Symbol(x1, Decl(mappedTypes6.ts, 84, 11)) ->d : Symbol(d, Decl(mappedTypes6.ts, 80, 15)) +>x1.d : Symbol(d, Decl(mappedTypes6.ts, 100, 15)) +>x1 : Symbol(x1, Decl(mappedTypes6.ts, 104, 11)) +>d : Symbol(d, Decl(mappedTypes6.ts, 100, 15)) x2 = { a: 1 }; // Error ->x2 : Symbol(x2, Decl(mappedTypes6.ts, 96, 11)) ->a : Symbol(a, Decl(mappedTypes6.ts, 103, 6)) +>x2 : Symbol(x2, Decl(mappedTypes6.ts, 116, 11)) +>a : Symbol(a, Decl(mappedTypes6.ts, 123, 6)) x2 = { a: 1, b: 1 }; // Error ->x2 : Symbol(x2, Decl(mappedTypes6.ts, 96, 11)) ->a : Symbol(a, Decl(mappedTypes6.ts, 104, 6)) ->b : Symbol(b, Decl(mappedTypes6.ts, 104, 12)) +>x2 : Symbol(x2, Decl(mappedTypes6.ts, 116, 11)) +>a : Symbol(a, Decl(mappedTypes6.ts, 124, 6)) +>b : Symbol(b, Decl(mappedTypes6.ts, 124, 12)) x2 = { a: 1, b: 1, c: 1 }; // Error ->x2 : Symbol(x2, Decl(mappedTypes6.ts, 96, 11)) ->a : Symbol(a, Decl(mappedTypes6.ts, 105, 6)) ->b : Symbol(b, Decl(mappedTypes6.ts, 105, 12)) ->c : Symbol(c, Decl(mappedTypes6.ts, 105, 18)) +>x2 : Symbol(x2, Decl(mappedTypes6.ts, 116, 11)) +>a : Symbol(a, Decl(mappedTypes6.ts, 125, 6)) +>b : Symbol(b, Decl(mappedTypes6.ts, 125, 12)) +>c : Symbol(c, Decl(mappedTypes6.ts, 125, 18)) x2 = { a: 1, b: 1, c: 1, d: 1 }; ->x2 : Symbol(x2, Decl(mappedTypes6.ts, 96, 11)) ->a : Symbol(a, Decl(mappedTypes6.ts, 106, 6)) ->b : Symbol(b, Decl(mappedTypes6.ts, 106, 12)) ->c : Symbol(c, Decl(mappedTypes6.ts, 106, 18)) ->d : Symbol(d, Decl(mappedTypes6.ts, 106, 24)) +>x2 : Symbol(x2, Decl(mappedTypes6.ts, 116, 11)) +>a : Symbol(a, Decl(mappedTypes6.ts, 126, 6)) +>b : Symbol(b, Decl(mappedTypes6.ts, 126, 12)) +>c : Symbol(c, Decl(mappedTypes6.ts, 126, 18)) +>d : Symbol(d, Decl(mappedTypes6.ts, 126, 24)) type Bar = { ->Bar : Symbol(Bar, Decl(mappedTypes6.ts, 106, 32)) +>Bar : Symbol(Bar, Decl(mappedTypes6.ts, 126, 32)) a: number; ->a : Symbol(a, Decl(mappedTypes6.ts, 108, 12)) +>a : Symbol(a, Decl(mappedTypes6.ts, 128, 12)) readonly b: number; ->b : Symbol(b, Decl(mappedTypes6.ts, 109, 14)) +>b : Symbol(b, Decl(mappedTypes6.ts, 129, 14)) } declare let x3: Bar; ->x3 : Symbol(x3, Decl(mappedTypes6.ts, 113, 11)) ->Bar : Symbol(Bar, Decl(mappedTypes6.ts, 106, 32)) +>x3 : Symbol(x3, Decl(mappedTypes6.ts, 133, 11)) +>Bar : Symbol(Bar, Decl(mappedTypes6.ts, 126, 32)) x3.a = 1; ->x3.a : Symbol(a, Decl(mappedTypes6.ts, 108, 12)) ->x3 : Symbol(x3, Decl(mappedTypes6.ts, 113, 11)) ->a : Symbol(a, Decl(mappedTypes6.ts, 108, 12)) +>x3.a : Symbol(a, Decl(mappedTypes6.ts, 128, 12)) +>x3 : Symbol(x3, Decl(mappedTypes6.ts, 133, 11)) +>a : Symbol(a, Decl(mappedTypes6.ts, 128, 12)) x3.b = 1; // Error ->x3.b : Symbol(b, Decl(mappedTypes6.ts, 109, 14)) ->x3 : Symbol(x3, Decl(mappedTypes6.ts, 113, 11)) ->b : Symbol(b, Decl(mappedTypes6.ts, 109, 14)) +>x3.b : Symbol(b, Decl(mappedTypes6.ts, 129, 14)) +>x3 : Symbol(x3, Decl(mappedTypes6.ts, 133, 11)) +>b : Symbol(b, Decl(mappedTypes6.ts, 129, 14)) declare let x4: Readonly; ->x4 : Symbol(x4, Decl(mappedTypes6.ts, 117, 11)) +>x4 : Symbol(x4, Decl(mappedTypes6.ts, 137, 11)) >Readonly : Symbol(Readonly, Decl(lib.es5.d.ts, --, --)) ->Bar : Symbol(Bar, Decl(mappedTypes6.ts, 106, 32)) +>Bar : Symbol(Bar, Decl(mappedTypes6.ts, 126, 32)) x4.a = 1; // Error ->x4.a : Symbol(a, Decl(mappedTypes6.ts, 108, 12)) ->x4 : Symbol(x4, Decl(mappedTypes6.ts, 117, 11)) ->a : Symbol(a, Decl(mappedTypes6.ts, 108, 12)) +>x4.a : Symbol(a, Decl(mappedTypes6.ts, 128, 12)) +>x4 : Symbol(x4, Decl(mappedTypes6.ts, 137, 11)) +>a : Symbol(a, Decl(mappedTypes6.ts, 128, 12)) x4.b = 1; // Error ->x4.b : Symbol(b, Decl(mappedTypes6.ts, 109, 14)) ->x4 : Symbol(x4, Decl(mappedTypes6.ts, 117, 11)) ->b : Symbol(b, Decl(mappedTypes6.ts, 109, 14)) +>x4.b : Symbol(b, Decl(mappedTypes6.ts, 129, 14)) +>x4 : Symbol(x4, Decl(mappedTypes6.ts, 137, 11)) +>b : Symbol(b, Decl(mappedTypes6.ts, 129, 14)) declare let x5: Readwrite; ->x5 : Symbol(x5, Decl(mappedTypes6.ts, 121, 11)) ->Readwrite : Symbol(Readwrite, Decl(mappedTypes6.ts, 59, 1)) ->Bar : Symbol(Bar, Decl(mappedTypes6.ts, 106, 32)) +>x5 : Symbol(x5, Decl(mappedTypes6.ts, 141, 11)) +>Readwrite : Symbol(Readwrite, Decl(mappedTypes6.ts, 58, 1)) +>Bar : Symbol(Bar, Decl(mappedTypes6.ts, 126, 32)) x5.a = 1; ->x5.a : Symbol(a, Decl(mappedTypes6.ts, 108, 12)) ->x5 : Symbol(x5, Decl(mappedTypes6.ts, 121, 11)) ->a : Symbol(a, Decl(mappedTypes6.ts, 108, 12)) +>x5.a : Symbol(a, Decl(mappedTypes6.ts, 128, 12)) +>x5 : Symbol(x5, Decl(mappedTypes6.ts, 141, 11)) +>a : Symbol(a, Decl(mappedTypes6.ts, 128, 12)) x5.b = 1; ->x5.b : Symbol(b, Decl(mappedTypes6.ts, 109, 14)) ->x5 : Symbol(x5, Decl(mappedTypes6.ts, 121, 11)) ->b : Symbol(b, Decl(mappedTypes6.ts, 109, 14)) +>x5.b : Symbol(b, Decl(mappedTypes6.ts, 129, 14)) +>x5 : Symbol(x5, Decl(mappedTypes6.ts, 141, 11)) +>b : Symbol(b, Decl(mappedTypes6.ts, 129, 14)) diff --git a/tests/baselines/reference/mappedTypes6.types b/tests/baselines/reference/mappedTypes6.types index 9c8d5e8a3d7fa..e91f80a137381 100644 --- a/tests/baselines/reference/mappedTypes6.types +++ b/tests/baselines/reference/mappedTypes6.types @@ -190,7 +190,6 @@ function f2(w: Denullified, x: Required, y: T, z: Partial) { >z : Partial } - function f3(w: Denullified, x: Required, y: T, z: Partial) { >f3 : (w: Denullified, x: Required, y: T, z: Partial) => void >w : Denullified @@ -277,6 +276,97 @@ function f10(x: Readonly, y: T, z: Readwrite) { >z : Readwrite } +type Nullified = { [P in keyof T]?: Nullable }; +>Nullified : Nullified + +function f11(w: Nullified, x: Required, y: T, z: Partial) { +>f11 : (w: Nullified, x: Required, y: T, z: Partial) => void +>w : Nullified +>x : Required +>y : T +>z : Partial + + w = w; +>w = w : Nullified +>w : Nullified +>w : Nullified + + w = x; +>w = x : Required +>w : Nullified +>x : Required + + w = y; +>w = y : T +>w : Nullified +>y : T + + w = z; +>w = z : Partial +>w : Nullified +>z : Partial + + x = w; // Error +>x = w : Nullified +>x : Required +>w : Nullified + + x = x; +>x = x : Required +>x : Required +>x : Required + + x = y; // Error +>x = y : T +>x : Required +>y : T + + x = z; // Error +>x = z : Partial +>x : Required +>z : Partial + + y = w; // Error +>y = w : Nullified +>y : T +>w : Nullified + + y = x; +>y = x : Required +>y : T +>x : Required + + y = y; +>y = y : T +>y : T +>y : T + + y = z; // Error +>y = z : Partial +>y : T +>z : Partial + + z = w; +>z = w : Nullified +>z : Partial +>w : Nullified + + z = x; +>z = x : Required +>z : Partial +>x : Required + + z = y; +>z = y : T +>z : Partial +>y : T + + z = z; +>z = z : Partial +>z : Partial +>z : Partial +} + type Foo = { >Foo : Foo diff --git a/tests/cases/conformance/types/mapped/mappedTypes6.ts b/tests/cases/conformance/types/mapped/mappedTypes6.ts index b3beef768466f..5655aee8f9f4f 100644 --- a/tests/cases/conformance/types/mapped/mappedTypes6.ts +++ b/tests/cases/conformance/types/mapped/mappedTypes6.ts @@ -54,7 +54,6 @@ function f2(w: Denullified, x: Required, y: T, z: Partial) { z = z; } - function f3(w: Denullified, x: Required, y: T, z: Partial) { w = {}; // Error x = {}; // Error @@ -78,6 +77,27 @@ function f10(x: Readonly, y: T, z: Readwrite) { z = z; } +type Nullified = { [P in keyof T]?: Nullable }; + +function f11(w: Nullified, x: Required, y: T, z: Partial) { + 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;