Skip to content

Commit d3413b8

Browse files
committed
Instantiate contextual types while in an inferrential context
1 parent a453eff commit d3413b8

7 files changed

+264
-66
lines changed

src/compiler/checker.ts

Lines changed: 66 additions & 60 deletions
Large diffs are not rendered by default.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//// [index.ts]
2+
interface ActionsObject<State> {
3+
[prop: string]: (state: State) => State;
4+
}
5+
6+
interface Options<State, Actions> {
7+
state?: State;
8+
view?: (state: State, actions: Actions) => any;
9+
actions: string | Actions;
10+
}
11+
12+
declare function app<State, Actions extends ActionsObject<State>>(obj: Options<State, Actions>): void;
13+
14+
app({
15+
state: 100,
16+
actions: {
17+
foo: s => s
18+
},
19+
view: (s, a) => undefined as any,
20+
});
21+
22+
23+
//// [index.js]
24+
app({
25+
state: 100,
26+
actions: {
27+
foo: function (s) { return s; }
28+
},
29+
view: function (s, a) { return undefined; }
30+
});
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
=== tests/cases/compiler/index.ts ===
2+
interface ActionsObject<State> {
3+
>ActionsObject : Symbol(ActionsObject, Decl(index.ts, 0, 0))
4+
>State : Symbol(State, Decl(index.ts, 0, 24))
5+
6+
[prop: string]: (state: State) => State;
7+
>prop : Symbol(prop, Decl(index.ts, 1, 5))
8+
>state : Symbol(state, Decl(index.ts, 1, 21))
9+
>State : Symbol(State, Decl(index.ts, 0, 24))
10+
>State : Symbol(State, Decl(index.ts, 0, 24))
11+
}
12+
13+
interface Options<State, Actions> {
14+
>Options : Symbol(Options, Decl(index.ts, 2, 1))
15+
>State : Symbol(State, Decl(index.ts, 4, 18))
16+
>Actions : Symbol(Actions, Decl(index.ts, 4, 24))
17+
18+
state?: State;
19+
>state : Symbol(Options.state, Decl(index.ts, 4, 35))
20+
>State : Symbol(State, Decl(index.ts, 4, 18))
21+
22+
view?: (state: State, actions: Actions) => any;
23+
>view : Symbol(Options.view, Decl(index.ts, 5, 18))
24+
>state : Symbol(state, Decl(index.ts, 6, 12))
25+
>State : Symbol(State, Decl(index.ts, 4, 18))
26+
>actions : Symbol(actions, Decl(index.ts, 6, 25))
27+
>Actions : Symbol(Actions, Decl(index.ts, 4, 24))
28+
29+
actions: string | Actions;
30+
>actions : Symbol(Options.actions, Decl(index.ts, 6, 51))
31+
>Actions : Symbol(Actions, Decl(index.ts, 4, 24))
32+
}
33+
34+
declare function app<State, Actions extends ActionsObject<State>>(obj: Options<State, Actions>): void;
35+
>app : Symbol(app, Decl(index.ts, 8, 1))
36+
>State : Symbol(State, Decl(index.ts, 10, 21))
37+
>Actions : Symbol(Actions, Decl(index.ts, 10, 27))
38+
>ActionsObject : Symbol(ActionsObject, Decl(index.ts, 0, 0))
39+
>State : Symbol(State, Decl(index.ts, 10, 21))
40+
>obj : Symbol(obj, Decl(index.ts, 10, 66))
41+
>Options : Symbol(Options, Decl(index.ts, 2, 1))
42+
>State : Symbol(State, Decl(index.ts, 10, 21))
43+
>Actions : Symbol(Actions, Decl(index.ts, 10, 27))
44+
45+
app({
46+
>app : Symbol(app, Decl(index.ts, 8, 1))
47+
48+
state: 100,
49+
>state : Symbol(state, Decl(index.ts, 12, 5))
50+
51+
actions: {
52+
>actions : Symbol(actions, Decl(index.ts, 13, 15))
53+
54+
foo: s => s
55+
>foo : Symbol(foo, Decl(index.ts, 14, 14))
56+
>s : Symbol(s, Decl(index.ts, 15, 12))
57+
>s : Symbol(s, Decl(index.ts, 15, 12))
58+
59+
},
60+
view: (s, a) => undefined as any,
61+
>view : Symbol(view, Decl(index.ts, 16, 6))
62+
>s : Symbol(s, Decl(index.ts, 17, 11))
63+
>a : Symbol(a, Decl(index.ts, 17, 13))
64+
>undefined : Symbol(undefined)
65+
66+
});
67+
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
=== tests/cases/compiler/index.ts ===
2+
interface ActionsObject<State> {
3+
>ActionsObject : ActionsObject<State>
4+
>State : State
5+
6+
[prop: string]: (state: State) => State;
7+
>prop : string
8+
>state : State
9+
>State : State
10+
>State : State
11+
}
12+
13+
interface Options<State, Actions> {
14+
>Options : Options<State, Actions>
15+
>State : State
16+
>Actions : Actions
17+
18+
state?: State;
19+
>state : State
20+
>State : State
21+
22+
view?: (state: State, actions: Actions) => any;
23+
>view : (state: State, actions: Actions) => any
24+
>state : State
25+
>State : State
26+
>actions : Actions
27+
>Actions : Actions
28+
29+
actions: string | Actions;
30+
>actions : string | Actions
31+
>Actions : Actions
32+
}
33+
34+
declare function app<State, Actions extends ActionsObject<State>>(obj: Options<State, Actions>): void;
35+
>app : <State, Actions extends ActionsObject<State>>(obj: Options<State, Actions>) => void
36+
>State : State
37+
>Actions : Actions
38+
>ActionsObject : ActionsObject<State>
39+
>State : State
40+
>obj : Options<State, Actions>
41+
>Options : Options<State, Actions>
42+
>State : State
43+
>Actions : Actions
44+
45+
app({
46+
>app({ state: 100, actions: { foo: s => s }, view: (s, a) => undefined as any,}) : void
47+
>app : <State, Actions extends ActionsObject<State>>(obj: Options<State, Actions>) => void
48+
>{ state: 100, actions: { foo: s => s }, view: (s, a) => undefined as any,} : { state: number; actions: { foo: (s: number) => number; }; view: (s: number, a: ActionsObject<number>) => any; }
49+
50+
state: 100,
51+
>state : number
52+
>100 : 100
53+
54+
actions: {
55+
>actions : { foo: (s: number) => number; }
56+
>{ foo: s => s } : { foo: (s: number) => number; }
57+
58+
foo: s => s
59+
>foo : (s: number) => number
60+
>s => s : (s: number) => number
61+
>s : number
62+
>s : number
63+
64+
},
65+
view: (s, a) => undefined as any,
66+
>view : (s: number, a: ActionsObject<number>) => any
67+
>(s, a) => undefined as any : (s: number, a: ActionsObject<number>) => any
68+
>s : number
69+
>a : ActionsObject<number>
70+
>undefined as any : any
71+
>undefined : undefined
72+
73+
});
74+

