打字稿使用条件类型推断构造函数参数
Typescript infer constructor parameter using conditional types
类似于如何通过类型推断使用 Typescript 推断函数参数:
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';
我想用同样的方法推断构造函数参数,但到目前为止还没有成功。目前我的设置如下所示:
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'
不确定 Typescript 是否支持此功能,因为 TS 文档中的 "Advanced Types" 部分仅提及函数而不是 类 用于推理(关于参数)。
还有其他人找到解决方案吗?
如果我在构造函数的 return 类型中将 T
更改为 any
,该示例有效:
type ConstructorProps<T> = T extends {
new (arg1: infer U): any;
// ^^^
} ? U : never;
记住,T
是构造函数的类型,和构造对象的类型不一样
class Test {
constructor(foo: number, baz: string) {}
}
type FirstConstructorProp<T> = T extends {
new (first: infer U, ...rest: any[]): any;
} ? U : never;
type F1 = FirstConstructorProp<Test>; // never
type F2 = FirstConstructorProp<typeof Test>; // number
type ConstructorProps<T> = T extends {
new (...args: infer U): any;
} ? U : never;
type P1 = ConstructorProps<Test>; // never
type P2 = ConstructorProps<typeof Test>; // [number, string]
如果你不使用大括号,它会起作用,请参阅其他 。
type ConstructorArgs<T> = T extends new(...args: infer U) => any ? U : never;
class Foo {
constructor(foo: string, bar: number) { }
}
type Bar = ConstructorArgs<typeof Foo> // type Bar = [string, number]
查看关联playground
Typescript 为此 ConstructorParameters<Type>
内置了实用程序类型
https://www.typescriptlang.org/docs/handbook/utility-types.html#constructorparameterstype
类似于如何通过类型推断使用 Typescript 推断函数参数:
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';
我想用同样的方法推断构造函数参数,但到目前为止还没有成功。目前我的设置如下所示:
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'
不确定 Typescript 是否支持此功能,因为 TS 文档中的 "Advanced Types" 部分仅提及函数而不是 类 用于推理(关于参数)。
还有其他人找到解决方案吗?
如果我在构造函数的 return 类型中将 T
更改为 any
,该示例有效:
type ConstructorProps<T> = T extends {
new (arg1: infer U): any;
// ^^^
} ? U : never;
记住,T
是构造函数的类型,和构造对象的类型不一样
class Test {
constructor(foo: number, baz: string) {}
}
type FirstConstructorProp<T> = T extends {
new (first: infer U, ...rest: any[]): any;
} ? U : never;
type F1 = FirstConstructorProp<Test>; // never
type F2 = FirstConstructorProp<typeof Test>; // number
type ConstructorProps<T> = T extends {
new (...args: infer U): any;
} ? U : never;
type P1 = ConstructorProps<Test>; // never
type P2 = ConstructorProps<typeof Test>; // [number, string]
如果你不使用大括号,它会起作用,请参阅其他
type ConstructorArgs<T> = T extends new(...args: infer U) => any ? U : never;
class Foo {
constructor(foo: string, bar: number) { }
}
type Bar = ConstructorArgs<typeof Foo> // type Bar = [string, number]
查看关联playground
Typescript 为此 ConstructorParameters<Type>
https://www.typescriptlang.org/docs/handbook/utility-types.html#constructorparameterstype