-
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
Interfaces that extend 'Symbol' cannot be used as 'symbol' type directly #46956
Comments
A wrapper object type( In your example, consider making the type InjectionKey<T> = symbol & T; |
@whzx5byb thanks for the info and suggestions 👏. type InjectionKey<T> = symbol & T; wouldn't be the same as interface InjectionKey<T> extends Symbol {}
So, it would be like type InjectionKey<T> = symbol; then the type becomes primitive, which makes extracting the generic parameter type impossible. I also found that the docs don't recommend using a generic type parameter with an unused type parameter, see here. So, this means the code I shared is wrong in many ways. But, I think it's the only way possible to create symbols that are paired with a specific type. See this playground for a use case, with two approaches. At this point, it looks like it's against the best practices, but the only way to solve a particular problem, at least the people at Vue, and also me after digging through this. So, I guess the maintainers could just close the issue because it's against their recommendation(which is fine), provide a legit alternative solution to this problem(it would be fantastic), or search for ways to create a legit way if it doesn't exist yet(i.e. they are willing to accept a feature request about this). |
Yeah, this code just isn't doing what you think it's doing. The type |
This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
@yusufkandemir the The reason that If someone is using Since this interface is only to be used with |
@pikax I appreciate the explanation. The main idea behind this was basically to have an I guess you are mainly referring to this:
which is correct in terms of "being wrong according to the TS best practices".
and similar supporting sentences. So, with the last answer I received from Ryan Cavanaugh(don't want to bother with a ping), the situation became clearer(I hope I understood it correctly 😛). The current implementation in Vue 3 is correct, because it just works and it's clean to use and understand, and there is no other way possible to achieve this, even though it looks like it's against TS best practices. But, I think it's safe to say that if this is the intended behavior of TS, and there is no other way of achieving such a thing (both of which seems to be the case), we can't really say the current implementation on Vue 3 is against the TS best practices. A best practice becomes invalid when it completely blocks us from achieving something. By the way, I love your work ❤️ |
Since Based on that I think is save to do something like: declare const InjectionSymbol: unique symbol
type InjectionKey<T> = symbol & { [InjectionSymbol]: T };
//@ts-expect-error it cannot be the same type
const foo = {foo: false} as InjectionKey<{foo: false}> The only issue, it works now but will it work in the future or will typescript support this type in the future? For example |
Bug Report
🔎 Search Terms
extends Symbol
extends Symbol index key
extending Symbol
InjectionKey
cannot be used as an index type
🕗 Version & Regression Information
⏯ Playground Link
Playground link with relevant code
💻 Code
🙁 Actual behavior
Interfaces that extend
Symbol
(e.g.InjectionKey
) can't be used assymbol
directly although they can be explicitly cast.🙂 Expected behavior
Interfaces that extend
Symbol
can be used assymbol
, which includes being used as index types.The text was updated successfully, but these errors were encountered: