Skip to content

Commit 7b9ae4d

Browse files
authored
Merge pull request #27952 from Microsoft/contextualSignature
When contextual type doesnt find property in unit type, use the index signature from unit type(fix the incorrectly used union type to get the signature)
2 parents 9554f09 + 31f4053 commit 7b9ae4d

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
@@ -16743,8 +16743,8 @@ namespace ts {
1674316743
return restType;
1674416744
}
1674516745
}
16746-
return isNumericLiteralName(name) && getIndexTypeOfContextualType(type, IndexKind.Number) ||
16747-
getIndexTypeOfContextualType(type, IndexKind.String);
16746+
return isNumericLiteralName(name) && getIndexTypeOfContextualType(t, IndexKind.Number) ||
16747+
getIndexTypeOfContextualType(t, IndexKind.String);
1674816748
}
1674916749
return undefined;
1675016750
}, /*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)