Skip to content

elaborate check before converting fresh literal type to regular #12595

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

Merged
merged 1 commit into from
Dec 12, 2016
Merged
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
21 changes: 20 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 (<UnionOrIntersectionType>type).types) {
Copy link
Contributor

@mhegazy mhegazy Dec 12, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is not this just maybeTypeOfKind(type, ~TypeFlags.Nullable)?

Copy link
Contributor Author

@vladima vladima Dec 12, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, since AFAIR maybeTypeOfKind(type, ~TypeFlags.Nullable) will return true for union or intersection type that consist of non-nullable type and nullable type and this is not what we want.

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
Expand Down Expand Up @@ -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);
}
}
Expand Down
31 changes: 31 additions & 0 deletions tests/baselines/reference/nestedFreshLiteral.errors.txt
Original file line number Diff line number Diff line change
@@ -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'.
}
19 changes: 19 additions & 0 deletions tests/baselines/reference/nestedFreshLiteral.js
Original file line number Diff line number Diff line change
@@ -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' } }
};
14 changes: 14 additions & 0 deletions tests/cases/compiler/nestedFreshLiteral.ts
Original file line number Diff line number Diff line change
@@ -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' } }
}