tests/baselines/reference/functionConstraintSatisfaction.types

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ var r = foo(new Function());
4444
>Function : FunctionConstructor
4545

4646
var r1 = foo((x) => x);
47-
>r1 : (x: any) => any
48-
>foo((x) => x) : (x: any) => any
47+
>r1 : Function
48+
>foo((x) => x) : Function
4949
>foo : <T extends Function>(x: T) => T
5050
>(x) => x : (x: any) => any
5151
>x : any
@@ -60,8 +60,8 @@ var r2 = foo((x: string[]) => x);
6060
>x : string[]
6161

6262
var r3 = foo(function (x) { return x });
63-
>r3 : (x: any) => any
64-
>foo(function (x) { return x }) : (x: any) => any
63+
>r3 : Function
64+
>foo(function (x) { return x }) : Function
6565
>foo : <T extends Function>(x: T) => T
6666
>function (x) { return x } : (x: any) => any
6767
>x : any

tests/baselines/reference/promisePermutations3.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ tests/cases/compiler/promisePermutations3.ts(159,21): error TS2345: Argument of
7171
Types of parameters 'onfulfilled' and 'success' are incompatible.
7272
Types of parameters 'value' and 'value' are incompatible.
7373
Type 'number' is not assignable to type 'string'.
74-
tests/cases/compiler/promisePermutations3.ts(165,21): error TS2345: Argument of type '{ <T>(x: T): IPromise<T>; <T>(x: T, y: T): Promise<T>; }' is not assignable to parameter of type '(value: (x: any) => any) => Promise<any>'.
74+
tests/cases/compiler/promisePermutations3.ts(165,21): error TS2345: Argument of type '{ <T>(x: T): IPromise<T>; <T>(x: T, y: T): Promise<T>; }' is not assignable to parameter of type '(value: {}) => Promise<any>'.
7575
Type 'IPromise<any>' is not assignable to type 'Promise<any>'.
7676
Types of property 'then' are incompatible.
7777
Type '<U>(success?: (value: any) => U, error?: (error: any) => U, progress?: (progress: any) => void) => IPromise<U>' is not assignable to type '{ <TResult1 = any, TResult2 = never>(onfulfilled?: (value: any) => TResult1 | PromiseLike<TResult1>, onrejected?: (reason: any) => TResult2 | PromiseLike<TResult2>): Promise<TResult1 | TResult2>; <U>(success?: (value: any) => Promise<U>, error?: (error: any) => Promise<U>, progress?: (progress: any) => void): Promise<U>; <U>(success?: (value: any) => Promise<U>, error?: (error: any) => U, progress?: (progress: any) => void): Promise<U>; <U>(success?: (value: any) => U, error?: (error: any) => Promise<U>, progress?: (progress: any) => void): Promise<U>; <U>(success?: (value: any) => U, error?: (error: any) => U, progress?: (progress: any) => void): Promise<U>; }'.
@@ -352,7 +352,7 @@ tests/cases/compiler/promisePermutations3.ts(165,21): error TS2345: Argument of
352352
var s12a = s12.then(testFunction12, testFunction12, testFunction12); // ok
353353
var s12b = s12.then(testFunction12P, testFunction12P, testFunction12P); // ok
354354
~~~~~~~~~~~~~~~
355-
!!! error TS2345: Argument of type '{ <T>(x: T): IPromise<T>; <T>(x: T, y: T): Promise<T>; }' is not assignable to parameter of type '(value: (x: any) => any) => Promise<any>'.
355+
!!! error TS2345: Argument of type '{ <T>(x: T): IPromise<T>; <T>(x: T, y: T): Promise<T>; }' is not assignable to parameter of type '(value: {}) => Promise<any>'.
356356
!!! error TS2345: Type 'IPromise<any>' is not assignable to type 'Promise<any>'.
357357
!!! error TS2345: Types of property 'then' are incompatible.
358358
!!! error TS2345: Type '<U>(success?: (value: any) => U, error?: (error: any) => U, progress?: (progress: any) => void) => IPromise<U>' is not assignable to type '{ <TResult1 = any, TResult2 = never>(onfulfilled?: (value: any) => TResult1 | PromiseLike<TResult1>, onrejected?: (reason: any) => TResult2 | PromiseLike<TResult2>): Promise<TResult1 | TResult2>; <U>(success?: (value: any) => Promise<U>, error?: (error: any) => Promise<U>, progress?: (progress: any) => void): Promise<U>; <U>(success?: (value: any) => Promise<U>, error?: (error: any) => U, progress?: (progress: any) => void): Promise<U>; <U>(success?: (value: any) => U, error?: (error: any) => Promise<U>, progress?: (progress: any) => void): Promise<U>; <U>(success?: (value: any) => U, error?: (error: any) => U, progress?: (progress: any) => void): Promise<U>; }'.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// @noImplicitAny: true
2+
// @filename: index.ts
3+
interface ActionsObject<State> {
4+
[prop: string]: (state: State) => State;
5+
}
6+
7+
interface Options<State, Actions> {
8+
state?: State;
9+
view?: (state: State, actions: Actions) => any;
10+
actions: string | Actions;
11+
}
12+
13+
declare function app<State, Actions extends ActionsObject<State>>(obj: Options<State, Actions>): void;
14+
15+
app({
16+
state: 100,
17+
actions: {
18+
foo: s => s
19+
},
20+
view: (s, a) => undefined as any,
21+
});

0 commit comments

Comments
 (0)