我想创建一个接口,其中属性可以是string
或必须返回Function
的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>;
}