Similarlly to how you can infer function parameters with Typescript via type inference:
type FunctionProps<T> = T extends (arg1: infer U) => any ? U : never;
const stringFunc: (str: string) => string = (str: string): string => str;
type StringFuncType = FunctionProps<typeof stringFunc>;
const a: StringFuncType = 'a';
I want to infer constructor parameters the same way, yet have been unsuccessful so far. Currently my setup looks like this:
type ConstructorProps<T> = T extends {
new (arg1: infer U): T;
} ? U : never;
class Foo { constructor(a: string) { } }
type FooConstructor = ConstructorProps<typeof Foo>;
// FooConstructor is always never but should be type string.
const a: FooConstructor = 'a'
Wasn't sure if this was yet supported in Typescript as the "Advanced Types" section in the TS docs only mention functions and not classes for inference (in regards to parameters).
Anybody else find a solution to this?