Closed
Description
TypeScript Version: 3.2 and up (possibly older ones too)
Code
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;
// To be sure the optional isn't the problem:
class InferFunctionTypes extends Component<{children: (foo: number) => string}> {}
createElement(InferFunctionTypes, (foo) => "" + foo); // ERROR! Parameter 'foo' implicitly has an 'any' type.
// In the above, P is properly infered on the createElement, per hover tooltip:
// function createElement<{ children: (foo: number) => string; }>(type: ComponentClass<{ children: (foo: number) => string; }>, ...children: ((foo: number) => string)[]): any
// If you don't expect P to be infered, it works properly:
createElement<{ children: (foo: number) => string }>(InferFunctionTypes, foo => "" + foo);
// Is unrelated to varargs
createElement2(InferFunctionTypes, [(foo) => "" + foo]); // ERROR! Parameter 'foo' implicitly has an 'any' type.
Expected behavior:
Typescript should be able to infer that foo
is a number
.
Actual behavior:
Parameter 'foo' implicitly has an 'any' type.
Related Issues: