From 31f4053d3eb873560c217771c1a6c81635008503 Mon Sep 17 00:00:00 2001 From: Sheetal Nandi Date: Wed, 17 Oct 2018 14:15:56 -0700 Subject: [PATCH] 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 --- src/compiler/checker.ts | 4 ++-- .../unionTypeWithIndexAndMethodSignature.js | 15 ++++++++++++ ...ionTypeWithIndexAndMethodSignature.symbols | 24 +++++++++++++++++++ ...unionTypeWithIndexAndMethodSignature.types | 23 ++++++++++++++++++ .../unionTypeWithIndexAndMethodSignature.ts | 9 +++++++ 5 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 tests/baselines/reference/unionTypeWithIndexAndMethodSignature.js create mode 100644 tests/baselines/reference/unionTypeWithIndexAndMethodSignature.symbols create mode 100644 tests/baselines/reference/unionTypeWithIndexAndMethodSignature.types create mode 100644 tests/cases/compiler/unionTypeWithIndexAndMethodSignature.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 604805545a4..8b6b71fa613 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -16732,8 +16732,8 @@ namespace ts { return restType; } } - return isNumericLiteralName(name) && getIndexTypeOfContextualType(type, IndexKind.Number) || - getIndexTypeOfContextualType(type, IndexKind.String); + return isNumericLiteralName(name) && getIndexTypeOfContextualType(t, IndexKind.Number) || + getIndexTypeOfContextualType(t, IndexKind.String); } return undefined; }, /*noReductions*/ true); diff --git a/tests/baselines/reference/unionTypeWithIndexAndMethodSignature.js b/tests/baselines/reference/unionTypeWithIndexAndMethodSignature.js new file mode 100644 index 00000000000..c78435ef0ce --- /dev/null +++ b/tests/baselines/reference/unionTypeWithIndexAndMethodSignature.js @@ -0,0 +1,15 @@ +//// [unionTypeWithIndexAndMethodSignature.ts] +interface Options { + m(x: number): void; + [key: string]: unknown; +} +declare function f(options: number | Options): void; +f({ + m(x) { }, +}); + +//// [unionTypeWithIndexAndMethodSignature.js] +"use strict"; +f({ + m: function (x) { } +}); diff --git a/tests/baselines/reference/unionTypeWithIndexAndMethodSignature.symbols b/tests/baselines/reference/unionTypeWithIndexAndMethodSignature.symbols new file mode 100644 index 00000000000..3eaae1f320d --- /dev/null +++ b/tests/baselines/reference/unionTypeWithIndexAndMethodSignature.symbols @@ -0,0 +1,24 @@ +=== tests/cases/compiler/unionTypeWithIndexAndMethodSignature.ts === +interface Options { +>Options : Symbol(Options, Decl(unionTypeWithIndexAndMethodSignature.ts, 0, 0)) + + m(x: number): void; +>m : Symbol(Options.m, Decl(unionTypeWithIndexAndMethodSignature.ts, 0, 19)) +>x : Symbol(x, Decl(unionTypeWithIndexAndMethodSignature.ts, 1, 6)) + + [key: string]: unknown; +>key : Symbol(key, Decl(unionTypeWithIndexAndMethodSignature.ts, 2, 5)) +} +declare function f(options: number | Options): void; +>f : Symbol(f, Decl(unionTypeWithIndexAndMethodSignature.ts, 3, 1)) +>options : Symbol(options, Decl(unionTypeWithIndexAndMethodSignature.ts, 4, 19)) +>Options : Symbol(Options, Decl(unionTypeWithIndexAndMethodSignature.ts, 0, 0)) + +f({ +>f : Symbol(f, Decl(unionTypeWithIndexAndMethodSignature.ts, 3, 1)) + + m(x) { }, +>m : Symbol(m, Decl(unionTypeWithIndexAndMethodSignature.ts, 5, 3)) +>x : Symbol(x, Decl(unionTypeWithIndexAndMethodSignature.ts, 6, 6)) + +}); diff --git a/tests/baselines/reference/unionTypeWithIndexAndMethodSignature.types b/tests/baselines/reference/unionTypeWithIndexAndMethodSignature.types new file mode 100644 index 00000000000..76607a40167 --- /dev/null +++ b/tests/baselines/reference/unionTypeWithIndexAndMethodSignature.types @@ -0,0 +1,23 @@ +=== tests/cases/compiler/unionTypeWithIndexAndMethodSignature.ts === +interface Options { + m(x: number): void; +>m : (x: number) => void +>x : number + + [key: string]: unknown; +>key : string +} +declare function f(options: number | Options): void; +>f : (options: number | Options) => void +>options : number | Options + +f({ +>f({ m(x) { },}) : void +>f : (options: number | Options) => void +>{ m(x) { },} : { m(x: number): void; } + + m(x) { }, +>m : (x: number) => void +>x : number + +}); diff --git a/tests/cases/compiler/unionTypeWithIndexAndMethodSignature.ts b/tests/cases/compiler/unionTypeWithIndexAndMethodSignature.ts new file mode 100644 index 00000000000..f8504b3ce22 --- /dev/null +++ b/tests/cases/compiler/unionTypeWithIndexAndMethodSignature.ts @@ -0,0 +1,9 @@ +// @strict: true +interface Options { + m(x: number): void; + [key: string]: unknown; +} +declare function f(options: number | Options): void; +f({ + m(x) { }, +}); \ No newline at end of file