Skip to content
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

Contextual typing based on instantiated types #30568

Merged
merged 6 commits into from
Mar 28, 2019
Merged
Show file tree
Hide file tree
Changes from 5 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
92 changes: 49 additions & 43 deletions src/compiler/checker.ts

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class A<T extends Object>{
>list : T
}
var a = new A();
>a : A<{}>
>new A() : A<{}>
>a : A<Object>
>new A() : A<Object>
>A : typeof A

Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ var r = foo(1); // ok
>1 : 1

var r2 = foo(null); // {}
>r2 : {}
>foo(null) : {}
>r2 : any
>foo(null) : any
>foo : <T, U extends T>(t: T) => U
>null : null

var r3 = foo(new Object()); // {}
>r3 : {}
>foo(new Object()) : {}
>r3 : Object
>foo(new Object()) : Object
>foo : <T, U extends T>(t: T) => U
>new Object() : Object
>Object : ObjectConstructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,11 @@ var r8 = foo(() => { }, () => { });
>() => { } : () => void

var r9 = foo(() => { }, () => 1);
>r9 : (x: () => void) => () => 1
>foo(() => { }, () => 1) : (x: () => void) => () => 1
>r9 : (x: () => void) => () => number
>foo(() => { }, () => 1) : (x: () => void) => () => number
>foo : <T, U extends T>(t: T, t2: U) => (x: T) => U
>() => { } : () => void
>() => 1 : () => 1
>() => 1 : () => number
>1 : 1

function other<T, U extends T>() {
Expand Down
164 changes: 164 additions & 0 deletions tests/baselines/reference/instantiateContextualTypes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
//// [instantiateContextualTypes.ts]
// #6611

export interface A<a> {
value: a;
}

function fn<a>(values: A<a>, value: a) : void {
}

declare let handlers: A<(value: number) => void>;
fn(handlers, value => alert(value));

// #21382

interface BaseProps<T> {
initialValues: T;
nextValues: (cur: T) => T;
}
declare class Component<P> { constructor(props: P); props: P; }
declare class GenericComponent<Props = {}, Values = object>
extends Component<Props & BaseProps<Values>> {
iv: Values;
}

new GenericComponent({ initialValues: 12, nextValues: val => 12 });

// #22149

declare function useStringOrNumber<T extends string | number>(t: T, useIt: T extends string ? ((s: string) => void) : ((n: number) => void)): void;
useStringOrNumber("", foo => {});

// #25299

type ActionType<P> = string & { attachPayloadTypeHack?: P & never }

type Handler<S, P> = P extends void
? (state: S) => S
: (state: S, payload: P) => S

interface ActionHandler<S, P> {
actionType: ActionType<P>
handler: Handler<S, P>
}

declare function handler<S, P>(actionType: ActionType<P>, handler: Handler<S, P>): ActionHandler<S, P>

declare function createReducer<S>(
defaultState: S,
...actionHandlers: ActionHandler<S, any>[]
): any

interface AppState {
dummy: string
}

const defaultState: AppState = {
dummy: ''
}

const NON_VOID_ACTION: ActionType<number> = 'NON_VOID_ACTION'
, VOID_ACTION: ActionType<void> = 'VOID_ACTION'

createReducer(
defaultState,
handler(NON_VOID_ACTION, (state, _payload) => state),
handler(VOID_ACTION, state => state)
)

// #25814

type R = {
a: (x: number) => void;
b: (x: string) => void;
};

type O = {
on<P extends keyof R>(x: P, callback: R[P]): void;
};

declare var x: O;
x.on('a', a => {});

// #29775

namespace N1 {

declare class Component<P> {
constructor(props: P);
}

interface ComponentClass<P = {}> {
new (props: P): Component<P>;
}

type CreateElementChildren<P> =
P extends { children?: infer C }
? C extends any[]
? C
: C[]
: unknown;

declare function createElement<P extends {}>(
type: ComponentClass<P>,
...children: CreateElementChildren<P>
): any;

declare function createElement2<P extends {}>(
type: ComponentClass<P>,
child: CreateElementChildren<P>
): any;

class InferFunctionTypes extends Component<{children: (foo: number) => string}> {}

createElement(InferFunctionTypes, (foo) => "" + foo);

createElement2(InferFunctionTypes, [(foo) => "" + foo]);

}

// #30341

type InnerBox<T> = {
value: T;
}

type OuterBox<T> = {
inner: InnerBox<T>
};

type BoxConsumerFromOuterBox<T> =
T extends OuterBox<infer U> ?
(box: InnerBox<U>) => void :
never;

declare function passContentsToFunc<T>(outerBox: T, consumer: BoxConsumerFromOuterBox<T>): void;

declare const outerBoxOfString: OuterBox<string>;

passContentsToFunc(outerBoxOfString, box => box.value);


//// [instantiateContextualTypes.js]
// #6611
function fn(values, value) {
}
fn(handlers, value => alert(value));
new GenericComponent({ initialValues: 12, nextValues: val => 12 });
useStringOrNumber("", foo => { });
const defaultState = {
dummy: ''
};
const NON_VOID_ACTION = 'NON_VOID_ACTION', VOID_ACTION = 'VOID_ACTION';
createReducer(defaultState, handler(NON_VOID_ACTION, (state, _payload) => state), handler(VOID_ACTION, state => state));
x.on('a', a => { });
// #29775
var N1;
(function (N1) {
class InferFunctionTypes extends Component {
}
createElement(InferFunctionTypes, (foo) => "" + foo);
createElement2(InferFunctionTypes, [(foo) => "" + foo]);
})(N1 || (N1 = {}));
passContentsToFunc(outerBoxOfString, box => box.value);
Loading