-
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
Const primitive value type getting lost? #48402
Comments
Another way to express the issue (I think this is the same issue at least): const TYPE_1 = 'TYPE_1';
const TYPE_2 = 'TYPE_2' as const;
const action1 = { type: TYPE_1 };
const action2 = { type: TYPE_2 };
action1.type = 'string'
// Why does this throw? It's not a readonly prop, so it should have inferred 'string'
action2.type = 'string' |
I'm not 100% sure this is the case, the playground hints that type of
I may not understand correctly what it means for a type to be widening, but it seems a rather inconsistent behaviour if working as intended. I would've expected the type won't change depending on the use case |
What you're ultimately observing is the difference between: const action1 = { type: "foo" }; // { type: string }
const action2 = { type: "foo" as const }; // { type: "foo" } It is a bit odd that the widening flag is apparently being preserved in the type of a |
This issue has been marked as 'Question' and has seen no recent activity. It has been automatically closed for house-keeping purposes. If you're still waiting on a response, questions are usually better suited to stackoverflow or the TypeScript Discord community. |
@RyanCavanaugh that felt a bit rough :D It's not a question, and doesn't belong to stackoverflow. I'd vastly appreciate if you could kindly reopen it. I'm happy to accept suggestions to issue title/description changes, if they are confusing. |
@TeoTN the difference in behavior, which is intentional, was correctly described by @fatcerberus above. There is a difference between the widening and non-widening forms of literals, which isn't displayed in the UI (we discussed this and decided that it would be more confusing, rather than less, on net). |
@RyanCavanaugh I find it odd that the “widening literal” flag gets preserved through a |
The intuition is that this program should not have an error: const a = "foo";
const b = { prop: a };
b.prop = "bar"; The widening rules for literals were largely derived empirically, since code like this predates the existence of literal types. One way to think about it is that it should be extremely difficult to get yourself into a literal type mismatch if none of your code contains either an If I'm misunderstanding your observation let me know; happy to clarify. |
I guess that makes sense. I think I would rather have |
Bug Report
It seems to me that type for primitive value assigned to a const variable gets widened in some contexts - I'm not 100% sure if this is a bug report or feature request, but this is a bit counterintuitive. Is this an expected behaviour?
(From what I saw in the playground hints, primitive values assigned to
const
behave as if they hadas const
modifier)🔎 Search Terms
primitive value
primitive widening
⏯ Playground Link
Playground link with relevant code
💻 Code
The hint shows the
TYPE_1
is not string:🙁 Actual behavior
Type guard wasn't able to narrow down a type because field type got widened to
string
.🙂 Expected behavior
Both examples should pass
The text was updated successfully, but these errors were encountered: