Skip to content

!. after ?. should be warned #35071

Description

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:

Activity

  1. kitsonk commented on Nov 13, 2019

    @kitsonk
    Contributor

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

  2. falsandtru commented on Nov 13, 2019

    @falsandtru
    ContributorAuthor

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

  3. RyanCavanaugh commented on Nov 13, 2019

    @RyanCavanaugh
    Member

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

  4. falsandtru commented on Nov 13, 2019

    @falsandtru
    ContributorAuthor

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions