gpt4 book ai didi

typescript - 在 TypeScript 中导出符号声明

转载 作者:搜寻专家 更新时间:2023-10-30 21:08:13 24 4
gpt4 key购买 nike

在我正在进行的一个项目中,我使用符号来标记函数以确定代码是应该使用函数表达式本身还是调用它并使用返回值。目前它看起来像这样:

const shouldEvaluate: unique symbol = Symbol('evaluate');

interface FlaggableFunction extends Function {
[shouldEvaluate]?: boolean
}

虽然这有效,但当我在我的配置中启用声明时,我收到错误 语义错误 TS4033 导出接口(interface)的属性“[shouldEvaluate]”具有或正在使用私有(private)名称“shouldEvaluate”。。如果我导出这个符号它会起作用,但是它也会导出到编译的 JS 中,这是我不想要的。有没有办法只将符号导出到声明文件,同时在编译后的 JS 中保持私有(private)?

到目前为止,我已尝试分别声明类型和启动变量

export declare let shouldEvaluate: unique symbol;
shouldEvaluate = Symbol('evaluate');

但这给了我一个错误,必须使用 const 声明唯一符号。我还尝试在索引签名中提供 FlaggableFunction,如下所示:

interface FlaggableFunction extends Function {
[key: unique symbol]: boolean
}

但这会抛出 TS1023 索引签名参数类型必须为“字符串”或“数字”。

最佳答案

Is there a way export a symbol only to the declaration file while keeping it private in the compiled JS?

当您执行 export declare let shouldEvaluate: unique symbol; 时,它无论如何都会公开。

所以不,您不能使用属于公共(public)类型的私有(private)变量。

您可以将所有内容保密。但这不是你想要的。

关于typescript - 在 TypeScript 中导出符号声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49807788/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com