Skip to content

Use string/number signature to get contextual type #27849

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16708,6 +16708,8 @@ namespace ts {
return restType;
}
}
return isNumericLiteralName(name) && getIndexTypeOfContextualType(type, IndexKind.Number) ||
getIndexTypeOfContextualType(type, IndexKind.String);
}
return undefined;
}, /*noReductions*/ true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ interface IWithNumberIndexSignature2 {
// If S is not empty, U has a string index signature of a union type of
// the types of the string index signatures from each type in S.
var x: IWithNoStringIndexSignature | IWithStringIndexSignature1 = { z: a => a }; // a should be number
var x: IWithNoStringIndexSignature | IWithStringIndexSignature1 = { foo: a => a }; // a should be any
var x: IWithNoStringIndexSignature | IWithStringIndexSignature1 = { foo: a => a }; // a should be number (because of index signature of IWithStringIndexSignature1)
var x: IWithNoStringIndexSignature | IWithStringIndexSignature1 = { foo: "hello" };
var x2: IWithStringIndexSignature1 | IWithStringIndexSignature2 = { z: a => a.toString() }; // a should be number
var x2: IWithStringIndexSignature1 | IWithStringIndexSignature2 = { z: a => a }; // a should be number
Expand All @@ -49,7 +49,7 @@ var x2: IWithStringIndexSignature1 | IWithStringIndexSignature2 = { z: a => a };
// If S is not empty, U has a numeric index signature of a union type of
// the types of the numeric index signatures from each type in S.
var x3: IWithNoNumberIndexSignature | IWithNumberIndexSignature1 = { 1: a => a }; // a should be number
var x3: IWithNoNumberIndexSignature | IWithNumberIndexSignature1 = { 0: a => a }; // a should be any
var x3: IWithNoNumberIndexSignature | IWithNumberIndexSignature1 = { 0: a => a }; // a should be number (because of index signature of IWithNumberIndexSignature1)
var x3: IWithNoNumberIndexSignature | IWithNumberIndexSignature1 = { 0: "hello" };
var x4: IWithNumberIndexSignature1 | IWithNumberIndexSignature2 = { 1: a => a.toString() }; // a should be number
var x4: IWithNumberIndexSignature1 | IWithNumberIndexSignature2 = { 1: a => a }; // a should be number
Expand All @@ -66,15 +66,15 @@ var x4: IWithNumberIndexSignature1 | IWithNumberIndexSignature2 = { 1: a => a };
// If S is not empty, U has a string index signature of a union type of
// the types of the string index signatures from each type in S.
var x = { z: function (a) { return a; } }; // a should be number
var x = { foo: function (a) { return a; } }; // a should be any
var x = { foo: function (a) { return a; } }; // a should be number (because of index signature of IWithStringIndexSignature1)
var x = { foo: "hello" };
var x2 = { z: function (a) { return a.toString(); } }; // a should be number
var x2 = { z: function (a) { return a; } }; // a should be number
// Let S be the set of types in U that has a numeric index signature.
// If S is not empty, U has a numeric index signature of a union type of
// the types of the numeric index signatures from each type in S.
var x3 = { 1: function (a) { return a; } }; // a should be number
var x3 = { 0: function (a) { return a; } }; // a should be any
var x3 = { 0: function (a) { return a; } }; // a should be number (because of index signature of IWithNumberIndexSignature1)
var x3 = { 0: "hello" };
var x4 = { 1: function (a) { return a.toString(); } }; // a should be number
var x4 = { 1: function (a) { return a; } }; // a should be number
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ var x: IWithNoStringIndexSignature | IWithStringIndexSignature1 = { z: a => a };
>a : Symbol(a, Decl(contextualTypeWithUnionTypeIndexSignatures.ts, 39, 70))
>a : Symbol(a, Decl(contextualTypeWithUnionTypeIndexSignatures.ts, 39, 70))

var x: IWithNoStringIndexSignature | IWithStringIndexSignature1 = { foo: a => a }; // a should be any
var x: IWithNoStringIndexSignature | IWithStringIndexSignature1 = { foo: a => a }; // a should be number (because of index signature of IWithStringIndexSignature1)
>x : Symbol(x, Decl(contextualTypeWithUnionTypeIndexSignatures.ts, 39, 3), Decl(contextualTypeWithUnionTypeIndexSignatures.ts, 40, 3), Decl(contextualTypeWithUnionTypeIndexSignatures.ts, 41, 3))
>IWithNoStringIndexSignature : Symbol(IWithNoStringIndexSignature, Decl(contextualTypeWithUnionTypeIndexSignatures.ts, 7, 1))
>IWithStringIndexSignature1 : Symbol(IWithStringIndexSignature1, Decl(contextualTypeWithUnionTypeIndexSignatures.ts, 14, 1))
Expand Down Expand Up @@ -118,7 +118,7 @@ var x3: IWithNoNumberIndexSignature | IWithNumberIndexSignature1 = { 1: a => a }
>a : Symbol(a, Decl(contextualTypeWithUnionTypeIndexSignatures.ts, 49, 71))
>a : Symbol(a, Decl(contextualTypeWithUnionTypeIndexSignatures.ts, 49, 71))

var x3: IWithNoNumberIndexSignature | IWithNumberIndexSignature1 = { 0: a => a }; // a should be any
var x3: IWithNoNumberIndexSignature | IWithNumberIndexSignature1 = { 0: a => a }; // a should be number (because of index signature of IWithNumberIndexSignature1)
>x3 : Symbol(x3, Decl(contextualTypeWithUnionTypeIndexSignatures.ts, 49, 3), Decl(contextualTypeWithUnionTypeIndexSignatures.ts, 50, 3), Decl(contextualTypeWithUnionTypeIndexSignatures.ts, 51, 3))
>IWithNoNumberIndexSignature : Symbol(IWithNoNumberIndexSignature, Decl(contextualTypeWithUnionTypeIndexSignatures.ts, 11, 1))
>IWithNumberIndexSignature1 : Symbol(IWithNumberIndexSignature1, Decl(contextualTypeWithUnionTypeIndexSignatures.ts, 20, 1))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@ var x: IWithNoStringIndexSignature | IWithStringIndexSignature1 = { z: a => a };
>a : number
>a : number

var x: IWithNoStringIndexSignature | IWithStringIndexSignature1 = { foo: a => a }; // a should be any
var x: IWithNoStringIndexSignature | IWithStringIndexSignature1 = { foo: a => a }; // a should be number (because of index signature of IWithStringIndexSignature1)
>x : IWithNoStringIndexSignature | IWithStringIndexSignature1
>{ foo: a => a } : { foo: (a: any) => any; }
>foo : (a: any) => any
>a => a : (a: any) => any
>a : any
>a : any
>{ foo: a => a } : { foo: (a: number) => number; }
>foo : (a: number) => number
>a => a : (a: number) => number
>a : number
>a : number

var x: IWithNoStringIndexSignature | IWithStringIndexSignature1 = { foo: "hello" };
>x : IWithNoStringIndexSignature | IWithStringIndexSignature1
Expand Down Expand Up @@ -99,13 +99,13 @@ var x3: IWithNoNumberIndexSignature | IWithNumberIndexSignature1 = { 1: a => a }
>a : number
>a : number

var x3: IWithNoNumberIndexSignature | IWithNumberIndexSignature1 = { 0: a => a }; // a should be any
var x3: IWithNoNumberIndexSignature | IWithNumberIndexSignature1 = { 0: a => a }; // a should be number (because of index signature of IWithNumberIndexSignature1)
>x3 : IWithNoNumberIndexSignature | IWithNumberIndexSignature1
>{ 0: a => a } : { 0: (a: any) => any; }
>0 : (a: any) => any
>a => a : (a: any) => any
>a : any
>a : any
>{ 0: a => a } : { 0: (a: number) => number; }
>0 : (a: number) => number
>a => a : (a: number) => number
>a : number
>a : number

var x3: IWithNoNumberIndexSignature | IWithNumberIndexSignature1 = { 0: "hello" };
>x3 : IWithNoNumberIndexSignature | IWithNumberIndexSignature1
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,8 @@ const a = <App4 state={100} foo={s => s} />; // TODO: should be number => number
>App4 : <State, Actions extends ActionsObjectOr<State>>(props: (string & { state: State; }) | (Actions & { state: State; })) => JSX.Element
>state : number
>100 : 100
>foo : (s: any) => any
>s => s : (s: any) => any
>s : any
>s : any
>foo : (s: number) => number
>s => s : (s: number) => number
>s : number
>s : number

2 changes: 1 addition & 1 deletion tests/baselines/reference/narrowingByTypeofInSwitch.types
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ function multipleGenericFuse<X extends L | number, Y extends R | number>(xy: X |

case 'function': return [xy, 1];
>'function' : "function"
>[xy, 1] : [X, number]
>[xy, 1] : [X, 1]
>xy : X
>1 : 1

Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/tsxAttributeResolution10.types
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export class MyComponent {
<MyComponent bar={true} />;
><MyComponent bar={true} /> : JSX.Element
>MyComponent : typeof MyComponent
>bar : boolean
>bar : true
>true : true

// Should be ok
Expand Down
8 changes: 8 additions & 0 deletions tests/baselines/reference/unionTypeWithIndexedLiteralType.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//// [unionTypeWithIndexedLiteralType.ts]
interface I { x: number; }
interface Idx { [index: string]: U; }
type U = Idx | I | "lit";
const u: U = { x: "lit" };

//// [unionTypeWithIndexedLiteralType.js]
var u = { x: "lit" };
20 changes: 20 additions & 0 deletions tests/baselines/reference/unionTypeWithIndexedLiteralType.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
=== tests/cases/compiler/unionTypeWithIndexedLiteralType.ts ===
interface I { x: number; }
>I : Symbol(I, Decl(unionTypeWithIndexedLiteralType.ts, 0, 0))
>x : Symbol(I.x, Decl(unionTypeWithIndexedLiteralType.ts, 0, 13))

interface Idx { [index: string]: U; }
>Idx : Symbol(Idx, Decl(unionTypeWithIndexedLiteralType.ts, 0, 26))
>index : Symbol(index, Decl(unionTypeWithIndexedLiteralType.ts, 1, 17))
>U : Symbol(U, Decl(unionTypeWithIndexedLiteralType.ts, 1, 37))

type U = Idx | I | "lit";
>U : Symbol(U, Decl(unionTypeWithIndexedLiteralType.ts, 1, 37))
>Idx : Symbol(Idx, Decl(unionTypeWithIndexedLiteralType.ts, 0, 26))
>I : Symbol(I, Decl(unionTypeWithIndexedLiteralType.ts, 0, 0))

const u: U = { x: "lit" };
>u : Symbol(u, Decl(unionTypeWithIndexedLiteralType.ts, 3, 5))
>U : Symbol(U, Decl(unionTypeWithIndexedLiteralType.ts, 1, 37))
>x : Symbol(x, Decl(unionTypeWithIndexedLiteralType.ts, 3, 14))

16 changes: 16 additions & 0 deletions tests/baselines/reference/unionTypeWithIndexedLiteralType.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
=== tests/cases/compiler/unionTypeWithIndexedLiteralType.ts ===
interface I { x: number; }
>x : number

interface Idx { [index: string]: U; }
>index : string

type U = Idx | I | "lit";
>U : U

const u: U = { x: "lit" };
>u : U
>{ x: "lit" } : { x: "lit"; }
>x : "lit"
>"lit" : "lit"

4 changes: 4 additions & 0 deletions tests/cases/compiler/unionTypeWithIndexedLiteralType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
interface I { x: number; }
interface Idx { [index: string]: U; }
type U = Idx | I | "lit";
const u: U = { x: "lit" };
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ interface IWithNumberIndexSignature2 {
// If S is not empty, U has a string index signature of a union type of
// the types of the string index signatures from each type in S.
var x: IWithNoStringIndexSignature | IWithStringIndexSignature1 = { z: a => a }; // a should be number
var x: IWithNoStringIndexSignature | IWithStringIndexSignature1 = { foo: a => a }; // a should be any
var x: IWithNoStringIndexSignature | IWithStringIndexSignature1 = { foo: a => a }; // a should be number (because of index signature of IWithStringIndexSignature1)
var x: IWithNoStringIndexSignature | IWithStringIndexSignature1 = { foo: "hello" };
var x2: IWithStringIndexSignature1 | IWithStringIndexSignature2 = { z: a => a.toString() }; // a should be number
var x2: IWithStringIndexSignature1 | IWithStringIndexSignature2 = { z: a => a }; // a should be number
Expand All @@ -48,7 +48,7 @@ var x2: IWithStringIndexSignature1 | IWithStringIndexSignature2 = { z: a => a };
// If S is not empty, U has a numeric index signature of a union type of
// the types of the numeric index signatures from each type in S.
var x3: IWithNoNumberIndexSignature | IWithNumberIndexSignature1 = { 1: a => a }; // a should be number
var x3: IWithNoNumberIndexSignature | IWithNumberIndexSignature1 = { 0: a => a }; // a should be any
var x3: IWithNoNumberIndexSignature | IWithNumberIndexSignature1 = { 0: a => a }; // a should be number (because of index signature of IWithNumberIndexSignature1)
var x3: IWithNoNumberIndexSignature | IWithNumberIndexSignature1 = { 0: "hello" };
var x4: IWithNumberIndexSignature1 | IWithNumberIndexSignature2 = { 1: a => a.toString() }; // a should be number
var x4: IWithNumberIndexSignature1 | IWithNumberIndexSignature2 = { 1: a => a }; // a should be number