[英]How to fix generic TypeScript function to return valid inferred type?
I have below TypeScript code ( link to playground ):我有以下 TypeScript 代码( 链接到操场):
type MyCallback<T> = (s: string, payload: T) => void;
interface IActions {
do1: MyCallback<number>;
do2: MyCallback<string>;
[key: string]: (s: string, payload: any) => void;
}
function convert<T extends { [key: string]: (s: string, payload: any) => void }>(callbackMap: T) {
const result: { [key: string]: <U>(payload: U) => void } = {};
Object.keys(callbackMap).forEach(key => {
if (typeof callbackMap[key] === 'function') {
result[key] = callbackMap[key].bind(null, "data");
}
})
return result;
}
const maps = convert<IActions>({
do1: (s: string, payload: number) => {
//
},
do2(s: string, payload: string) {
//
}
});
maps.do1(1); // valid
maps.smth("1"); // should be type-check error, but TS thinks it's valid
What I'm trying to do is to create a function, which accepts an object via an interface.我要做的是创建一个 function,它通过接口接受 object。 The function converts all methods from the object to a new object, where all methods have one fixed parameter (via
bind
method). function 将所有方法从 object 转换为新的 object,其中所有方法都有一个固定参数(通过
bind
方法)。 In other words, I want to convert this interface也就是说,我要转换这个接口
interface IActions {
do1: (state: string, payload: number);
do2: (state: string, payload: string);
.....
}
to至
interface IActions {
do1: (payload: number);
do2: (payload: string);
....
}
I want to make it generic, so it converts any interface based on generic parameter.我想让它通用,所以它转换任何基于通用参数的接口。
The issue with my current approach is that I don't have any intellisense and type checking for my maps
object.我当前方法的问题是我的
maps
object 没有任何智能感知和类型检查。
Is it possible to modify my convert
function in such a way, that return type is automatically inferred by the incoming interface?是否可以通过传入接口自动推断返回类型来修改我的
convert
function? In other words, I have full type checking and intellisense for return value ( maps
in my case).换句话说,我有完整的类型检查和返回值的智能感知(在我的例子中是
maps
)。
The fact that maps.smth
is valid is due to the explicit index signature on the result. maps.smth
有效的事实是由于结果上的显式索引签名。 What you need here is a mapped type to map over the properties of IActions
to a new type containing the modified methods.你需要的是一个映射到 map 的类型,而不是
IActions
的属性到一个包含修改后的方法的新类型。 To create the new method signature we can use a conditional type to extract the rest of the parameters (skip the first one)要创建新的方法签名,我们可以使用条件类型来提取参数的 rest(跳过第一个)
type MyCallback<T> = (s: string, payload: T) => void;
interface IActions {
do1: MyCallback<number>;
do2: MyCallback<string>;
}
function convert<T extends Record<keyof T, (s: string, payload: any) => void>>(callbackMap: T) {
const result: Record<string, (...a: any[]) => any> = {}
Object.keys(callbackMap).forEach(key => {
if (typeof callbackMap[key as keyof T] === 'function') {
result[key] = callbackMap[key as keyof T].bind(null, "data");
}
})
return result as {
[P in keyof T]: T[P] extends (s: string, ...p: infer P) => infer R ? (...p: P) => R : never;
};
}
const maps = convert<IActions>({
do1: (s: string, payload: number) => {
//
},
do2(s: string, payload: string) {
//
}
});
maps.do1(1); // valid
maps.do1("1"); //err
maps.smth("1"); // should be type-check error, but TS thinks it's valid
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.