-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Walk all outer expression kinds before comitting to the return type of a symbol call #61071
base: main
Are you sure you want to change the base?
Walk all outer expression kinds before comitting to the return type of a symbol call #61071
Conversation
Doesn't |
Yes, I could control what it is supposed to handle with a bitmask but I decided that there is no harm to just use the default ( -const bar = Symbol() as string; // Conversion of type 'symbol' to type 'string' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.(2352)
+const bar = Symbol() as string; // Conversion of type 'typeof bar' to type 'string' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.(2352) |
@typescript-bot pack this |
Hey @RyanCavanaugh, I've packed this into an installable tgz. You can install it for testing by referencing it in your
and then running There is also a playground for this build and an npm module you can use via |
Is there a test for this? I don't see it in the PR. |
@jakebailey there is one now 😉 |
|
||
const testErrorMessage1 = Symbol() as string; | ||
~~~~~~~~~~~~~~~~~~ | ||
!!! error TS2352: Conversion of type 'typeof testErrorMessage1' to type 'string' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, this I'm not satisfied with (pun intended).
The complaint is about Symbol() as string
, but the fact that it's in the context of a const declaration, its type now changes. It seems very odd to me that if I were to write something like (() => Symbol() as string)()
that the message would be different.
I feel less weird about this error in the context of satisfies
, maybe, but even that I think is strange to be talking about the actual declaration name....
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, I changed it to walkUpOuterExpressions(node, OuterExpressionKinds.Parentheses | OuterExpressionKinds.Satisfies)
now
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks; can you also update the test with the other outer expression kinds, just to illustrate the differences? (Mainly just parens, satisfies
, maybe !
).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems very odd to me that if I were to write something like (() => Symbol() as string)() that the message would be different.
The type is already different between those 2 though:
const sym1 = Symbol();
const sym2 = (() => Symbol())();
I see your point though and I don't mind either solution ;p
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The types are different, absolutely, I just view const foo = Symbol()
to be a very specific syntactic construct, which this PR is saying should be extended to satisfies
.
That being said, #61070 is closed as "not a defect", so I'm not sure if we're actually going to accept this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That being said, #61070 is closed as "not a defect", so I'm not sure if we're actually going to accept this?
That's fine. I opened this PR before that :P Feel free to close it. It's obviously not a real-world problem, but I sympathized with the OP's view that this is surprising in the context of the satisfies
role. OTOH, Symbol()
is just special so 🤷
fixes #61070