From 887b30682bf377e28826fbeafc5abdbebf5e5e77 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Sun, 24 Mar 2019 11:34:14 -0700 Subject: [PATCH] Add tests --- .../compiler/instantiateContextualTypes.ts | 142 ++++++++++++++++++ 1 file changed, 142 insertions(+) create mode 100644 tests/cases/compiler/instantiateContextualTypes.ts diff --git a/tests/cases/compiler/instantiateContextualTypes.ts b/tests/cases/compiler/instantiateContextualTypes.ts new file mode 100644 index 00000000000..b81356fa91e --- /dev/null +++ b/tests/cases/compiler/instantiateContextualTypes.ts @@ -0,0 +1,142 @@ +// @strict: true +// @target: es6 + +// #6611 + +export interface A { + value: a; +} + +function fn(values: A, value: a) : void { +} + +declare let handlers: A<(value: number) => void>; +fn(handlers, value => alert(value)); + +// #21382 + +interface BaseProps { + initialValues: T; + nextValues: (cur: T) => T; +} +declare class Component

{ constructor(props: P); props: P; } +declare class GenericComponent + extends Component> { + iv: Values; +} + +new GenericComponent({ initialValues: 12, nextValues: val => 12 }); + +// #22149 + +declare function useStringOrNumber(t: T, useIt: T extends string ? ((s: string) => void) : ((n: number) => void)): void; +useStringOrNumber("", foo => {}); + +// #25299 + +type ActionType

= string & { attachPayloadTypeHack?: P & never } + +type Handler = P extends void + ? (state: S) => S + : (state: S, payload: P) => S + +interface ActionHandler { + actionType: ActionType

+ handler: Handler +} + +declare function handler(actionType: ActionType

, handler: Handler): ActionHandler + +declare function createReducer( + defaultState: S, + ...actionHandlers: ActionHandler[] + ): any + +interface AppState { + dummy: string +} + +const defaultState: AppState = { + dummy: '' +} + +const NON_VOID_ACTION: ActionType = 'NON_VOID_ACTION' + , VOID_ACTION: ActionType = '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

(x: P, callback: R[P]): void; +}; + +declare var x: O; +x.on('a', a => {}); + +// #29775 + +namespace N1 { + +declare class Component

{ + constructor(props: P); +} + +interface ComponentClass

{ + new (props: P): Component

; +} + +type CreateElementChildren

= + P extends { children?: infer C } + ? C extends any[] + ? C + : C[] + : unknown; + +declare function createElement

( + type: ComponentClass

, + ...children: CreateElementChildren

+): any; + +declare function createElement2

( + type: ComponentClass

, + child: CreateElementChildren

+): any; + +class InferFunctionTypes extends Component<{children: (foo: number) => string}> {} + +createElement(InferFunctionTypes, (foo) => "" + foo); + +createElement2(InferFunctionTypes, [(foo) => "" + foo]); + +} + +// #30341 + +type InnerBox = { + value: T; +} + +type OuterBox = { + inner: InnerBox +}; + +type BoxConsumerFromOuterBox = + T extends OuterBox ? + (box: InnerBox) => void : + never; + +declare function passContentsToFunc(outerBox: T, consumer: BoxConsumerFromOuterBox): void; + +declare const outerBoxOfString: OuterBox; + +passContentsToFunc(outerBoxOfString, box => box.value);