-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Never-initialized variable type is not narrowed to undefined #61496
Comments
That makes sense β I guess I would expect this part to be flagged by typescript-eslint rather than TS itself, however due to the lack of narrowing it is not flagged. |
Noting that this does have observable impact within TS, too, though, not just for tooling, for example: let alwaysUndefined: string | undefined;
let foo = alwaysUndefined?.toLocaleString(); // allowed (but bad code)
export {} let alwaysUndefined: string | undefined = undefined;
let foo = alwaysUndefined?.toLocaleString(); // TS ERROR
export {} |
When the |
### Changelog Added `@foxglove/no-never-initialized-let` (enabled by default). ### Docs Added to readme ### Description Adds a rule to fill a gap when `init-declarations` is not enabled. Related discussions: eslint/eslint#19581 & microsoft/TypeScript#61496 Copying from readme: > ### [`@foxglove/no-never-initialized-let`](./no-never-initialized-let.js) > > Disallow variable declarations that use `let` but have no intitial value and are never assigned. These variables will always be `undefined` and are likely a programmer error. > > The builtin [prefer-const](https://eslint.org/docs/latest/rules/prefer-const) rule doesn't flag these because they lack an initializer. Otherwise, they could be flagged by [init-declarations](https://eslint.org/docs/latest/rules/init-declarations), but this rule is mostly stylistic and has some implications for TypeScript type inference & refinement. (See [eslint/eslint#19581](eslint/eslint#19581) & [microsoft/TypeScript#61496](microsoft/TypeScript#61496) for more discussion.) > > Examples of **incorrect** code for this rule: > > ```ts > let prevX; > let prevY; > if (x !== prevX) { > prevX = x; > } > if (y !== prevY) { > prevX = x; // typo, should have been Y > } > ``` > > Examples of **correct** code for this rule: > > ```ts > let prevX; > let prevY; > if (x !== prevX) { > prevX = x; > } > if (y !== prevY) { > prevY = y; > } > ``` > >
π Search Terms
narrow uninitialized, uninitialized undefined
π Version & Regression Information
initialized
/uninitialized
β― Playground Link
https://www.typescriptlang.org/play/?ts=5.9.0-dev.20250326#code/MYewdgzgLgBAZiEMC8MAUUDuIBcNoBOAlmAOYCUeAbiEQCYoB8MA3gFAwwBEArhAKb4oxYFC4BuDjCkB6GTH4QANiSgBaOkQgBDAEZL+asPwAe6lcZgABKAE8ADouDF76xRagySRdXX7AlbQJtKCJwCCkDWHB+PEISUhgAHxgeMD84En46SSkiOHQYlGRULBByVilOORgAPQB+KpgagDlEeykAXzZutgQQNC4ucnEgA
π» Code
π Actual behavior
The type is not narrowed, which prevents typescript-eslint from flagging this comparison that always fails: typescript-eslint playground
π Expected behavior
The
one === two
comparison should be flagged because it always returns false (there is no wayone
has any value other thanundefined
).Additional information about the issue
Also filed a related issue with ESLint: eslint/eslint#19581
The text was updated successfully, but these errors were encountered: