-
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
Unexpected type listed in error message #29861
Comments
I'm not sure what "working as intended" means. Are you saying that It really makes no sense to me why TypeScript would, in the second snippet, be able to determine/infer that It may be that this is a difficult issue or not worth the effort to fix, but I don't understand how "working as intended" is the appropriate label for its triage? |
@getify So when you perform the assignment, the type of String literal types get widened to their base type on assignment (except in some cases, for example assignment to |
My apologies. |
In |
Worth noting that that workaround won't work when elaborating from the top of a union, but it's likely good enough.™ |
@getify apologies there, I labelled this issue and then got interrupted while writing a response. Thanks for being patient. |
🤔 I'm starting to think TypeScript might want to report both the general primitive and narrow literal. Would it be confusing for an intermediate-to-advanced user that it sometimes reports one or the other? Maybe the error for these should be like
|
I'd have to see an example of where that would be useful - in that case, I'd say it's actually bound to confuse a user more than help. Even if we're trying to build intuition around singleton types existing for new users, I think it is a regression for intermediate-late users who might still struggle with our messages. Remember that sometimes our error messages are look like this, so helpful information needs to be actionable and help diagnose the problem. There's nothing about the string |
An error message saying ...but even more so for any of the non-string primitives, since they still have OTOH, I don't understand at all how having both pieces of information could ever possibly confuse someone, especially those more experienced ("intermediate-late") users. If you know about literal-types, then you know that |
When singleton types don't appear at the top-level of the target side, we'll now just simplify down to the the base primitive. Thanks @JoshuaKGoldberg! |
TypeScript Version: 3.2? (whatever version is currently on the playground)
Search Terms: "error message", "unexpected", "type", "is not assignable to"
Code
vs:
Expected behavior:
Expect both error messages to say: "Type 'string' is not assignable to type 'number'."
Actual behavior:
The error message in the first snippet says: "Type '"hello"' is not assignable to type 'number'."
The error message for the second snippet correctly says: "Type 'string' is not assignable to type 'number'."
Playground Link:
Unexpected type listed error message: https://www.typescriptlang.org/play/#src=var%20a%20%3D%203%3B%0D%0Aa%20%3D%20%22hello%22%3B
Expected type listed error message: https://www.typescriptlang.org/play/#src=var%20a%20%3D%203%3B%0D%0Avar%20b%20%3D%20%22hello%22%3B%0D%0Aa%20%3D%20b%3B
The text was updated successfully, but these errors were encountered: