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
[no-unsafe-member-access] Add exception for optional chaining #2728
Comments
The unsafety is that you're operating on an Optional chaning makes it marginally more safe in that you won't get a runtime error for null/undefined - but it's still not safe from a types perspective. In your specific code sample it's "safe-ish" because you're assigning to a variable explicitly typed as |
Hey, @bradzacher. Thanks for the quick response! I get what you're saying, but that shouldn't be the responsibility of this rule. We have other rules (no-unsafe-assigment/call/return) that govern the usage of It sounds like you're concerned that I could do something with that const getEvent = (): { detail: { uuid: any } } => ({
detail: { uuid: null },
});
const logString = (t: string): void => {
console.log(t);
}
const handleEvent = (): void => {
logString(getEvent().detail.uuid);
} Now, ignore the
But no linter flags are raised inside the My question is how is that different from my original example? Or even simpler, how is I hope that made sense. Curious to hear your thoughts. |
This is worth its own issue being filed, of course, but it makes sense to me to resolve this discussion first. |
Your example is just not handled by any rule right now. In the ESLint world, you can't assume anything about the user's config. You can't assume that a user has rule X turned on because they're using rule Y - each rule is entirely isolated from one-another. Which is why this rule just assumes you're using it in isolation. If you want to add a flag (default false) which allows the rule to ignore optional chains on |
Sure. But my main point is that Anyway, I'll make a note to try to implement this myself. Thanks for the feedback and the link to your comment about |
Repro
Expected Result
I'd like to not have an error when accessing
event.detail?.uuid
, either by default or by special rule config, because I'm not seeing what is unsafe about this member access. I've already satisfied theno-unsafe-assignment
rule by declaringuuid
to be of typeunknown
, and with optional chaining, it should not be possible to get a runtime error here, no matter what the value ofevent.detail
actually is.Actual Result
The
.uuid
access yielded:Versions
@typescript-eslint/eslint-plugin
4.5.0
@typescript-eslint/parser
4.5.0
TypeScript
4.0.3
ESLint
7.11.0
node
14.12.0
The text was updated successfully, but these errors were encountered: