Typescript: `!.` after `?.` should be warned

Created on 13 Nov 2019  ·  4Comments  ·  Source: microsoft/TypeScript

This is an anti-pattern on the current type system. And the compiler must not do quick fix so. !. after ?. is simply replaceable with ?. and should do so.


TypeScript Version: 3.7.x-dev.20191105


Search Terms:

Code

document.querySelector('_')!.textContent!.split('') // old code
document.querySelector('_')?.textContent!.split('') ?? 0 // string[]
document.querySelector('_')?.textContent?.split('') ?? 0 // string[] | 0

Expected behavior:

document.querySelector('_')!.textContent!.split('') // old code
document.querySelector('_')?.textContent!.split('') ?? 0 // unsafe, warning
document.querySelector('_')?.textContent?.split('') ?? 0 // safe

Actual behavior:

document.querySelector('_')!.textContent!.split('') // old code
document.querySelector('_')?.textContent!.split('') ?? 0 // unsafe, no warning
document.querySelector('_')?.textContent?.split('') ?? 0 // safe

Playground Link:

Related Issues:

Awaiting More Feedback Suggestion

Most helpful comment

This issue has meaning without adopting #35025 and meaningless if #35025 is adopted. So this issue and #35025 are in opposition. This is the definitive difference.

All 4 comments

Isn't this a just linting rule? Whenever you use ! it is potentially unsafe. The compiler never warns, an error is an error.

When quick fix observes this rule, it is wasteful splitting.

@falsandtru some discussion about what exactly is different between this and #35025 would be nice

This issue has meaning without adopting #35025 and meaningless if #35025 is adopted. So this issue and #35025 are in opposition. This is the definitive difference.

Was this page helpful?
0 / 5 - 0 ratings