例如:
const myFunc = () => 'result';
myFunc.myKey = 'value';
type func = () => string;
// AND
type func = {
myKey: string;
}
我将如何定义的类型myFunc
?
谢谢!
您可以使用接口来做到这一点,但该属性可能需要是可选的,以允许分配函数,然后分配属性:
interface FuncWithMyKey {
(): string;
myKey?: string;
}
const myFunc: FuncWithMyKey = () => 'result';
myFunc.myKey = 'value';
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句