-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Asserting changes type of original variable inline (the change reverts in subsequent lines) #58055
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
Comments
Note that the type of So it's at most an issue with the type shown in the tooltip. |
thanks @MartinJohns I have clarified the bug report title to make it clear that it is on the asserted line only. Whether the issue is with the type shown in the tooltip or not is beyond my understanding of the typescript codebase. Downstream tools like eslint which rely on the typescript types report an error as if the type of |
Duplicate of #43873 |
We need to retain basically both the function someFunction<T extends keyof typeof x>(prop:T): T {
switch (prop) {
case 'y':
return prop; Note that const r1: 'y' = prop; also doesn't error. The "correct" type is both |
The issue is that from typescript-eslint's perspective for the rule The way we do this is in the most basic form - "are the type objects the same?" Not even doing any fancy type assignability checks - it all just boils down to So in this particular case when we inspect the left we don't see the type There's no flags set on the left type either so we can't even guess that the left might be the generic type. The contextual type too is the same. Is there another API we can use to get the type of the left without the assertion here (eg see that it is the generic type)? |
To clarify - it's not just what you show to the user - this is not a problem of intellisense has to display one so we pick the best one via some logic. Instead it's what the type API itself reports for the expression. When there is no assertion it reports the type |
I would argue that, if this triggers const r1: 'y' = prop; is already legal as Ryan suggests. |
I suppose there'd be some issue if it became function someFunction<T extends keyof typeof x>(prop: T) {
if (prop === "y") {
x[prop].toUpperCase(); // error
const p: "y" = prop;
x[p].toUpperCase(); // okay
const q: T & "y" = prop; // error!
x[q].toUpperCase(); // this would have been okay
}
} |
@jcalz my using of a @RyanCavanaugh needing to keep the return type broad to enable returing T makes sense IF statement example: ![]() |
This issue has been marked as "Not a Defect" and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
Uh oh!
There was an error while loading. Please reload this page.
π Search Terms
asserting type
as
changing underlying typeπ Version & Regression Information
β― Playground Link
https://www.typescriptlang.org/play?ts=5.4.3#code/MYewdgzgLgBAHjAvDA3gWAFAxgcgJ44BcuAFgKYA2FIOANJtjnETAIwAMmAvppgGYBXMMCgBLcDAggAtmQBiQkeLAAeACowycKGTAATCDADWZPCD4woeAA5lz8AHwAKawCcQ1wmoCUqBpIB3UShgEhgXd2tfdCxsGGAAQwgyXAJCfziYAHosmATYKBJRQ2sQUTBYNw8YYskxKhgAIjxGmAAfJrhWvQEUqBBLGzIIYFdRa1gwEFgAIzJygHM8mYo+gbAE13cAjLiczQgKcthXAVWmqYBaITJgYYhNvEurW0uk5NcxcFa3MgA3XRQQwbLYgIJgJYgASuZIUAEQXbYUCQE6sJAwKrWADciOyuUymQAegB+XHI6AwVwAJnRmLyhnwOBxsUy+wJcRJuJmrjICSMzOwPAw3EwQA
π» Code
π Actual behavior
If you inspect the types of prop with and without the assertion you can see that without the assertion TS reports the type of prop to be T extends "y" | "x" but with the assertion TS reports the type of

prop
as"y"
.This is a bug with TS itself - a behaviour changed in TS v4.3. In v4.2 TS correctly reports both locations as the generic - in v4.3+ it exhibits the above behaviour.
π Expected behavior
The assertion should not be changing the type of the original variable.
Additional information about the issue
Discovered while debugging a eslint issue .
The text was updated successfully, but these errors were encountered: