Skip to content

Commit

Permalink
Fixed inconsistent type compatibility for a type with a call signatur…
Browse files Browse the repository at this point in the history
…e and and index signature (microsoft#23226)
  • Loading branch information
dragomirtitian committed Apr 7, 2018
1 parent 4170f35 commit 1d7723a
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11451,9 +11451,17 @@ namespace ts {

/**
* Return true if type was inferred from an object literal, written as an object type literal, or is the shape of a module
* with no call or construct signatures.
* with no call or construct signatures, or is an intersection type and one of the constituents of the intersection satisfies any on the previous rules.
*/
function isObjectTypeWithInferableIndex(type: Type) {
if (type.flags & TypeFlags.Intersection) {
for (const t of (<IntersectionType>type).types) {
if (isObjectTypeWithInferableIndex(t)) {
return true;
}
}
return false;
}
return type.symbol && (type.symbol.flags & (SymbolFlags.ObjectLiteral | SymbolFlags.TypeLiteral | SymbolFlags.ValueModule)) !== 0 &&
!typeHasCallOrConstructSignatures(type);
}
Expand Down
12 changes: 12 additions & 0 deletions tests/baselines/reference/stringIndexerAndCallSignature.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//// [stringIndexerAndCallSignature.ts]
type I = {
(): string;
[name: string]: number;
}

declare let val: { (): string } & { foo: number; }
let a: I = val;


//// [stringIndexerAndCallSignature.js]
var a = val;
18 changes: 18 additions & 0 deletions tests/baselines/reference/stringIndexerAndCallSignature.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
=== tests/cases/compiler/stringIndexerAndCallSignature.ts ===
type I = {
>I : Symbol(I, Decl(stringIndexerAndCallSignature.ts, 0, 0))

(): string;
[name: string]: number;
>name : Symbol(name, Decl(stringIndexerAndCallSignature.ts, 2, 5))
}

declare let val: { (): string } & { foo: number; }
>val : Symbol(val, Decl(stringIndexerAndCallSignature.ts, 5, 11))
>foo : Symbol(foo, Decl(stringIndexerAndCallSignature.ts, 5, 35))

let a: I = val;
>a : Symbol(a, Decl(stringIndexerAndCallSignature.ts, 6, 3))
>I : Symbol(I, Decl(stringIndexerAndCallSignature.ts, 0, 0))
>val : Symbol(val, Decl(stringIndexerAndCallSignature.ts, 5, 11))

18 changes: 18 additions & 0 deletions tests/baselines/reference/stringIndexerAndCallSignature.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
=== tests/cases/compiler/stringIndexerAndCallSignature.ts ===
type I = {
>I : I

(): string;
[name: string]: number;
>name : string
}

declare let val: { (): string } & { foo: number; }
>val : (() => string) & { foo: number; }
>foo : number

let a: I = val;
>a : I
>I : I
>val : (() => string) & { foo: number; }

7 changes: 7 additions & 0 deletions tests/cases/compiler/stringIndexerAndCallSignature.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
type I = {
(): string;
[name: string]: number;
}

declare let val: { (): string } & { foo: number; }
let a: I = val;

0 comments on commit 1d7723a

Please sign in to comment.