将属性定义为在Typescript中重新生成字符串的字符串或函数

Seb*_*ald 5 declaration typescript tsd

我想创建一个接口,其中属性可以是a stringFunction必须返回a string.我目前有以下内容:

interface IExample {
  prop: string|Function;
}
Run Code Online (Sandbox Code Playgroud)

但这对我来说并不明确,因为Function它可以归还任何东西.我想告诉编译器返回值必须是a string.

这怎么可能在Typescript?或者它可能吗?

TSV*_*TSV 11

type propType = () => string;

interface IExample {
   field : string | propType;
}

class MyClass1 implements IExample {
    field : string;
}

class MyClass2 implements IExample {
    field() {
        return "";
    }
}
Run Code Online (Sandbox Code Playgroud)

更新1

type PropertyFunction<T> = () => T;

interface IExample {
   field : string | PropertyFunction<string>;
}
Run Code Online (Sandbox Code Playgroud)