TypeScript 中的符号意义 [英] Sense of symbols in TypeScript
问题描述
TypeScript 中的符号是什么意思?来自 Java,与接口声明一起使用似乎有点多余.为什么我必须在 TypeScript 中设置符号?即以下代码段:
What is the sense of symbols in TypeScript? Coming from Java it seems a bit redundant to use together with an interface declaration. Why do I have to set symbols in TypeScript? i.e. following code segment:
export const RawProcessOptions = Symbol("RawProcessOptions");
export interface RawProcessOptions extends ProcessOptions {
}
推荐答案
我不知道是什么让您相信您必须遵循特定的编码模式;如果您对导致该模式的特定场景有疑问,请更新问题.通常,如果您不想使用字符串名称(例如,如果您想避免与字符串命名的属性发生冲突),则符号只是一种不同类型的键,您可以使用它来将属性附加到对象).这篇文章有一个不错的概述.Symbols 是一个 JavaScript 特性,不是 TypeScript 特有的(尽管它们在 TypeScript 中发现了一些额外的用于类型定义黑客的用途).TypeScript 为符号命名的属性提供类型检查,就像字符串命名的属性一样.
I don't know what leads you to believe you have to follow that particular coding pattern; if you have a question about a specific scenario that led to that pattern, please update the question. In general, a symbol is just a different kind of key you can use to attach a property to an object if you don't want to use a string name (e.g., if you want to be certain of avoiding conflicts with string-named properties). This article has a decent overview. Symbols are a JavaScript feature not specific to TypeScript (although they find some additional uses in TypeScript for type definition hackery). TypeScript provides type checking for symbol-named properties just like string-named properties.
这篇关于TypeScript 中的符号意义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!