From 35e9e165b17af24b00ecc1f397253d371333cb71 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Wed, 27 Jun 2018 23:25:26 -0700 Subject: [PATCH 1/5] Strip `null` and `undefined` from targets when relating from certain atomic types. --- src/compiler/checker.ts | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index eb23835ffc4a9..fce41c663a1f9 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -10631,6 +10631,22 @@ namespace ts { target = getSimplifiedType(target); } + // Try to see if we're relating something like `Foo` -> `Bar | null | undefined`. + // If so, reporting the `null` and `undefined` in the type is hardly useful. + // First, see if we're even relating an atomic type to a union. + // Then see if the target is stripped down to a single non-union type. + // We actually want to remove null and undefined naively here (rather than getNonNullableType), + // since we don't want to end up with a worse error like "`Foo` is not assignable to `NonNullable`" + // when dealing with generics. + if (target.flags & TypeFlags.Union && + source.flags & ((TypeFlags.Primitive | TypeFlags.Object) & ~(TypeFlags.Nullable | TypeFlags.Void)) && + (target as UnionType).types.length <= 3 && maybeTypeOfKind(target, TypeFlags.Nullable)) { + const nullStrippedTarget = extractTypesOfKind(target, ~TypeFlags.Nullable); + if (!(nullStrippedTarget.flags & (TypeFlags.Union | TypeFlags.Never))) { + target = nullStrippedTarget; + } + } + // both types are the same - covers 'they are the same primitive type or both are Any' or the same type parameter cases if (source === target) return Ternary.True; @@ -12223,7 +12239,7 @@ namespace ts { if (deferredGlobalNonNullableTypeAlias !== unknownSymbol) { return getTypeAliasInstantiation(deferredGlobalNonNullableTypeAlias, [type]); } - return getTypeWithFacts(type, TypeFacts.NEUndefinedOrNull); // Type alias unavailable, fall back to non-higherorder behavior + return getTypeWithFacts(type, TypeFacts.NEUndefinedOrNull); // Type alias unavailable, fall back to non-higher-order behavior } function getNonNullableType(type: Type): Type { From 534503a761494dc1ce4923fd6825c53eec2502b6 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Wed, 27 Jun 2018 23:26:11 -0700 Subject: [PATCH 2/5] Accepted baselines. --- ...ckJsdocTypeTagOnObjectProperty2.errors.txt | 4 ++-- ...ExtendsObjectIntersectionErrors.errors.txt | 4 ++-- ...intersectionWithUnionConstraint.errors.txt | 4 ++-- .../reference/mappedTypeErrors.errors.txt | 12 +++++----- .../reference/nestedFreshLiteral.errors.txt | 18 +++++++------- .../reference/objectCreate-errors.errors.txt | 24 +++++++++---------- .../typeFromJSConstructor.errors.txt | 4 ++-- .../typeFromJSInitializer.errors.txt | 4 ++-- 8 files changed, 36 insertions(+), 38 deletions(-) diff --git a/tests/baselines/reference/checkJsdocTypeTagOnObjectProperty2.errors.txt b/tests/baselines/reference/checkJsdocTypeTagOnObjectProperty2.errors.txt index 48559d79cb189..926de030117e5 100644 --- a/tests/baselines/reference/checkJsdocTypeTagOnObjectProperty2.errors.txt +++ b/tests/baselines/reference/checkJsdocTypeTagOnObjectProperty2.errors.txt @@ -1,4 +1,4 @@ -tests/cases/conformance/jsdoc/0.js(5,3): error TS2322: Type 'number' is not assignable to type 'string | undefined'. +tests/cases/conformance/jsdoc/0.js(5,3): error TS2322: Type 'number' is not assignable to type 'string'. tests/cases/conformance/jsdoc/0.js(7,3): error TS2322: Type '(n1: number) => string' is not assignable to type '(arg0: number) => number'. Type 'string' is not assignable to type 'number'. tests/cases/conformance/jsdoc/0.js(11,3): error TS2322: Type '(n1: number) => string' is not assignable to type '(arg0: number) => number'. @@ -16,7 +16,7 @@ tests/cases/conformance/jsdoc/0.js(22,22): error TS2345: Argument of type '"0"' /** @type {string|undefined} */ bar: 42, ~~~~~~~ -!!! error TS2322: Type 'number' is not assignable to type 'string | undefined'. +!!! error TS2322: Type 'number' is not assignable to type 'string'. /** @type {function(number): number} */ method1(n1) { ~~~~~~~ diff --git a/tests/baselines/reference/interfaceExtendsObjectIntersectionErrors.errors.txt b/tests/baselines/reference/interfaceExtendsObjectIntersectionErrors.errors.txt index 87faadeb4a6db..31fe5c67d85d2 100644 --- a/tests/baselines/reference/interfaceExtendsObjectIntersectionErrors.errors.txt +++ b/tests/baselines/reference/interfaceExtendsObjectIntersectionErrors.errors.txt @@ -39,7 +39,7 @@ tests/cases/conformance/interfaces/interfaceDeclarations/interfaceExtendsObjectI tests/cases/conformance/interfaces/interfaceDeclarations/interfaceExtendsObjectIntersectionErrors.ts(35,29): error TS2411: Property 'a' of type '"hello"' is not assignable to string index type 'number'. tests/cases/conformance/interfaces/interfaceDeclarations/interfaceExtendsObjectIntersectionErrors.ts(39,11): error TS2430: Interface 'I20' incorrectly extends interface 'Partial'. Types of property 'a' are incompatible. - Type 'string' is not assignable to type 'number | undefined'. + Type 'string' is not assignable to type 'number'. tests/cases/conformance/interfaces/interfaceDeclarations/interfaceExtendsObjectIntersectionErrors.ts(40,11): error TS2430: Interface 'I21' incorrectly extends interface 'Readonly'. Types of property 'a' are incompatible. Type 'string' is not assignable to type 'number'. @@ -154,7 +154,7 @@ tests/cases/conformance/interfaces/interfaceDeclarations/interfaceExtendsObjectI ~~~ !!! error TS2430: Interface 'I20' incorrectly extends interface 'Partial'. !!! error TS2430: Types of property 'a' are incompatible. -!!! error TS2430: Type 'string' is not assignable to type 'number | undefined'. +!!! error TS2430: Type 'string' is not assignable to type 'number'. interface I21 extends Readonly { a: string } ~~~ !!! error TS2430: Interface 'I21' incorrectly extends interface 'Readonly'. diff --git a/tests/baselines/reference/intersectionWithUnionConstraint.errors.txt b/tests/baselines/reference/intersectionWithUnionConstraint.errors.txt index 9bdfc6f30f9ad..2ff5947af2bd6 100644 --- a/tests/baselines/reference/intersectionWithUnionConstraint.errors.txt +++ b/tests/baselines/reference/intersectionWithUnionConstraint.errors.txt @@ -12,7 +12,7 @@ tests/cases/conformance/types/intersection/intersectionWithUnionConstraint.ts(10 Type 'T & U' is not assignable to type 'number'. tests/cases/conformance/types/intersection/intersectionWithUnionConstraint.ts(11,9): error TS2322: Type 'T & U' is not assignable to type 'number | undefined'. Type 'string | undefined' is not assignable to type 'number | undefined'. - Type 'string' is not assignable to type 'number | undefined'. + Type 'string' is not assignable to type 'number'. Type 'T & U' is not assignable to type 'number'. tests/cases/conformance/types/intersection/intersectionWithUnionConstraint.ts(12,9): error TS2322: Type 'T & U' is not assignable to type 'null | undefined'. Type 'string | undefined' is not assignable to type 'null | undefined'. @@ -50,7 +50,7 @@ tests/cases/conformance/types/intersection/intersectionWithUnionConstraint.ts(12 ~~ !!! error TS2322: Type 'T & U' is not assignable to type 'number | undefined'. !!! error TS2322: Type 'string | undefined' is not assignable to type 'number | undefined'. -!!! error TS2322: Type 'string' is not assignable to type 'number | undefined'. +!!! error TS2322: Type 'string' is not assignable to type 'number'. !!! error TS2322: Type 'T & U' is not assignable to type 'number'. let y6: null | undefined = x; // Error ~~ diff --git a/tests/baselines/reference/mappedTypeErrors.errors.txt b/tests/baselines/reference/mappedTypeErrors.errors.txt index f78c32531a486..fe29215d140ac 100644 --- a/tests/baselines/reference/mappedTypeErrors.errors.txt +++ b/tests/baselines/reference/mappedTypeErrors.errors.txt @@ -39,13 +39,13 @@ tests/cases/conformance/types/mapped/mappedTypeErrors.ts(124,14): error TS2345: Object literal may only specify known properties, and 'c' does not exist in type 'Pick'. tests/cases/conformance/types/mapped/mappedTypeErrors.ts(128,5): error TS2322: Type '{ a: string; }' is not assignable to type 'T2'. Types of property 'a' are incompatible. - Type 'string' is not assignable to type 'number | undefined'. + Type 'string' is not assignable to type 'number'. tests/cases/conformance/types/mapped/mappedTypeErrors.ts(129,5): error TS2322: Type '{ a: string; }' is not assignable to type 'Partial'. Types of property 'a' are incompatible. - Type 'string' is not assignable to type 'number | undefined'. + Type 'string' is not assignable to type 'number'. tests/cases/conformance/types/mapped/mappedTypeErrors.ts(130,5): error TS2322: Type '{ a: string; }' is not assignable to type '{ [x: string]: any; a?: number | undefined; }'. Types of property 'a' are incompatible. - Type 'string' is not assignable to type 'number | undefined'. + Type 'string' is not assignable to type 'number'. tests/cases/conformance/types/mapped/mappedTypeErrors.ts(136,16): error TS2322: Type 'T' is not assignable to type 'string | number | symbol'. Type 'T' is not assignable to type 'symbol'. tests/cases/conformance/types/mapped/mappedTypeErrors.ts(136,21): error TS2536: Type 'P' cannot be used to index type 'T'. @@ -242,17 +242,17 @@ tests/cases/conformance/types/mapped/mappedTypeErrors.ts(136,21): error TS2536: ~~ !!! error TS2322: Type '{ a: string; }' is not assignable to type 'T2'. !!! error TS2322: Types of property 'a' are incompatible. -!!! error TS2322: Type 'string' is not assignable to type 'number | undefined'. +!!! error TS2322: Type 'string' is not assignable to type 'number'. let x2: Partial = { a: 'no' }; // Error ~~ !!! error TS2322: Type '{ a: string; }' is not assignable to type 'Partial'. !!! error TS2322: Types of property 'a' are incompatible. -!!! error TS2322: Type 'string' is not assignable to type 'number | undefined'. +!!! error TS2322: Type 'string' is not assignable to type 'number'. let x3: { [P in keyof T2]: T2[P]} = { a: 'no' }; // Error ~~ !!! error TS2322: Type '{ a: string; }' is not assignable to type '{ [x: string]: any; a?: number | undefined; }'. !!! error TS2322: Types of property 'a' are incompatible. -!!! error TS2322: Type 'string' is not assignable to type 'number | undefined'. +!!! error TS2322: Type 'string' is not assignable to type 'number'. // Repro from #13044 diff --git a/tests/baselines/reference/nestedFreshLiteral.errors.txt b/tests/baselines/reference/nestedFreshLiteral.errors.txt index d68b398449d53..5eea71dba81a9 100644 --- a/tests/baselines/reference/nestedFreshLiteral.errors.txt +++ b/tests/baselines/reference/nestedFreshLiteral.errors.txt @@ -1,10 +1,9 @@ tests/cases/compiler/nestedFreshLiteral.ts(12,21): error TS2322: Type '{ nested: { prop: { colour: string; }; }; }' is not assignable to type 'NestedCSSProps'. Types of property 'nested' are incompatible. - Type '{ prop: { colour: string; }; }' is not assignable to type 'NestedSelector | undefined'. - Type '{ prop: { colour: string; }; }' is not assignable to type 'NestedSelector'. - Types of property 'prop' are incompatible. - Type '{ colour: string; }' is not assignable to type 'CSSProps'. - Object literal may only specify known properties, but 'colour' does not exist in type 'CSSProps'. Did you mean to write 'color'? + Type '{ prop: { colour: string; }; }' is not assignable to type 'NestedSelector'. + Types of property 'prop' are incompatible. + Type '{ colour: string; }' is not assignable to type 'CSSProps'. + Object literal may only specify known properties, but 'colour' does not exist in type 'CSSProps'. Did you mean to write 'color'? ==== tests/cases/compiler/nestedFreshLiteral.ts (1 errors) ==== @@ -23,9 +22,8 @@ tests/cases/compiler/nestedFreshLiteral.ts(12,21): error TS2322: Type '{ nested: ~~~~~~~~~~~~~ !!! error TS2322: Type '{ nested: { prop: { colour: string; }; }; }' is not assignable to type 'NestedCSSProps'. !!! error TS2322: Types of property 'nested' are incompatible. -!!! error TS2322: Type '{ prop: { colour: string; }; }' is not assignable to type 'NestedSelector | undefined'. -!!! error TS2322: Type '{ prop: { colour: string; }; }' is not assignable to type 'NestedSelector'. -!!! error TS2322: Types of property 'prop' are incompatible. -!!! error TS2322: Type '{ colour: string; }' is not assignable to type 'CSSProps'. -!!! error TS2322: Object literal may only specify known properties, but 'colour' does not exist in type 'CSSProps'. Did you mean to write 'color'? +!!! error TS2322: Type '{ prop: { colour: string; }; }' is not assignable to type 'NestedSelector'. +!!! error TS2322: Types of property 'prop' are incompatible. +!!! error TS2322: Type '{ colour: string; }' is not assignable to type 'CSSProps'. +!!! error TS2322: Object literal may only specify known properties, but 'colour' does not exist in type 'CSSProps'. Did you mean to write 'color'? } \ No newline at end of file diff --git a/tests/baselines/reference/objectCreate-errors.errors.txt b/tests/baselines/reference/objectCreate-errors.errors.txt index 12f62cc4d2cb3..200a9bc456063 100644 --- a/tests/baselines/reference/objectCreate-errors.errors.txt +++ b/tests/baselines/reference/objectCreate-errors.errors.txt @@ -1,23 +1,23 @@ -tests/cases/compiler/objectCreate-errors.ts(1,24): error TS2345: Argument of type '1' is not assignable to parameter of type 'object | null'. -tests/cases/compiler/objectCreate-errors.ts(2,24): error TS2345: Argument of type '"string"' is not assignable to parameter of type 'object | null'. -tests/cases/compiler/objectCreate-errors.ts(3,24): error TS2345: Argument of type 'false' is not assignable to parameter of type 'object | null'. +tests/cases/compiler/objectCreate-errors.ts(1,24): error TS2345: Argument of type '1' is not assignable to parameter of type 'object'. +tests/cases/compiler/objectCreate-errors.ts(2,24): error TS2345: Argument of type '"string"' is not assignable to parameter of type 'object'. +tests/cases/compiler/objectCreate-errors.ts(3,24): error TS2345: Argument of type 'false' is not assignable to parameter of type 'object'. tests/cases/compiler/objectCreate-errors.ts(4,24): error TS2345: Argument of type 'undefined' is not assignable to parameter of type 'object | null'. -tests/cases/compiler/objectCreate-errors.ts(7,24): error TS2345: Argument of type '1' is not assignable to parameter of type 'object | null'. -tests/cases/compiler/objectCreate-errors.ts(8,24): error TS2345: Argument of type '"string"' is not assignable to parameter of type 'object | null'. -tests/cases/compiler/objectCreate-errors.ts(9,24): error TS2345: Argument of type 'false' is not assignable to parameter of type 'object | null'. +tests/cases/compiler/objectCreate-errors.ts(7,24): error TS2345: Argument of type '1' is not assignable to parameter of type 'object'. +tests/cases/compiler/objectCreate-errors.ts(8,24): error TS2345: Argument of type '"string"' is not assignable to parameter of type 'object'. +tests/cases/compiler/objectCreate-errors.ts(9,24): error TS2345: Argument of type 'false' is not assignable to parameter of type 'object'. tests/cases/compiler/objectCreate-errors.ts(10,24): error TS2345: Argument of type 'undefined' is not assignable to parameter of type 'object | null'. ==== tests/cases/compiler/objectCreate-errors.ts (8 errors) ==== var e1 = Object.create(1); // Error ~ -!!! error TS2345: Argument of type '1' is not assignable to parameter of type 'object | null'. +!!! error TS2345: Argument of type '1' is not assignable to parameter of type 'object'. var e2 = Object.create("string"); // Error ~~~~~~~~ -!!! error TS2345: Argument of type '"string"' is not assignable to parameter of type 'object | null'. +!!! error TS2345: Argument of type '"string"' is not assignable to parameter of type 'object'. var e3 = Object.create(false); // Error ~~~~~ -!!! error TS2345: Argument of type 'false' is not assignable to parameter of type 'object | null'. +!!! error TS2345: Argument of type 'false' is not assignable to parameter of type 'object'. var e4 = Object.create(undefined); // Error ~~~~~~~~~ !!! error TS2345: Argument of type 'undefined' is not assignable to parameter of type 'object | null'. @@ -25,13 +25,13 @@ tests/cases/compiler/objectCreate-errors.ts(10,24): error TS2345: Argument of ty var e5 = Object.create(1, {}); // Error ~ -!!! error TS2345: Argument of type '1' is not assignable to parameter of type 'object | null'. +!!! error TS2345: Argument of type '1' is not assignable to parameter of type 'object'. var e6 = Object.create("string", {}); // Error ~~~~~~~~ -!!! error TS2345: Argument of type '"string"' is not assignable to parameter of type 'object | null'. +!!! error TS2345: Argument of type '"string"' is not assignable to parameter of type 'object'. var e7 = Object.create(false, {}); // Error ~~~~~ -!!! error TS2345: Argument of type 'false' is not assignable to parameter of type 'object | null'. +!!! error TS2345: Argument of type 'false' is not assignable to parameter of type 'object'. var e8 = Object.create(undefined, {}); // Error ~~~~~~~~~ !!! error TS2345: Argument of type 'undefined' is not assignable to parameter of type 'object | null'. \ No newline at end of file diff --git a/tests/baselines/reference/typeFromJSConstructor.errors.txt b/tests/baselines/reference/typeFromJSConstructor.errors.txt index ffcf5393f516d..09de1bad81201 100644 --- a/tests/baselines/reference/typeFromJSConstructor.errors.txt +++ b/tests/baselines/reference/typeFromJSConstructor.errors.txt @@ -2,7 +2,7 @@ tests/cases/conformance/salsa/a.js(10,5): error TS7008: Member 'twices' implicit tests/cases/conformance/salsa/a.js(14,5): error TS2322: Type '"hi"' is not assignable to type 'number'. tests/cases/conformance/salsa/a.js(21,5): error TS2322: Type 'false' is not assignable to type 'number'. tests/cases/conformance/salsa/a.js(24,5): error TS2322: Type 'null' is not assignable to type 'string | undefined'. -tests/cases/conformance/salsa/a.js(25,5): error TS2322: Type 'false' is not assignable to type 'string | undefined'. +tests/cases/conformance/salsa/a.js(25,5): error TS2322: Type 'false' is not assignable to type 'string'. tests/cases/conformance/salsa/a.js(26,5): error TS2531: Object is possibly 'null'. @@ -41,7 +41,7 @@ tests/cases/conformance/salsa/a.js(26,5): error TS2531: Object is possibly 'null !!! error TS2322: Type 'null' is not assignable to type 'string | undefined'. this.twice = false // error ~~~~~~~~~~ -!!! error TS2322: Type 'false' is not assignable to type 'string | undefined'. +!!! error TS2322: Type 'false' is not assignable to type 'string'. this.twices.push(1) // error: Object is possibly null ~~~~~~~~~~~ !!! error TS2531: Object is possibly 'null'. diff --git a/tests/baselines/reference/typeFromJSInitializer.errors.txt b/tests/baselines/reference/typeFromJSInitializer.errors.txt index 08d66a2a9f388..8e9bb5f3e3df6 100644 --- a/tests/baselines/reference/typeFromJSInitializer.errors.txt +++ b/tests/baselines/reference/typeFromJSInitializer.errors.txt @@ -3,7 +3,7 @@ tests/cases/conformance/salsa/a.js(4,5): error TS7008: Member 'unknowable' impli tests/cases/conformance/salsa/a.js(5,5): error TS7008: Member 'empty' implicitly has an 'any[]' type. tests/cases/conformance/salsa/a.js(25,12): error TS7006: Parameter 'a' implicitly has an 'any' type. tests/cases/conformance/salsa/a.js(25,29): error TS7006: Parameter 'l' implicitly has an 'any[]' type. -tests/cases/conformance/salsa/a.js(37,5): error TS2322: Type '"error"' is not assignable to type 'number | undefined'. +tests/cases/conformance/salsa/a.js(37,5): error TS2322: Type '"error"' is not assignable to type 'number'. ==== tests/cases/conformance/salsa/a.js (6 errors) ==== @@ -55,7 +55,7 @@ tests/cases/conformance/salsa/a.js(37,5): error TS2322: Type '"error"' is not as b = undefined b = 'error' ~ -!!! error TS2322: Type '"error"' is not assignable to type 'number | undefined'. +!!! error TS2322: Type '"error"' is not assignable to type 'number'. // l should be any[] l.push(1) From 12a201c29efb182b53f4512e7a99dc1c623a2367 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Thu, 28 Jun 2018 15:30:10 -0700 Subject: [PATCH 3/5] Added test case. --- .../cases/compiler/elaboratedErrorsOnNullableTargets01.ts | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 tests/cases/compiler/elaboratedErrorsOnNullableTargets01.ts diff --git a/tests/cases/compiler/elaboratedErrorsOnNullableTargets01.ts b/tests/cases/compiler/elaboratedErrorsOnNullableTargets01.ts new file mode 100644 index 0000000000000..22f71c88d5ddb --- /dev/null +++ b/tests/cases/compiler/elaboratedErrorsOnNullableTargets01.ts @@ -0,0 +1,7 @@ + +export declare let x: null | { foo: { bar: string | null } | undefined } | undefined; +export declare let y: { foo: { bar: number | undefined } }; + +x = y; + +y = x; \ No newline at end of file From c853d7c048c89cae5123f748438b85f0d80aae72 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Thu, 28 Jun 2018 15:34:27 -0700 Subject: [PATCH 4/5] Don't elaborate on primitives at all. --- src/compiler/checker.ts | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index fce41c663a1f9..d6bf84fd9a989 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -10633,13 +10633,14 @@ namespace ts { // Try to see if we're relating something like `Foo` -> `Bar | null | undefined`. // If so, reporting the `null` and `undefined` in the type is hardly useful. - // First, see if we're even relating an atomic type to a union. + // First, see if we're even relating an object type to a union. // Then see if the target is stripped down to a single non-union type. - // We actually want to remove null and undefined naively here (rather than getNonNullableType), - // since we don't want to end up with a worse error like "`Foo` is not assignable to `NonNullable`" - // when dealing with generics. - if (target.flags & TypeFlags.Union && - source.flags & ((TypeFlags.Primitive | TypeFlags.Object) & ~(TypeFlags.Nullable | TypeFlags.Void)) && + // Note + // * We actually want to remove null and undefined naively here (rather than using getNonNullableType), + // since we don't want to end up with a worse error like "`Foo` is not assignable to `NonNullable`" + // when dealing with generics. + // * We also don't deal with primitive source types, since we already halt elaboration below. + if (target.flags & TypeFlags.Union && source.flags & TypeFlags.Object && (target as UnionType).types.length <= 3 && maybeTypeOfKind(target, TypeFlags.Nullable)) { const nullStrippedTarget = extractTypesOfKind(target, ~TypeFlags.Nullable); if (!(nullStrippedTarget.flags & (TypeFlags.Union | TypeFlags.Never))) { From 584e923f4239b1b79d2ee1d1c0ad79b4edb847b0 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Thu, 28 Jun 2018 16:00:51 -0700 Subject: [PATCH 5/5] Accepted baselines. --- ...ckJsdocTypeTagOnObjectProperty2.errors.txt | 4 +-- ...oratedErrorsOnNullableTargets01.errors.txt | 31 +++++++++++++++++++ .../elaboratedErrorsOnNullableTargets01.js | 13 ++++++++ ...laboratedErrorsOnNullableTargets01.symbols | 19 ++++++++++++ .../elaboratedErrorsOnNullableTargets01.types | 23 ++++++++++++++ ...ExtendsObjectIntersectionErrors.errors.txt | 4 +-- ...intersectionWithUnionConstraint.errors.txt | 4 +-- .../reference/mappedTypeErrors.errors.txt | 12 +++---- .../reference/objectCreate-errors.errors.txt | 24 +++++++------- .../typeFromJSConstructor.errors.txt | 4 +-- .../typeFromJSInitializer.errors.txt | 4 +-- 11 files changed, 114 insertions(+), 28 deletions(-) create mode 100644 tests/baselines/reference/elaboratedErrorsOnNullableTargets01.errors.txt create mode 100644 tests/baselines/reference/elaboratedErrorsOnNullableTargets01.js create mode 100644 tests/baselines/reference/elaboratedErrorsOnNullableTargets01.symbols create mode 100644 tests/baselines/reference/elaboratedErrorsOnNullableTargets01.types diff --git a/tests/baselines/reference/checkJsdocTypeTagOnObjectProperty2.errors.txt b/tests/baselines/reference/checkJsdocTypeTagOnObjectProperty2.errors.txt index 926de030117e5..48559d79cb189 100644 --- a/tests/baselines/reference/checkJsdocTypeTagOnObjectProperty2.errors.txt +++ b/tests/baselines/reference/checkJsdocTypeTagOnObjectProperty2.errors.txt @@ -1,4 +1,4 @@ -tests/cases/conformance/jsdoc/0.js(5,3): error TS2322: Type 'number' is not assignable to type 'string'. +tests/cases/conformance/jsdoc/0.js(5,3): error TS2322: Type 'number' is not assignable to type 'string | undefined'. tests/cases/conformance/jsdoc/0.js(7,3): error TS2322: Type '(n1: number) => string' is not assignable to type '(arg0: number) => number'. Type 'string' is not assignable to type 'number'. tests/cases/conformance/jsdoc/0.js(11,3): error TS2322: Type '(n1: number) => string' is not assignable to type '(arg0: number) => number'. @@ -16,7 +16,7 @@ tests/cases/conformance/jsdoc/0.js(22,22): error TS2345: Argument of type '"0"' /** @type {string|undefined} */ bar: 42, ~~~~~~~ -!!! error TS2322: Type 'number' is not assignable to type 'string'. +!!! error TS2322: Type 'number' is not assignable to type 'string | undefined'. /** @type {function(number): number} */ method1(n1) { ~~~~~~~ diff --git a/tests/baselines/reference/elaboratedErrorsOnNullableTargets01.errors.txt b/tests/baselines/reference/elaboratedErrorsOnNullableTargets01.errors.txt new file mode 100644 index 0000000000000..e2a3ebbf1db72 --- /dev/null +++ b/tests/baselines/reference/elaboratedErrorsOnNullableTargets01.errors.txt @@ -0,0 +1,31 @@ +tests/cases/compiler/elaboratedErrorsOnNullableTargets01.ts(4,1): error TS2322: Type '{ foo: { bar: number; }; }' is not assignable to type '{ foo: { bar: string; }; }'. + Types of property 'foo' are incompatible. + Type '{ bar: number; }' is not assignable to type '{ bar: string; }'. + Types of property 'bar' are incompatible. + Type 'number' is not assignable to type 'string'. +tests/cases/compiler/elaboratedErrorsOnNullableTargets01.ts(6,1): error TS2322: Type '{ foo: { bar: string; }; }' is not assignable to type '{ foo: { bar: number; }; }'. + Types of property 'foo' are incompatible. + Type '{ bar: string; }' is not assignable to type '{ bar: number; }'. + Types of property 'bar' are incompatible. + Type 'string' is not assignable to type 'number'. + + +==== tests/cases/compiler/elaboratedErrorsOnNullableTargets01.ts (2 errors) ==== + export declare let x: null | { foo: { bar: string | null } | undefined } | undefined; + export declare let y: { foo: { bar: number | undefined } }; + + x = y; + ~ +!!! error TS2322: Type '{ foo: { bar: number; }; }' is not assignable to type '{ foo: { bar: string; }; }'. +!!! error TS2322: Types of property 'foo' are incompatible. +!!! error TS2322: Type '{ bar: number; }' is not assignable to type '{ bar: string; }'. +!!! error TS2322: Types of property 'bar' are incompatible. +!!! error TS2322: Type 'number' is not assignable to type 'string'. + + y = x; + ~ +!!! error TS2322: Type '{ foo: { bar: string; }; }' is not assignable to type '{ foo: { bar: number; }; }'. +!!! error TS2322: Types of property 'foo' are incompatible. +!!! error TS2322: Type '{ bar: string; }' is not assignable to type '{ bar: number; }'. +!!! error TS2322: Types of property 'bar' are incompatible. +!!! error TS2322: Type 'string' is not assignable to type 'number'. \ No newline at end of file diff --git a/tests/baselines/reference/elaboratedErrorsOnNullableTargets01.js b/tests/baselines/reference/elaboratedErrorsOnNullableTargets01.js new file mode 100644 index 0000000000000..5c5f870021870 --- /dev/null +++ b/tests/baselines/reference/elaboratedErrorsOnNullableTargets01.js @@ -0,0 +1,13 @@ +//// [elaboratedErrorsOnNullableTargets01.ts] +export declare let x: null | { foo: { bar: string | null } | undefined } | undefined; +export declare let y: { foo: { bar: number | undefined } }; + +x = y; + +y = x; + +//// [elaboratedErrorsOnNullableTargets01.js] +"use strict"; +exports.__esModule = true; +exports.x = exports.y; +exports.y = exports.x; diff --git a/tests/baselines/reference/elaboratedErrorsOnNullableTargets01.symbols b/tests/baselines/reference/elaboratedErrorsOnNullableTargets01.symbols new file mode 100644 index 0000000000000..4f218b2e28f8e --- /dev/null +++ b/tests/baselines/reference/elaboratedErrorsOnNullableTargets01.symbols @@ -0,0 +1,19 @@ +=== tests/cases/compiler/elaboratedErrorsOnNullableTargets01.ts === +export declare let x: null | { foo: { bar: string | null } | undefined } | undefined; +>x : Symbol(x, Decl(elaboratedErrorsOnNullableTargets01.ts, 0, 18)) +>foo : Symbol(foo, Decl(elaboratedErrorsOnNullableTargets01.ts, 0, 30)) +>bar : Symbol(bar, Decl(elaboratedErrorsOnNullableTargets01.ts, 0, 37)) + +export declare let y: { foo: { bar: number | undefined } }; +>y : Symbol(y, Decl(elaboratedErrorsOnNullableTargets01.ts, 1, 18)) +>foo : Symbol(foo, Decl(elaboratedErrorsOnNullableTargets01.ts, 1, 23)) +>bar : Symbol(bar, Decl(elaboratedErrorsOnNullableTargets01.ts, 1, 30)) + +x = y; +>x : Symbol(x, Decl(elaboratedErrorsOnNullableTargets01.ts, 0, 18)) +>y : Symbol(y, Decl(elaboratedErrorsOnNullableTargets01.ts, 1, 18)) + +y = x; +>y : Symbol(y, Decl(elaboratedErrorsOnNullableTargets01.ts, 1, 18)) +>x : Symbol(x, Decl(elaboratedErrorsOnNullableTargets01.ts, 0, 18)) + diff --git a/tests/baselines/reference/elaboratedErrorsOnNullableTargets01.types b/tests/baselines/reference/elaboratedErrorsOnNullableTargets01.types new file mode 100644 index 0000000000000..e580b90a732ab --- /dev/null +++ b/tests/baselines/reference/elaboratedErrorsOnNullableTargets01.types @@ -0,0 +1,23 @@ +=== tests/cases/compiler/elaboratedErrorsOnNullableTargets01.ts === +export declare let x: null | { foo: { bar: string | null } | undefined } | undefined; +>x : { foo: { bar: string; }; } +>null : null +>foo : { bar: string; } +>bar : string +>null : null + +export declare let y: { foo: { bar: number | undefined } }; +>y : { foo: { bar: number; }; } +>foo : { bar: number; } +>bar : number + +x = y; +>x = y : { foo: { bar: number; }; } +>x : { foo: { bar: string; }; } +>y : { foo: { bar: number; }; } + +y = x; +>y = x : { foo: { bar: string; }; } +>y : { foo: { bar: number; }; } +>x : { foo: { bar: string; }; } + diff --git a/tests/baselines/reference/interfaceExtendsObjectIntersectionErrors.errors.txt b/tests/baselines/reference/interfaceExtendsObjectIntersectionErrors.errors.txt index 31fe5c67d85d2..87faadeb4a6db 100644 --- a/tests/baselines/reference/interfaceExtendsObjectIntersectionErrors.errors.txt +++ b/tests/baselines/reference/interfaceExtendsObjectIntersectionErrors.errors.txt @@ -39,7 +39,7 @@ tests/cases/conformance/interfaces/interfaceDeclarations/interfaceExtendsObjectI tests/cases/conformance/interfaces/interfaceDeclarations/interfaceExtendsObjectIntersectionErrors.ts(35,29): error TS2411: Property 'a' of type '"hello"' is not assignable to string index type 'number'. tests/cases/conformance/interfaces/interfaceDeclarations/interfaceExtendsObjectIntersectionErrors.ts(39,11): error TS2430: Interface 'I20' incorrectly extends interface 'Partial'. Types of property 'a' are incompatible. - Type 'string' is not assignable to type 'number'. + Type 'string' is not assignable to type 'number | undefined'. tests/cases/conformance/interfaces/interfaceDeclarations/interfaceExtendsObjectIntersectionErrors.ts(40,11): error TS2430: Interface 'I21' incorrectly extends interface 'Readonly'. Types of property 'a' are incompatible. Type 'string' is not assignable to type 'number'. @@ -154,7 +154,7 @@ tests/cases/conformance/interfaces/interfaceDeclarations/interfaceExtendsObjectI ~~~ !!! error TS2430: Interface 'I20' incorrectly extends interface 'Partial'. !!! error TS2430: Types of property 'a' are incompatible. -!!! error TS2430: Type 'string' is not assignable to type 'number'. +!!! error TS2430: Type 'string' is not assignable to type 'number | undefined'. interface I21 extends Readonly { a: string } ~~~ !!! error TS2430: Interface 'I21' incorrectly extends interface 'Readonly'. diff --git a/tests/baselines/reference/intersectionWithUnionConstraint.errors.txt b/tests/baselines/reference/intersectionWithUnionConstraint.errors.txt index 2ff5947af2bd6..9bdfc6f30f9ad 100644 --- a/tests/baselines/reference/intersectionWithUnionConstraint.errors.txt +++ b/tests/baselines/reference/intersectionWithUnionConstraint.errors.txt @@ -12,7 +12,7 @@ tests/cases/conformance/types/intersection/intersectionWithUnionConstraint.ts(10 Type 'T & U' is not assignable to type 'number'. tests/cases/conformance/types/intersection/intersectionWithUnionConstraint.ts(11,9): error TS2322: Type 'T & U' is not assignable to type 'number | undefined'. Type 'string | undefined' is not assignable to type 'number | undefined'. - Type 'string' is not assignable to type 'number'. + Type 'string' is not assignable to type 'number | undefined'. Type 'T & U' is not assignable to type 'number'. tests/cases/conformance/types/intersection/intersectionWithUnionConstraint.ts(12,9): error TS2322: Type 'T & U' is not assignable to type 'null | undefined'. Type 'string | undefined' is not assignable to type 'null | undefined'. @@ -50,7 +50,7 @@ tests/cases/conformance/types/intersection/intersectionWithUnionConstraint.ts(12 ~~ !!! error TS2322: Type 'T & U' is not assignable to type 'number | undefined'. !!! error TS2322: Type 'string | undefined' is not assignable to type 'number | undefined'. -!!! error TS2322: Type 'string' is not assignable to type 'number'. +!!! error TS2322: Type 'string' is not assignable to type 'number | undefined'. !!! error TS2322: Type 'T & U' is not assignable to type 'number'. let y6: null | undefined = x; // Error ~~ diff --git a/tests/baselines/reference/mappedTypeErrors.errors.txt b/tests/baselines/reference/mappedTypeErrors.errors.txt index fe29215d140ac..f78c32531a486 100644 --- a/tests/baselines/reference/mappedTypeErrors.errors.txt +++ b/tests/baselines/reference/mappedTypeErrors.errors.txt @@ -39,13 +39,13 @@ tests/cases/conformance/types/mapped/mappedTypeErrors.ts(124,14): error TS2345: Object literal may only specify known properties, and 'c' does not exist in type 'Pick'. tests/cases/conformance/types/mapped/mappedTypeErrors.ts(128,5): error TS2322: Type '{ a: string; }' is not assignable to type 'T2'. Types of property 'a' are incompatible. - Type 'string' is not assignable to type 'number'. + Type 'string' is not assignable to type 'number | undefined'. tests/cases/conformance/types/mapped/mappedTypeErrors.ts(129,5): error TS2322: Type '{ a: string; }' is not assignable to type 'Partial'. Types of property 'a' are incompatible. - Type 'string' is not assignable to type 'number'. + Type 'string' is not assignable to type 'number | undefined'. tests/cases/conformance/types/mapped/mappedTypeErrors.ts(130,5): error TS2322: Type '{ a: string; }' is not assignable to type '{ [x: string]: any; a?: number | undefined; }'. Types of property 'a' are incompatible. - Type 'string' is not assignable to type 'number'. + Type 'string' is not assignable to type 'number | undefined'. tests/cases/conformance/types/mapped/mappedTypeErrors.ts(136,16): error TS2322: Type 'T' is not assignable to type 'string | number | symbol'. Type 'T' is not assignable to type 'symbol'. tests/cases/conformance/types/mapped/mappedTypeErrors.ts(136,21): error TS2536: Type 'P' cannot be used to index type 'T'. @@ -242,17 +242,17 @@ tests/cases/conformance/types/mapped/mappedTypeErrors.ts(136,21): error TS2536: ~~ !!! error TS2322: Type '{ a: string; }' is not assignable to type 'T2'. !!! error TS2322: Types of property 'a' are incompatible. -!!! error TS2322: Type 'string' is not assignable to type 'number'. +!!! error TS2322: Type 'string' is not assignable to type 'number | undefined'. let x2: Partial = { a: 'no' }; // Error ~~ !!! error TS2322: Type '{ a: string; }' is not assignable to type 'Partial'. !!! error TS2322: Types of property 'a' are incompatible. -!!! error TS2322: Type 'string' is not assignable to type 'number'. +!!! error TS2322: Type 'string' is not assignable to type 'number | undefined'. let x3: { [P in keyof T2]: T2[P]} = { a: 'no' }; // Error ~~ !!! error TS2322: Type '{ a: string; }' is not assignable to type '{ [x: string]: any; a?: number | undefined; }'. !!! error TS2322: Types of property 'a' are incompatible. -!!! error TS2322: Type 'string' is not assignable to type 'number'. +!!! error TS2322: Type 'string' is not assignable to type 'number | undefined'. // Repro from #13044 diff --git a/tests/baselines/reference/objectCreate-errors.errors.txt b/tests/baselines/reference/objectCreate-errors.errors.txt index 200a9bc456063..12f62cc4d2cb3 100644 --- a/tests/baselines/reference/objectCreate-errors.errors.txt +++ b/tests/baselines/reference/objectCreate-errors.errors.txt @@ -1,23 +1,23 @@ -tests/cases/compiler/objectCreate-errors.ts(1,24): error TS2345: Argument of type '1' is not assignable to parameter of type 'object'. -tests/cases/compiler/objectCreate-errors.ts(2,24): error TS2345: Argument of type '"string"' is not assignable to parameter of type 'object'. -tests/cases/compiler/objectCreate-errors.ts(3,24): error TS2345: Argument of type 'false' is not assignable to parameter of type 'object'. +tests/cases/compiler/objectCreate-errors.ts(1,24): error TS2345: Argument of type '1' is not assignable to parameter of type 'object | null'. +tests/cases/compiler/objectCreate-errors.ts(2,24): error TS2345: Argument of type '"string"' is not assignable to parameter of type 'object | null'. +tests/cases/compiler/objectCreate-errors.ts(3,24): error TS2345: Argument of type 'false' is not assignable to parameter of type 'object | null'. tests/cases/compiler/objectCreate-errors.ts(4,24): error TS2345: Argument of type 'undefined' is not assignable to parameter of type 'object | null'. -tests/cases/compiler/objectCreate-errors.ts(7,24): error TS2345: Argument of type '1' is not assignable to parameter of type 'object'. -tests/cases/compiler/objectCreate-errors.ts(8,24): error TS2345: Argument of type '"string"' is not assignable to parameter of type 'object'. -tests/cases/compiler/objectCreate-errors.ts(9,24): error TS2345: Argument of type 'false' is not assignable to parameter of type 'object'. +tests/cases/compiler/objectCreate-errors.ts(7,24): error TS2345: Argument of type '1' is not assignable to parameter of type 'object | null'. +tests/cases/compiler/objectCreate-errors.ts(8,24): error TS2345: Argument of type '"string"' is not assignable to parameter of type 'object | null'. +tests/cases/compiler/objectCreate-errors.ts(9,24): error TS2345: Argument of type 'false' is not assignable to parameter of type 'object | null'. tests/cases/compiler/objectCreate-errors.ts(10,24): error TS2345: Argument of type 'undefined' is not assignable to parameter of type 'object | null'. ==== tests/cases/compiler/objectCreate-errors.ts (8 errors) ==== var e1 = Object.create(1); // Error ~ -!!! error TS2345: Argument of type '1' is not assignable to parameter of type 'object'. +!!! error TS2345: Argument of type '1' is not assignable to parameter of type 'object | null'. var e2 = Object.create("string"); // Error ~~~~~~~~ -!!! error TS2345: Argument of type '"string"' is not assignable to parameter of type 'object'. +!!! error TS2345: Argument of type '"string"' is not assignable to parameter of type 'object | null'. var e3 = Object.create(false); // Error ~~~~~ -!!! error TS2345: Argument of type 'false' is not assignable to parameter of type 'object'. +!!! error TS2345: Argument of type 'false' is not assignable to parameter of type 'object | null'. var e4 = Object.create(undefined); // Error ~~~~~~~~~ !!! error TS2345: Argument of type 'undefined' is not assignable to parameter of type 'object | null'. @@ -25,13 +25,13 @@ tests/cases/compiler/objectCreate-errors.ts(10,24): error TS2345: Argument of ty var e5 = Object.create(1, {}); // Error ~ -!!! error TS2345: Argument of type '1' is not assignable to parameter of type 'object'. +!!! error TS2345: Argument of type '1' is not assignable to parameter of type 'object | null'. var e6 = Object.create("string", {}); // Error ~~~~~~~~ -!!! error TS2345: Argument of type '"string"' is not assignable to parameter of type 'object'. +!!! error TS2345: Argument of type '"string"' is not assignable to parameter of type 'object | null'. var e7 = Object.create(false, {}); // Error ~~~~~ -!!! error TS2345: Argument of type 'false' is not assignable to parameter of type 'object'. +!!! error TS2345: Argument of type 'false' is not assignable to parameter of type 'object | null'. var e8 = Object.create(undefined, {}); // Error ~~~~~~~~~ !!! error TS2345: Argument of type 'undefined' is not assignable to parameter of type 'object | null'. \ No newline at end of file diff --git a/tests/baselines/reference/typeFromJSConstructor.errors.txt b/tests/baselines/reference/typeFromJSConstructor.errors.txt index 09de1bad81201..ffcf5393f516d 100644 --- a/tests/baselines/reference/typeFromJSConstructor.errors.txt +++ b/tests/baselines/reference/typeFromJSConstructor.errors.txt @@ -2,7 +2,7 @@ tests/cases/conformance/salsa/a.js(10,5): error TS7008: Member 'twices' implicit tests/cases/conformance/salsa/a.js(14,5): error TS2322: Type '"hi"' is not assignable to type 'number'. tests/cases/conformance/salsa/a.js(21,5): error TS2322: Type 'false' is not assignable to type 'number'. tests/cases/conformance/salsa/a.js(24,5): error TS2322: Type 'null' is not assignable to type 'string | undefined'. -tests/cases/conformance/salsa/a.js(25,5): error TS2322: Type 'false' is not assignable to type 'string'. +tests/cases/conformance/salsa/a.js(25,5): error TS2322: Type 'false' is not assignable to type 'string | undefined'. tests/cases/conformance/salsa/a.js(26,5): error TS2531: Object is possibly 'null'. @@ -41,7 +41,7 @@ tests/cases/conformance/salsa/a.js(26,5): error TS2531: Object is possibly 'null !!! error TS2322: Type 'null' is not assignable to type 'string | undefined'. this.twice = false // error ~~~~~~~~~~ -!!! error TS2322: Type 'false' is not assignable to type 'string'. +!!! error TS2322: Type 'false' is not assignable to type 'string | undefined'. this.twices.push(1) // error: Object is possibly null ~~~~~~~~~~~ !!! error TS2531: Object is possibly 'null'. diff --git a/tests/baselines/reference/typeFromJSInitializer.errors.txt b/tests/baselines/reference/typeFromJSInitializer.errors.txt index 8e9bb5f3e3df6..08d66a2a9f388 100644 --- a/tests/baselines/reference/typeFromJSInitializer.errors.txt +++ b/tests/baselines/reference/typeFromJSInitializer.errors.txt @@ -3,7 +3,7 @@ tests/cases/conformance/salsa/a.js(4,5): error TS7008: Member 'unknowable' impli tests/cases/conformance/salsa/a.js(5,5): error TS7008: Member 'empty' implicitly has an 'any[]' type. tests/cases/conformance/salsa/a.js(25,12): error TS7006: Parameter 'a' implicitly has an 'any' type. tests/cases/conformance/salsa/a.js(25,29): error TS7006: Parameter 'l' implicitly has an 'any[]' type. -tests/cases/conformance/salsa/a.js(37,5): error TS2322: Type '"error"' is not assignable to type 'number'. +tests/cases/conformance/salsa/a.js(37,5): error TS2322: Type '"error"' is not assignable to type 'number | undefined'. ==== tests/cases/conformance/salsa/a.js (6 errors) ==== @@ -55,7 +55,7 @@ tests/cases/conformance/salsa/a.js(37,5): error TS2322: Type '"error"' is not as b = undefined b = 'error' ~ -!!! error TS2322: Type '"error"' is not assignable to type 'number'. +!!! error TS2322: Type '"error"' is not assignable to type 'number | undefined'. // l should be any[] l.push(1)