diff --git a/tests/baselines/reference/controlFlowGenericTypes.errors.txt b/tests/baselines/reference/controlFlowGenericTypes.errors.txt index 27101aaa81c..27141a5898c 100644 --- a/tests/baselines/reference/controlFlowGenericTypes.errors.txt +++ b/tests/baselines/reference/controlFlowGenericTypes.errors.txt @@ -5,12 +5,10 @@ controlFlowGenericTypes.ts(81,11): error TS2339: Property 'foo' does not exist o controlFlowGenericTypes.ts(90,44): error TS2355: A function whose declared type is neither 'undefined', 'void', nor 'any' must return a value. controlFlowGenericTypes.ts(91,11): error TS2339: Property 'foo' does not exist on type 'MyUnion'. Property 'foo' does not exist on type 'AA'. -controlFlowGenericTypes.ts(156,16): error TS18048: 'obj' is possibly 'undefined'. -controlFlowGenericTypes.ts(167,9): error TS18048: 'iSpec' is possibly 'undefined'. -controlFlowGenericTypes.ts(168,9): error TS18048: 'iSpec' is possibly 'undefined'. +controlFlowGenericTypes.ts(168,9): error TS2536: Type 'keyof PublicSpec' cannot be used to index type 'InternalSpec'. -==== controlFlowGenericTypes.ts (8 errors) ==== +==== controlFlowGenericTypes.ts (6 errors) ==== function f1(x: T, y: { a: T }, z: [T]): string { if (x) { x; @@ -178,9 +176,7 @@ controlFlowGenericTypes.ts(168,9): error TS18048: 'iSpec' is possibly 'undefined } function fx3 | undefined, K extends keyof T>(obj: T, key: K) { - const x1 = obj[key]; // Error - ~~~ -!!! error TS18048: 'obj' is possibly 'undefined'. + const x1 = obj[key]; const x2 = obj && obj[key]; } @@ -191,12 +187,10 @@ controlFlowGenericTypes.ts(168,9): error TS18048: 'iSpec' is possibly 'undefined InternalSpec extends Record | undefined = undefined> { m() { let iSpec = null! as InternalSpec; - iSpec[null! as keyof InternalSpec]; // Error, object possibly undefined - ~~~~~ -!!! error TS18048: 'iSpec' is possibly 'undefined'. - iSpec[null! as keyof PublicSpec]; // Error, object possibly undefined - ~~~~~ -!!! error TS18048: 'iSpec' is possibly 'undefined'. + iSpec[null! as keyof InternalSpec]; + iSpec[null! as keyof PublicSpec]; // Error + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2536: Type 'keyof PublicSpec' cannot be used to index type 'InternalSpec'. if (iSpec === undefined) { return; } diff --git a/tests/baselines/reference/controlFlowGenericTypes.js b/tests/baselines/reference/controlFlowGenericTypes.js index d22dd7421ed..60af0a54ebd 100644 --- a/tests/baselines/reference/controlFlowGenericTypes.js +++ b/tests/baselines/reference/controlFlowGenericTypes.js @@ -156,7 +156,7 @@ function fx2, K extends keyof T>(obj: T, key: } function fx3 | undefined, K extends keyof T>(obj: T, key: K) { - const x1 = obj[key]; // Error + const x1 = obj[key]; const x2 = obj && obj[key]; } @@ -167,8 +167,8 @@ class TableBaseEnum< InternalSpec extends Record | undefined = undefined> { m() { let iSpec = null! as InternalSpec; - iSpec[null! as keyof InternalSpec]; // Error, object possibly undefined - iSpec[null! as keyof PublicSpec]; // Error, object possibly undefined + iSpec[null! as keyof InternalSpec]; + iSpec[null! as keyof PublicSpec]; // Error if (iSpec === undefined) { return; } @@ -339,7 +339,7 @@ function fx2(obj, key) { var x2 = obj && obj[key]; } function fx3(obj, key) { - var x1 = obj[key]; // Error + var x1 = obj[key]; var x2 = obj && obj[key]; } // Repro from #44166 @@ -348,8 +348,8 @@ var TableBaseEnum = /** @class */ (function () { } TableBaseEnum.prototype.m = function () { var iSpec = null; - iSpec[null]; // Error, object possibly undefined - iSpec[null]; // Error, object possibly undefined + iSpec[null]; + iSpec[null]; // Error if (iSpec === undefined) { return; } diff --git a/tests/baselines/reference/controlFlowGenericTypes.symbols b/tests/baselines/reference/controlFlowGenericTypes.symbols index a96581cb4ab..4c8edd62076 100644 --- a/tests/baselines/reference/controlFlowGenericTypes.symbols +++ b/tests/baselines/reference/controlFlowGenericTypes.symbols @@ -469,7 +469,7 @@ function fx3 | undefined, K extends keyof T>(o >key : Symbol(key, Decl(controlFlowGenericTypes.ts, 154, 86)) >K : Symbol(K, Decl(controlFlowGenericTypes.ts, 154, 59)) - const x1 = obj[key]; // Error + const x1 = obj[key]; >x1 : Symbol(x1, Decl(controlFlowGenericTypes.ts, 155, 9)) >obj : Symbol(obj, Decl(controlFlowGenericTypes.ts, 154, 79)) >key : Symbol(key, Decl(controlFlowGenericTypes.ts, 154, 86)) @@ -503,11 +503,11 @@ class TableBaseEnum< >iSpec : Symbol(iSpec, Decl(controlFlowGenericTypes.ts, 165, 11)) >InternalSpec : Symbol(InternalSpec, Decl(controlFlowGenericTypes.ts, 162, 55)) - iSpec[null! as keyof InternalSpec]; // Error, object possibly undefined + iSpec[null! as keyof InternalSpec]; >iSpec : Symbol(iSpec, Decl(controlFlowGenericTypes.ts, 165, 11)) >InternalSpec : Symbol(InternalSpec, Decl(controlFlowGenericTypes.ts, 162, 55)) - iSpec[null! as keyof PublicSpec]; // Error, object possibly undefined + iSpec[null! as keyof PublicSpec]; // Error >iSpec : Symbol(iSpec, Decl(controlFlowGenericTypes.ts, 165, 11)) >PublicSpec : Symbol(PublicSpec, Decl(controlFlowGenericTypes.ts, 161, 20)) diff --git a/tests/baselines/reference/controlFlowGenericTypes.types b/tests/baselines/reference/controlFlowGenericTypes.types index 34aaf807091..ea257ef2c9e 100644 --- a/tests/baselines/reference/controlFlowGenericTypes.types +++ b/tests/baselines/reference/controlFlowGenericTypes.types @@ -661,27 +661,27 @@ function fx3 | undefined, K extends keyof T>(o >key : K > : ^ - const x1 = obj[key]; // Error ->x1 : Record[K] -> : ^^^^^^^^^^^^^^^^^^^^^^^^^^ ->obj[key] : Record[K] -> : ^^^^^^^^^^^^^^^^^^^^^^^^^^ ->obj : Record | undefined -> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + const x1 = obj[key]; +>x1 : T[K] +> : ^^^^ +>obj[key] : T[K] +> : ^^^^ +>obj : T +> : ^ >key : K > : ^ const x2 = obj && obj[key]; ->x2 : Record[K] -> : ^^^^^^^^^^^^^^^^^^^^^^^^^^ ->obj && obj[key] : Record[K] -> : ^^^^^^^^^^^^^^^^^^^^^^^^^^ +>x2 : NonNullable[K] +> : ^^^^^^^^^^^^^^^^^ +>obj && obj[key] : NonNullable[K] +> : ^^^^^^^^^^^^^^^^^ >obj : T > : ^ ->obj[key] : Record[K] -> : ^^^^^^^^^^^^^^^^^^^^^^^^^^ ->obj : Record -> : ^^^^^^^^^^^^^^^^^^^^^^^ +>obj[key] : NonNullable[K] +> : ^^^^^^^^^^^^^^^^^ +>obj : NonNullable +> : ^^^^^^^^^^^^^^ >key : K > : ^ } @@ -706,21 +706,21 @@ class TableBaseEnum< >null! : never > : ^^^^^ - iSpec[null! as keyof InternalSpec]; // Error, object possibly undefined ->iSpec[null! as keyof InternalSpec] : Record[keyof InternalSpec] -> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ->iSpec : Record | undefined -> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + iSpec[null! as keyof InternalSpec]; +>iSpec[null! as keyof InternalSpec] : InternalSpec[keyof InternalSpec] +> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +>iSpec : InternalSpec +> : ^^^^^^^^^^^^ >null! as keyof InternalSpec : keyof InternalSpec > : ^^^^^^^^^^^^^^^^^^ >null! : never > : ^^^^^ - iSpec[null! as keyof PublicSpec]; // Error, object possibly undefined ->iSpec[null! as keyof PublicSpec] : Record[keyof PublicSpec] -> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ->iSpec : Record | undefined -> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + iSpec[null! as keyof PublicSpec]; // Error +>iSpec[null! as keyof PublicSpec] : any +> : ^^^ +>iSpec : InternalSpec +> : ^^^^^^^^^^^^ >null! as keyof PublicSpec : keyof PublicSpec > : ^^^^^^^^^^^^^^^^ >null! : never @@ -737,20 +737,20 @@ class TableBaseEnum< return; } iSpec[null! as keyof InternalSpec]; ->iSpec[null! as keyof InternalSpec] : Record[keyof InternalSpec] -> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ->iSpec : Record -> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +>iSpec[null! as keyof InternalSpec] : (InternalSpec & {})[keyof InternalSpec] +> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +>iSpec : InternalSpec & {} +> : ^^^^^^^^^^^^^^^^^ >null! as keyof InternalSpec : keyof InternalSpec > : ^^^^^^^^^^^^^^^^^^ >null! : never > : ^^^^^ iSpec[null! as keyof PublicSpec]; ->iSpec[null! as keyof PublicSpec] : Record[keyof PublicSpec] -> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ->iSpec : Record -> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +>iSpec[null! as keyof PublicSpec] : (InternalSpec & {})[keyof PublicSpec] +> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +>iSpec : InternalSpec & {} +> : ^^^^^^^^^^^^^^^^^ >null! as keyof PublicSpec : keyof PublicSpec > : ^^^^^^^^^^^^^^^^ >null! : never