Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
192 views
in Technique[技术] by (71.8m points)

javascript - 在TypeScript中将符号用作对象键类型(Using symbol as object-key type in TypeScript)

I'm trying to define an object with a symbol as key-type since MDN says:(由于MDN表示,我试图将符号定义为键类型的对象:)

A symbol value may be used as an identifier for object properties [...](符号值可以用作对象属性的标识符[...]) But using it as type for the key-property:(但是使用它作为键属性的类型:) type obj = { [key: symbol | string]: string } results in the following error:(导致以下错误:) TS1023: An index signature parameter type must be either 'string' or 'number'.(TS1023:索引签名参数类型必须为“字符串”或“数字”。) even it can be used as index-type.(即使它可以用作索引类型。) I'm using the latest typescript version ( v3.7.2 ), related questions I've found:(我正在使用最新的打字稿版本( v3.7.2 ),发现了相关的问题:) Typescript: destructuring an object with symbols as keys (He's using an actual instance of a Symbol, I want the type symbol )(打字稿:使用符号作为键来破坏对象 (他使用的是Symbol的实际实例,我希望输入 symbol )) TypeScript: An index signature parameter must be a 'string' or 'number' when trying to use string |(TypeScript:尝试使用字符串|时,索引签名参数必须为“字符串”或“数字”) number() ES6: destructuring an object with symbols as keys (That can't be a solution - it seems kinda wrong to use an actual instance as type since every Symbol instance is unique...)(ES6:使用符号作为键来破坏对象 (这不是解决方案-因为每个Symbol实例都是唯一的,所以使用实际实例作为类型似乎有点错误。)) I've also took a look at the typescript symbol docs but they only show how it's used as value, not as type.(我也看过打字稿符号文档,但它们仅显示了它如何用作值,而不是类型。) Example:(例:) const obj = {} as { [key: number | symbol]: string // Won't work }; const sym = Symbol('My symbol'); obj[sym] = 'Hi'; Issue on Microsoft/TypeScript(关于Microsoft / TypeScript的问题) Open feature request(开启功能要求)   ask by Simon translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Unfortunately this is not possible at the moment in TypeScript.(不幸的是,目前在TypeScript中这是不可能的。)

If you have to interoperate with some APIs that expect this or really want to use symbols as keys, you can do this awkward version:(如果您必须与某些期望使用此API的API进行互操作,或者真的要使用符号作为键,则可以使用以下笨拙的版本:) // Ensure we can not pass regular map to our custom functions type SymbolMapTag = { readonly symbol: unique symbol } type SymbolMap = SymbolMapTag & { [Key in string | number | symbol]: string; } function set_symbol<T extends SymbolMap, TSym extends symbol> (target: T, sym: TSym, value: T[TSym]) { target[sym] = value; } function get_symbol<T extends SymbolMap, TSym extends symbol> (target: T, sym: TSym): T[TSym] { return target[sym]; } const symbol_map = {} as SymbolMap; const sym = Symbol('My symbol'); set_symbol(symbol_map, sym, "hi"); get_symbol(symbol_map, sym); // string type NonSymbolMap = { [Key in string | number]: string; } const non_symbol_map = {} as NonSymbolMap; set_symbol(non_symbol_map, sym, "hi"); // error get_symbol(non_symbol_map, sym); // error

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...