-
Notifications
You must be signed in to change notification settings - Fork 12.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unable to use imported symbol as class property when exported inside an object #42830
Comments
Not sure about exports, but if you want to use symbol as key you need to use |
Interesting. Why does it work when the symbol is created in the same file then? The type What is the difference between a Also, when I try to assign the |
Ok, this is definitely a bug. I found these docs that describe doing what I am doing: // Lib
export const SERIALIZE = Symbol("serialize-method-key");
export interface Serializable {
[SERIALIZE](obj: {}): string;
}
// consumer
import { SERIALIZE, Serializable } from "lib";
class JSONSerializableItem implements Serializable {
[SERIALIZE](obj: {}) {
return JSON.stringify(obj);
}
} The I even tried pulling these properties out into a shared interface, in case that somehow impacts the "uniqueness" requirement here. Paradoxically it now complains about both:
|
Nevermind, this is a duplicate of an apparently longstanding lack of support for symbol indexers. Looks like I wont be using typescript for this library after all. |
I don't think that issue is relevant in you case. Not going into quirks of symbols support you can look at You need to preserve this uniqueness of symbols but as you declare them as just const _dynamo: symbol = storeSymbols._dynamo` it means any symbol. |
@IllusionMH I tried declaring them as unique symbols and it didn't change the error. That issue seems to be directly relevant: typescript does not support user defined symbols as indexable properties. If you have a potential solution for this I'm happy to try it out. |
As said before - you need to make sure that they are unique. |
I tried that. It doesn't work on the imported symbols. It works in the source file (the way the playground works) either way. You should try opening an editor and importing a unique symbol. It doesn't work. EDIT: ok... typescript is really picky about this. This does not work, when importing the symbols lose their uniqueness const _type = Symbol('_type')
const _dynamo: unique symbol = Symbol('_dynamo')
const _logger: unique symbol = Symbol('_logger')
export const storeSymbols = {
_type,
_dynamo,
_logger,
} But this works const _dynamo: unique symbol = Symbol('_dynamo')
const _logger: unique symbol = Symbol('_logger')
export { _dynamo }
export { _logger } |
UPD. Damn, GitHub didn't show your comment until I submitted this one. OK I see - symbols in objects are still widened to However option with assertion still works. ATM you have two options:
|
Why does providing the This is going to make it very awkward to structure the library exports. |
That's actually duplicate of #35562 |
Yep, nice find. |
Bug Report
imported symbols cannot be used as class properties. Symbols declared in the same file can.
π Search Terms
Element implicitly has an 'any' type because type has no index signature
typescript symbol element no index signature
β― Playground Link
This playground example cannot recreate the bug because it depends in importing the symbol
Playground link with similar but slightly different code
π» Code
π Actual behavior
TS Error
π Expected behavior
No errors
Here is the local error
Here is a screenshot with the error, next to the same code in the file that declared the symbol, where no error presents
The text was updated successfully, but these errors were encountered: