admin管理员组文章数量:1327661
Say I have this function definition:
export type ErrorValueCallback = (err: any, val?: any) => void;
a standard callback interface. And I can use it like so:
export const foo = function(v: string, cb:ErrorValueCallback){
cb(null, 'foo');
};
But what if want to make this callback generic, something like this:
export type EVCallback = <T>(err: any, val: T) => void;
that syntax works, but when I try to use it:
export const foo = function(v: string, cb:ErrorValueCallback<string>){
cb(null, 'foo');
};
I get an error
ErrorValueCallback is not generic
how do I what I am looking to do?
Say I have this function definition:
export type ErrorValueCallback = (err: any, val?: any) => void;
a standard callback interface. And I can use it like so:
export const foo = function(v: string, cb:ErrorValueCallback){
cb(null, 'foo');
};
But what if want to make this callback generic, something like this:
export type EVCallback = <T>(err: any, val: T) => void;
that syntax works, but when I try to use it:
export const foo = function(v: string, cb:ErrorValueCallback<string>){
cb(null, 'foo');
};
I get an error
ErrorValueCallback is not generic
how do I what I am looking to do?
Share Improve this question asked Jul 10, 2018 at 23:35 user7898461user78984612 Answers
Reset to default 7You need to add the generic to the type type ErrorValueCallback<T>
Fixed example
export type ErrorValueCallback<T> = (err: any, val: T) => void; // FIX
export const foo = function(v: string, cb:ErrorValueCallback<string>){
cb(null, 'foo');
};
I think you wanted to use EVCallback instead
export type EVCallback<T> = (err: any, val: T) => void;
like this:
export const foo = function(v: string, EVCallback<string>){
cb(null, 'foo');
};
本文标签: javascriptTypeScript create generic callback typeStack Overflow
版权声明:本文标题:javascript - TypeScript create generic callback type - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742228683a2436809.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论