我正在尝试编写一个模板函数,即箭头函数,并将其分配给 const
变量,
这应该是 const method: MethodType<T> = <T>(...) => { ... }
的形式
但是当我尝试输入变量 method
时它会报错.以下是代码片段:
export type KeyMethod<T> = (data: T) => any;
export interface DiffResult<T> {
additions: T[];
updates: T[];
deletes: T[];
};
export type DiffMethod<T> = (oldState: T[], newState: T[]) => DiffResult<T>;
it complains about this template
vvv
export const diffMethod: DiffMethod<T> = <T>(oldState: T[], newState: T[]) => {
return {
additions: [],
updates: [],
deletes: []
}
};
有什么办法吗?也许我没有遵循语法,但我还没有找到类似的例子。
最佳答案
正如 Nitzan Tomer 的评论所暗示的,您应该像下面这样写。
export interface DiffResult<T> {
additions: T[];
updates: T[];
deletes: T[];
}
export type DiffMethod = <T>(oldState: T[], newState: T[]) => DiffResult<T>;
export const diffMethod: DiffMethod = <T>(oldState: T[], newState: T[]) => {
return {
additions: [],
updates: [],
deletes: []
};
};
关于Typescript 声明一个泛型函数并为其分配一个箭头函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43896386/