typescript-eslint: [no-unnecessary-condition] False error on uninitialized variable

  • I have tried restarting my IDE and the issue persists.
  • I have updated to the latest version of the packages.
  • I have read the FAQ and my problem is not listed.

Playground

{
  "rules": {
      "@typescript-eslint/no-unnecessary-condition": [
      "error"
    ],
  }
}
let foo: string;

const setFoo = (): void => {
  if (foo?.length < 1) {
    foo = 'foo';
  }
};

tsconfig

{
  "compilerOptions": {
    "outDir": "./lib",
    "module": "commonjs",
    "target": "es2015",
    "lib": ["es6", "es7"],
    "sourceMap": true,
    "allowJs": false,
    "moduleResolution": "node",
    "forceConsistentCasingInFileNames": true,
    "noImplicitReturns": true,
    "noImplicitThis": true,
    "noImplicitAny": true,
    "strict": true,
    "strictNullChecks": true,
    "suppressImplicitAnyIndexErrors": true,
    "noUnusedLocals": true,
    "noErrorTruncation": true,
    "pretty": true,
    "declaration": true
  },
  "include": [
    "src/**/*"
  ]
}

Expected Result

Existence check on a possibly uninitialized variable should not trigger no-unnecessary-condition

For sure, the type might be extended to let foo = string | undefined; as workaround, but that actually looks a bit uncommon for a let definition, where not initializing might be intended.

Actual Result Unnecessary optional chain on a non-nullish value @typescript-eslint/no-unnecessary-condition

Versions

package version
@typescript-eslint/eslint-plugin 5.10.0
@typescript-eslint/parser 5.10.0
TypeScript 4.5.5
ESLint 8.7.0
ts-node 10.4.0

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 1
  • Comments: 16 (11 by maintainers)

Most upvoted comments

Note from #9206 that var is also a relevant case:

if (Math.random()) {
  var x = 1;
}
if (x) {}

Small adjustments to playground and I am able to reproduce.

I believe exactOptionalPropertyTypes=true is the secret.

Will open a separate issue so it can receive a separate investigation.

@JacobLey - that does not reproduce on the playground.

any such issues are unrelated to this issue and should have been fixed as part of #6635. please file a new issue with a repro and we can investigate.

Note from #9206 that var is also a relevant case:

if (Math.random()) {
  var x = 1;
}
if (x) {}

Fortunately, that’s a TS use-before-define error 🙂, so I think we can avoid the mental strain of thinking about var, unless we can think of another case where it specifically matters.

Across module boundaries there be Dragons. So I’m more than happy for us to fail the case!

that’s good idea to use scopes for that

there is only one issue with external variables, eg. exported from another file, but that should be even rarer case

this has to be fixed in typescript, uninitialized variables should be reported by typechecker as X | undefined not X or we should be able to receive information that variable is uninitialized in some way

https://www.typescriptlang.org/play?ssl=8&ssc=1&pln=1&pc=1#code/DYUwLgBGIM5gXBOAnAlgOwOYG4BQuAzAV3QGMxUB7dCADwgAoBKCAb1wk4mXCORuhwAdKCxgAFngC++WszxA

let test: string;
function x () {
    return test.length;
}
x();

this code is going to compile without error but, its actually going to produce runtime error