Skip to content

!. after ?. should be warned #35071

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

Open
falsandtru opened this issue Nov 13, 2019 · 4 comments
Open

!. after ?. should be warned #35071

falsandtru opened this issue Nov 13, 2019 · 4 comments
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@falsandtru
Copy link
Contributor

falsandtru commented Nov 13, 2019

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:

@kitsonk
Copy link
Contributor

kitsonk commented Nov 13, 2019

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

@falsandtru falsandtru changed the title Mixed !. and ?. should be warned !. after ?. should be warned Nov 13, 2019
@falsandtru
Copy link
Contributor Author

falsandtru commented Nov 13, 2019

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

@RyanCavanaugh
Copy link
Member

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

@falsandtru
Copy link
Contributor Author

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.

@RyanCavanaugh RyanCavanaugh added Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript labels Nov 13, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

3 participants