-
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
Type guard "in" for undefined not working when using strictNullChecks + noUncheckedIndexedAccess #54241
Comments
Being tracked at #10530. The issue is that const abc: Record<string, string> = {};
function bar() {
const arg = "k"
if (arg in abc) {
const data = abc[arg];
// const data: string
}
} Until and unless #10530 is fully resolved, the most ergonomic way to do what you're doing is to give up on function baz(arg: string) {
const data = abc[arg];
if (data !== undefined) {
data;
// const data: string
}
} |
That issue is vaguely similar at best - the referenced issue refers to all (other) keys to be inferred as non-undefined and only for LITERAL strings. However here it's about a non-literal string which has explicitly been checked with |
Sigh, no it's the right issue, it's just that the description isn't quite specific enough anymore. I'd edit the title if I could. See #53454 for example. |
This was the entire concern of the feature in the first place when we implemented it. People wanted a sound and convenient method to check for array bounds or key inclusion automatically. And we said, "We don't have any way to do that since both index and map/array mutation can happen at almost any time," and everyone said, "No, that's fine. We can deal with the inconvenient but sound version, having anything is better than nothing for sure." So that's what we have, and indeed it is inconvenient in some cases. |
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
1 similar comment
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
@RyanCavanaugh I suppose there's an argument to be made that naming a feature "no unchecked indexed access" was misleading when it errors even in cases where the index is checked (via But yeah, this is a good case-in-point for why noUncheckedIndexedAccess isn't the default. |
Bug Report
type guard in undefined array indexed access
🔎 Search Terms
🕗 Version & Regression Information
⏯ Playground Link
https://www.typescriptlang.org/play?noUncheckedIndexedAccess=true&filetype=ts#code/FAYw9gdgzgLgBAQwEYgFxwEoFNwCcAmAPLLgJYQDmANCeRQHxwC8cA3gL4DcwwAZgK4QQMUpDhIEuABSJcFdLUpwAlG2BwNcUrzgzJFLREQoVazebjho8fAhgJmxkAG1ZBgLrdz7YD6A
💻 Code
🙁 Actual behavior
data is
string|undefined
despite it being typeguarded with "in" on aRecord<string, string>
which means that it cannot beundefined
🙂 Expected behavior
data
should bestring
The current bug causes either lots of redundant code (as we have to typeguard for undefined again separately) or disabling
noUncheckedIndexAccess
which would snooze tons of potential bugs though.The text was updated successfully, but these errors were encountered: