Skip to content

Commit 1d7723a

Browse files
Fixed inconsistent type compatibility for a type with a call signature and and index signature (microsoft#23226)
1 parent 4170f35 commit 1d7723a

File tree

5 files changed

+64
-1
lines changed

5 files changed

+64
-1
lines changed

src/compiler/checker.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -11451,9 +11451,17 @@ namespace ts {
1145111451

1145211452
/**
1145311453
* Return true if type was inferred from an object literal, written as an object type literal, or is the shape of a module
11454-
* with no call or construct signatures.
11454+
* 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.
1145511455
*/
1145611456
function isObjectTypeWithInferableIndex(type: Type) {
11457+
if (type.flags & TypeFlags.Intersection) {
11458+
for (const t of (<IntersectionType>type).types) {
11459+
if (isObjectTypeWithInferableIndex(t)) {
11460+
return true;
11461+
}
11462+
}
11463+
return false;
11464+
}
1145711465
return type.symbol && (type.symbol.flags & (SymbolFlags.ObjectLiteral | SymbolFlags.TypeLiteral | SymbolFlags.ValueModule)) !== 0 &&
1145811466
!typeHasCallOrConstructSignatures(type);
1145911467
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//// [stringIndexerAndCallSignature.ts]
2+
type I = {
3+
(): string;
4+
[name: string]: number;
5+
}
6+
7+
declare let val: { (): string } & { foo: number; }
8+
let a: I = val;
9+
10+
11+
//// [stringIndexerAndCallSignature.js]
12+
var a = val;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
=== tests/cases/compiler/stringIndexerAndCallSignature.ts ===
2+
type I = {
3+
>I : Symbol(I, Decl(stringIndexerAndCallSignature.ts, 0, 0))
4+
5+
(): string;
6+
[name: string]: number;
7+
>name : Symbol(name, Decl(stringIndexerAndCallSignature.ts, 2, 5))
8+
}
9+
10+
declare let val: { (): string } & { foo: number; }
11+
>val : Symbol(val, Decl(stringIndexerAndCallSignature.ts, 5, 11))
12+
>foo : Symbol(foo, Decl(stringIndexerAndCallSignature.ts, 5, 35))
13+
14+
let a: I = val;
15+
>a : Symbol(a, Decl(stringIndexerAndCallSignature.ts, 6, 3))
16+
>I : Symbol(I, Decl(stringIndexerAndCallSignature.ts, 0, 0))
17+
>val : Symbol(val, Decl(stringIndexerAndCallSignature.ts, 5, 11))
18+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
=== tests/cases/compiler/stringIndexerAndCallSignature.ts ===
2+
type I = {
3+
>I : I
4+
5+
(): string;
6+
[name: string]: number;
7+
>name : string
8+
}
9+
10+
declare let val: { (): string } & { foo: number; }
11+
>val : (() => string) & { foo: number; }
12+
>foo : number
13+
14+
let a: I = val;
15+
>a : I
16+
>I : I
17+
>val : (() => string) & { foo: number; }
18+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
type I = {
2+
(): string;
3+
[name: string]: number;
4+
}
5+
6+
declare let val: { (): string } & { foo: number; }
7+
let a: I = val;

0 commit comments

Comments
 (0)