【发布时间】:2018-09-23 06:28:47
【问题描述】:
在我正在处理的项目中,我使用符号来标记函数以确定代码是应该使用函数表达式本身还是调用它并使用返回值。目前看起来是这样的:
const shouldEvaluate: unique symbol = Symbol('evaluate');
interface FlaggableFunction extends Function {
[shouldEvaluate]?: boolean
}
虽然这可行,但当我在配置中启用声明时,我收到错误 semantic error TS4033 Property '[shouldEvaluate]' of exported interface has or is using private name 'shouldEvaluate'.。如果我导出这个符号,它会起作用,但是它也会被导出到编译的 JS 中,这是我不想要的。有没有办法只将符号导出到声明文件,同时在编译的 JS 中保持私有?
到目前为止,我已经尝试过分别声明类型和初始化变量
export declare let shouldEvaluate: unique symbol;
shouldEvaluate = Symbol('evaluate');
但这给了我一个错误,即必须使用 const 声明唯一符号。我还尝试在索引签名中给出FlaggableFunction,如下所示:
interface FlaggableFunction extends Function {
[key: unique symbol]: boolean
}
但这会引发TS1023 An index signature parameter type must be 'string' or 'number'.
【问题讨论】:
标签: typescript