我想创建一个接口,其中属性可以是string
或Function
必须返回的a string
。我目前有以下内容:
interface IExample {
prop: string|Function;
}
但这对我来说还不够明确,因为Function
允许返回任何东西。我想告诉编译器,返回值必须为string
。
在TypeScript中怎么可能?还是有可能吗?
type propType = () => string;
interface IExample {
field : string | propType;
}
class MyClass1 implements IExample {
field : string;
}
class MyClass2 implements IExample {
field() {
return "";
}
}
type PropertyFunction<T> = () => T;
interface IExample {
field : string | PropertyFunction<string>;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句