From f72b85de61b34c4ec2079bde8d9446942bdcc82c Mon Sep 17 00:00:00 2001 From: Vladimir Matveev Date: Wed, 30 Nov 2016 16:42:15 -0800 Subject: [PATCH] elaborate check before converting fresh literal type to regular --- src/compiler/checker.ts | 21 ++++++++++++- .../reference/nestedFreshLiteral.errors.txt | 31 +++++++++++++++++++ .../baselines/reference/nestedFreshLiteral.js | 19 ++++++++++++ tests/cases/compiler/nestedFreshLiteral.ts | 14 +++++++++ 4 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 tests/baselines/reference/nestedFreshLiteral.errors.txt create mode 100644 tests/baselines/reference/nestedFreshLiteral.js create mode 100644 tests/cases/compiler/nestedFreshLiteral.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 3842532257243..cdde24bcc5295 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -7156,6 +7156,25 @@ namespace ts { } } + function isUnionOrIntersectionTypeWithoutNullableConstituents(type: Type): boolean { + if (!(type.flags & TypeFlags.UnionOrIntersection)) { + return false; + } + // at this point we know that this is union or intersection type possibly with nullable constituents. + // check if we still will have compound type if we ignore nullable components. + let seenNonNullable = false; + for (const t of (type).types) { + if (t.flags & TypeFlags.Nullable) { + continue; + } + if (seenNonNullable) { + return true; + } + seenNonNullable = true; + } + return false; + } + // Compare two types and return // Ternary.True if they are related with no assumptions, // Ternary.Maybe if they are related with assumptions of other relationships, or @@ -7188,7 +7207,7 @@ namespace ts { // and intersection types are further deconstructed on the target side, we don't want to // make the check again (as it might fail for a partial target type). Therefore we obtain // the regular source type and proceed with that. - if (target.flags & TypeFlags.UnionOrIntersection) { + if (isUnionOrIntersectionTypeWithoutNullableConstituents(target)) { source = getRegularTypeOfObjectLiteral(source); } } diff --git a/tests/baselines/reference/nestedFreshLiteral.errors.txt b/tests/baselines/reference/nestedFreshLiteral.errors.txt new file mode 100644 index 0000000000000..6aff94ac0a6a4 --- /dev/null +++ b/tests/baselines/reference/nestedFreshLiteral.errors.txt @@ -0,0 +1,31 @@ +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, and 'colour' does not exist in type 'CSSProps'. + + +==== tests/cases/compiler/nestedFreshLiteral.ts (1 errors) ==== + interface CSSProps { + color?: string + } + interface NestedCSSProps { + nested?: NestedSelector + } + interface NestedSelector { + prop: CSSProps; + } + + let stylen: NestedCSSProps = { + nested: { prop: { colour: 'red' } } + ~~~~~~~~~~~~~ +!!! 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, and 'colour' does not exist in type 'CSSProps'. + } \ No newline at end of file diff --git a/tests/baselines/reference/nestedFreshLiteral.js b/tests/baselines/reference/nestedFreshLiteral.js new file mode 100644 index 0000000000000..36d4743deae41 --- /dev/null +++ b/tests/baselines/reference/nestedFreshLiteral.js @@ -0,0 +1,19 @@ +//// [nestedFreshLiteral.ts] +interface CSSProps { + color?: string +} +interface NestedCSSProps { + nested?: NestedSelector +} +interface NestedSelector { + prop: CSSProps; +} + +let stylen: NestedCSSProps = { + nested: { prop: { colour: 'red' } } +} + +//// [nestedFreshLiteral.js] +var stylen = { + nested: { prop: { colour: 'red' } } +}; diff --git a/tests/cases/compiler/nestedFreshLiteral.ts b/tests/cases/compiler/nestedFreshLiteral.ts new file mode 100644 index 0000000000000..bf3bf5df8d32b --- /dev/null +++ b/tests/cases/compiler/nestedFreshLiteral.ts @@ -0,0 +1,14 @@ +// @strictNullChecks: true +interface CSSProps { + color?: string +} +interface NestedCSSProps { + nested?: NestedSelector +} +interface NestedSelector { + prop: CSSProps; +} + +let stylen: NestedCSSProps = { + nested: { prop: { colour: 'red' } } +} \ No newline at end of file