-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Closed
Labels
enhancement: new plugin ruleNew rule request for eslint-pluginNew rule request for eslint-pluginhas prthere is a PR raised to close thisthere is a PR raised to close thispackage: eslint-pluginIssues related to @typescript-eslint/eslint-pluginIssues related to @typescript-eslint/eslint-plugin
Description
The optional chain operator is built such that it will return undefined
if there is a nullish member.
Ending an optional chain expression with a non-null assertion is blatantly incorrect, and leads to type safety holes in your code, which will likely lead to runtime errors.
It is much better to either hoist the non-null assertion up the chain, so the thing you "know" is nullish is asserted on, instead of the entire expression.
function foo(x?: { a?: () => {} }) {
return x?.a!;
}
function bar(x?: { a?: () => {} }) {
return x?.a()!;
}
glen-84 and sindresorhus
Metadata
Metadata
Assignees
Labels
enhancement: new plugin ruleNew rule request for eslint-pluginNew rule request for eslint-pluginhas prthere is a PR raised to close thisthere is a PR raised to close thispackage: eslint-pluginIssues related to @typescript-eslint/eslint-pluginIssues related to @typescript-eslint/eslint-plugin