declare interface PayloadActionCreatorWithFnType<
P = void, R = void, T extends string = string> {
(arg: P): ReturnType<MyFnType<P, R, T>>;
type: T;
}
,用于提取函数类型的返回类型。像这样的conditional type:我正在尝试使用这样的可调用字段定义接口:
type MyFnType<P, R, T> = (arg: T) => Promise<R>;
declare interface PayloadActionCreatorWithFnType<P = void, R = void, T extends string = string> {
(arg: P): MyFnType<P, R, T>;
type: T;
}
问题是,上面定义了一个对象,当调用obj()
时,该对象将返回MyFunType
。我想要的是为该可调用函数设置TYPE,而不是为该函数设置返回类型。
我可以通过]实现相同的目的>
declare interface PayloadActionCreatorWithFnType<P = void, R = void, T extends string = string> { (arg: P): Promise<R>; type: T; }
// Real application const impl = (arg: string): Promise<string> => { return new Promise(res => {}); }; impl.type = 'type'; const obj1: PayloadActionCreator<string, string, string> = impl; // no ts error const obj2: PayloadActionCreatorWithFnType<string, string, string> = impl; // returns error
我想在上面的案例中使用FuncType,因为我想在其他地方重用该类型。
谢谢!
我正在尝试使用可调用字段定义接口,如下所示:type MyFnType
=(arg:T)=> Promise [我想您可能只是在寻找ReturnType<T>
实用程序类型:ReturnType<T>
该实用程序类型是declare interface PayloadActionCreatorWithFnType<
P = void, R = void, T extends string = string> {
(arg: P): ReturnType<MyFnType<P, R, T>>;
type: T;
}
,用于提取函数类型的返回类型。像这样的conditional type:好的,希望能有所帮助;祝你好运!
type ReturnType<T extends (...args: any) => any> =
T extends (...args: any) => infer R ? R : any;
declare interface PayloadActionCreatorWithFnType<
P = void, R = void, T extends string = string> {
(arg: P): ReturnType<MyFnType<P, R, T>>;
type: T;
}
,用于提取函数类型的返回类型。像这样的conditional type: