Skip to content

Commit 31f4053

Browse files
committed
When contextual type doesnt find property, use the index signature from unit type(fix the incorrectly used union type to get the signature)
Fixes #27949
1 parent 7b5ef64 commit 31f4053

5 files changed

+73
-2
lines changed

src/compiler/checker.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16732,8 +16732,8 @@ namespace ts {
1673216732
return restType;
1673316733
}
1673416734
}
16735-
return isNumericLiteralName(name) && getIndexTypeOfContextualType(type, IndexKind.Number) ||
16736-
getIndexTypeOfContextualType(type, IndexKind.String);
16735+
return isNumericLiteralName(name) && getIndexTypeOfContextualType(t, IndexKind.Number) ||
16736+
getIndexTypeOfContextualType(t, IndexKind.String);
1673716737
}
1673816738
return undefined;
1673916739
}, /*noReductions*/ true);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//// [unionTypeWithIndexAndMethodSignature.ts]
2+
interface Options {
3+
m(x: number): void;
4+
[key: string]: unknown;
5+
}
6+
declare function f(options: number | Options): void;
7+
f({
8+
m(x) { },
9+
});
10+
11+
//// [unionTypeWithIndexAndMethodSignature.js]
12+
"use strict";
13+
f({
14+
m: function (x) { }
15+
});
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
=== tests/cases/compiler/unionTypeWithIndexAndMethodSignature.ts ===
2+
interface Options {
3+
>Options : Symbol(Options, Decl(unionTypeWithIndexAndMethodSignature.ts, 0, 0))
4+
5+
m(x: number): void;
6+
>m : Symbol(Options.m, Decl(unionTypeWithIndexAndMethodSignature.ts, 0, 19))
7+
>x : Symbol(x, Decl(unionTypeWithIndexAndMethodSignature.ts, 1, 6))
8+
9+
[key: string]: unknown;
10+
>key : Symbol(key, Decl(unionTypeWithIndexAndMethodSignature.ts, 2, 5))
11+
}
12+
declare function f(options: number | Options): void;
13+
>f : Symbol(f, Decl(unionTypeWithIndexAndMethodSignature.ts, 3, 1))
14+
>options : Symbol(options, Decl(unionTypeWithIndexAndMethodSignature.ts, 4, 19))
15+
>Options : Symbol(Options, Decl(unionTypeWithIndexAndMethodSignature.ts, 0, 0))
16+
17+
f({
18+
>f : Symbol(f, Decl(unionTypeWithIndexAndMethodSignature.ts, 3, 1))
19+
20+
m(x) { },
21+
>m : Symbol(m, Decl(unionTypeWithIndexAndMethodSignature.ts, 5, 3))
22+
>x : Symbol(x, Decl(unionTypeWithIndexAndMethodSignature.ts, 6, 6))
23+
24+
});
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
=== tests/cases/compiler/unionTypeWithIndexAndMethodSignature.ts ===
2+
interface Options {
3+
m(x: number): void;
4+
>m : (x: number) => void
5+
>x : number
6+
7+
[key: string]: unknown;
8+
>key : string
9+
}
10+
declare function f(options: number | Options): void;
11+
>f : (options: number | Options) => void
12+
>options : number | Options
13+
14+
f({
15+
>f({ m(x) { },}) : void
16+
>f : (options: number | Options) => void
17+
>{ m(x) { },} : { m(x: number): void; }
18+
19+
m(x) { },
20+
>m : (x: number) => void
21+
>x : number
22+
23+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// @strict: true
2+
interface Options {
3+
m(x: number): void;
4+
[key: string]: unknown;
5+
}
6+
declare function f(options: number | Options): void;
7+
f({
8+
m(x) { },
9+
});

0 commit comments

Comments
 (0)