Skip to content
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

Avoid elaborating object literals against array-likes #25752

Merged
merged 4 commits into from
Jul 18, 2018
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
8 changes: 7 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11182,7 +11182,8 @@ namespace ts {
if (reportErrors) {
const bestMatchingType =
findMatchingDiscriminantType(source, target) ||
findMatchingTypeReferenceOrTypeAliasReference(source, target);
findMatchingTypeReferenceOrTypeAliasReference(source, target) ||
findBestTypeForObjectLiteral(source, target);

isRelatedTo(source, bestMatchingType || targetTypes[targetTypes.length - 1], /*reportErrors*/ true);
}
Expand All @@ -11207,6 +11208,11 @@ namespace ts {
}
}

function findBestTypeForObjectLiteral(source: Type, unionTarget: UnionOrIntersectionType) {
if (getObjectFlags(source) & ObjectFlags.ObjectLiteral && forEachType(unionTarget, isArrayLikeType)) {
return find(unionTarget.types, t => !isArrayLikeType(t));
}
}

// Keep this up-to-date with the same logic within `getApparentTypeOfContextualType`, since they should behave similarly
function findMatchingDiscriminantType(source: Type, target: UnionOrIntersectionType) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
tests/cases/compiler/objectLiteralsAgainstUnionsOfArrays01.ts(9,5): error TS2322: Type '{ bar: { prop: number; }; }[]' is not assignable to type 'Foo[]'.
Type '{ bar: { prop: number; }; }' is not assignable to type 'Foo'.
Types of property 'bar' are incompatible.
Type '{ prop: number; }' is not assignable to type 'Bar | Bar[]'.
Type '{ prop: number; }' is not assignable to type 'Bar'.
Types of property 'prop' are incompatible.
Type 'number' is not assignable to type 'string'.


==== tests/cases/compiler/objectLiteralsAgainstUnionsOfArrays01.ts (1 errors) ====
interface Foo {
bar: Bar | Bar[];
}

interface Bar {
prop: string;
}

let x: Foo[] = [
~
!!! error TS2322: Type '{ bar: { prop: number; }; }[]' is not assignable to type 'Foo[]'.
!!! error TS2322: Type '{ bar: { prop: number; }; }' is not assignable to type 'Foo'.
!!! error TS2322: Types of property 'bar' are incompatible.
!!! error TS2322: Type '{ prop: number; }' is not assignable to type 'Bar | Bar[]'.
!!! error TS2322: Type '{ prop: number; }' is not assignable to type 'Bar'.
!!! error TS2322: Types of property 'prop' are incompatible.
!!! error TS2322: Type 'number' is not assignable to type 'string'.
{ bar: { prop: 100 } }
]

18 changes: 18 additions & 0 deletions tests/baselines/reference/objectLiteralsAgainstUnionsOfArrays01.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//// [objectLiteralsAgainstUnionsOfArrays01.ts]
interface Foo {
bar: Bar | Bar[];
}

interface Bar {
prop: string;
}

let x: Foo[] = [
{ bar: { prop: 100 } }
]


//// [objectLiteralsAgainstUnionsOfArrays01.js]
var x = [
{ bar: { prop: 100 } }
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
=== tests/cases/compiler/objectLiteralsAgainstUnionsOfArrays01.ts ===
interface Foo {
>Foo : Symbol(Foo, Decl(objectLiteralsAgainstUnionsOfArrays01.ts, 0, 0))

bar: Bar | Bar[];
>bar : Symbol(Foo.bar, Decl(objectLiteralsAgainstUnionsOfArrays01.ts, 0, 15))
>Bar : Symbol(Bar, Decl(objectLiteralsAgainstUnionsOfArrays01.ts, 2, 1))
>Bar : Symbol(Bar, Decl(objectLiteralsAgainstUnionsOfArrays01.ts, 2, 1))
}

interface Bar {
>Bar : Symbol(Bar, Decl(objectLiteralsAgainstUnionsOfArrays01.ts, 2, 1))

prop: string;
>prop : Symbol(Bar.prop, Decl(objectLiteralsAgainstUnionsOfArrays01.ts, 4, 15))
}

let x: Foo[] = [
>x : Symbol(x, Decl(objectLiteralsAgainstUnionsOfArrays01.ts, 8, 3))
>Foo : Symbol(Foo, Decl(objectLiteralsAgainstUnionsOfArrays01.ts, 0, 0))

{ bar: { prop: 100 } }
>bar : Symbol(bar, Decl(objectLiteralsAgainstUnionsOfArrays01.ts, 9, 3))
>prop : Symbol(prop, Decl(objectLiteralsAgainstUnionsOfArrays01.ts, 9, 10))

]

Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
=== tests/cases/compiler/objectLiteralsAgainstUnionsOfArrays01.ts ===
interface Foo {
>Foo : Foo

bar: Bar | Bar[];
>bar : Bar | Bar[]
>Bar : Bar
>Bar : Bar
}

interface Bar {
>Bar : Bar

prop: string;
>prop : string
}

let x: Foo[] = [
>x : Foo[]
>Foo : Foo
>[ { bar: { prop: 100 } }] : { bar: { prop: number; }; }[]

{ bar: { prop: 100 } }
>{ bar: { prop: 100 } } : { bar: { prop: number; }; }
>bar : { prop: number; }
>{ prop: 100 } : { prop: number; }
>prop : number
>100 : 100

]

11 changes: 11 additions & 0 deletions tests/cases/compiler/objectLiteralsAgainstUnionsOfArrays01.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
interface Foo {
bar: Bar | Bar[];
}

interface Bar {
prop: string;
}

let x: Foo[] = [
{ bar: { prop: 100 } }
]