-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Open
Labels
bugSomething isn't workingSomething isn't workingpackage: eslint-pluginIssues related to @typescript-eslint/eslint-pluginIssues related to @typescript-eslint/eslint-plugintriageWaiting for team members to take a lookWaiting for team members to take a look
Description
Before You File a Bug Report Please Confirm You Have Done The Following...
- I have tried restarting my IDE and the issue persists.
- I have updated to the latest version of the packages.
- I have searched for related issues and found none that matched my issue.
- I have read the FAQ and my problem is not listed.
Playground Link
Repro Code
declare const maybeUndefined: { optionalProp?: string } | undefined;
const r: Record<string, string> = { }; // try changing to Record<string, string | undefined>
const maybeUndefinedInAWayTSDoesntSee = r['will be undefined!'];
if (maybeUndefined && maybeUndefined.optionalProp === maybeUndefinedInAWayTSDoesntSee) {
// ^ only valid to convert to maybeUndefined?.optionalProp === maybeUndefinedInAWayTSDoesntSee if
// maybeUndefinedInAWayTSDoesntSee is NOT undefined.
}ESLint Config
module.exports = {
"rules": {
"@typescript-eslint/prefer-optional-chain": "warn"
}
}tsconfig
Expected Result
I expect that the report, which is highly dependent on type information for soundness, should generate a suggestion
Actual Result
An autofix was applied.
Additional Info
The use of autofixes rather than suggestions exacerbates bugs like #11700 and plays poorly with code that uses record/index/array types without noUncheckedIndexedAccess.
I propose to downgrade any autofix that is conditional on type information. Syntactic equivalents (or near enough) can remain autofixes for sure, such as const c = a != null ? a.b : undefined (which somehow we don't report??) can stay autofixes (if we actually have any non-type-dependent reports??)
We've done a similar process, eventually eliminating autofixes altogether, with strict-boolean-expressions. See #7312, #6173, #7743, #10136
Metadata
Metadata
Assignees
Labels
bugSomething isn't workingSomething isn't workingpackage: eslint-pluginIssues related to @typescript-eslint/eslint-pluginIssues related to @typescript-eslint/eslint-plugintriageWaiting for team members to take a lookWaiting for team members to take a look
{ "compilerOptions": { "strictNullChecks": true } }