我有以下代码。
interface MySecondInterface<A>{
type: A;
}
interface MyInterface {
(val1:string, val2:string): MySecondInterface<string>;
(): MySecondInterface<string>;
}
const myInterfaceFn: MyInterface = (value1: string, value2:string) => {
return {
type: value1 + value2
};
}
const myInterfaceFn2: MyInterface = () => {
return {
type: "Text"
};
}
你可以找到代码here https://www.typescriptlang.org/play/#src=%0D%0Ainterface%20MySecondInterface%3CA%3E%7B%0D%0A%20%20%20%20type%3A%20A%3B%0D%0A%7D%0D%0A%0D%0Ainterface%20MyInterface%20%7B%0D%0A%20%20%20%20(val1%3Astring%2C%20val2%3Astring)%3A%20MySecondInterface%3Cstring%3E%3B%0D%0A%20%20%20%20()%3A%20MySecondInterface%3Cstring%3E%3B%0D%0A%7D%0D%0A%0D%0Aconst%20myInterfaceFn%3A%20MyInterface%20%3D%20(value1%3A%20string%2C%20value2%3Astring)%20%3D%3E%20%7B%0D%0A%20%20%20%20return%20%7B%0D%0A%20%20%20%20%20%20%20%20type%3A%20value1%20%2B%20value2%0D%0A%20%20%20%20%7D%3B%0D%0A%7D%0D%0A%0D%0A%0D%0A%0D%0Aconst%20myInterfaceFn3%3A%20MyInterface%20%3D%20()%20%3D%3E%20%7B%0D%0A%20%20%20%20return%20%7B%0D%0A%20%20%20%20%20%20%20%20type%3A%20%22Text%22%0D%0A%20%20%20%20%7D%3B%0D%0A%7D。
我收到错误
Type '(value: string) => { type: string; }' is not assignable to type 'MyInterface'.
我将如何创建支持这两种方法签名的接口?
基本上,这是一个带有 2 个参数或不带参数的函数的接口。我怎样才能做到这一点?
使用怎么样type
代替interface
?
interface MySecondInterface<A>{
type: A;
}
type MyInterface = ((val1: string, val2: string) => MySecondInterface<string>) | (() => MySecondInterface<string>);
const myInterfaceFn: MyInterface = (value1: string, value2:string) => {
return {
type: value1 + value2
};
}
const myInterfaceFn2: MyInterface = () => {
return {
type: "Text"
};
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)