Skip to content

Bug: [@typescript-eslint/no-unnecessary-type-assertion] False positives when using non-null assertion operator for functions with potentially void return type #8906

@lukaVarga

Description

@lukaVarga

Before You File a Bug Report Please Confirm You Have Done The Following...

  • I have tried restarting my IDE and the issue persists.
  • I have updated to the latest version of the packages.
  • I have searched for related issues and found none that matched my issue.
  • I have read the FAQ and my problem is not listed.

Playground Link

https://typescript-eslint.io/play/#ts=5.3.3&fileType=.tsx&code=GYVwdgxgLglg9mABBOAnVBTaAbAngEQyi1jAHMA5DDAE1oEEBnRjVWBAdRigAsBVMHWAwwtAEpEQqMADUAhtgAUc1GQD8ALkRgQAWwBGrAJRadB1ogA%2BicEJG1EAbwBQiRDGCJlqo09dvETCgpJBUyAG5-AF9nGOdQSHYkFHQSPEJiaBFKajoaJhY2eDAuXgoQbGwJYOl5JTDNbT1DVBMm81QrJsq-Nw8vMN8XAMDJaUQwyLc4tyCQ7uxIuISshHdINEwcAiISbKpaBmZWJNKeGTgYGmqQuu91U2bjR46ugDdLml73T3uh-1mY1CqimiBicRQYEYUEQxGhAGUQBAIBhmC8WogALzITZpHaZUg5Q75Y5FTjcfiCDDCUTXIF3IwAQkikOhsNRUERyNRjAATOiLNiUlsoOldllyAc8gUTsUzuVKjdagpFEzIgB6dWIbAiYidRg8OAVGhgADkMOAchg2EQPFYGGcrJhcKgAFF0GgBZ1sSJhXiMntJbkjoVThSLlclbIVWrnJrEAAVeEAYS8fu2vkt1sYjoQbJdSeT7tQnvaGJ9G1S2wDEqJ0tJYd4EbpNWjSiMkWc2CIiDeKgTHIAjF73p9It2YX3UAPofyyxZrDpKuOe1OZ1AAMwj6y2an2GidtdDrHrdOi-GBush2XkpufKMMyJH2cns9ign7YMk0NyikKqr0jGT79hyG6vri1bioSUrXmSJQUgIdi0g%2BQHOPE4CrEgmAAI4gDAmA0OUHSKAA%2BowcC6BgRFem0HxXE4YJoTheEEURLSKM%2BUCDoyHZxlqBpGtgXxZtgzjMfhtBsawHEgdCg68WJGC4RJhFPKgMnThyvI8Rq-GGsaiAiYpymsWpGnrryCniaZxGcRuOl8YgAkGUZ1mSWZdkdkAA&eslintrc=N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6Jge1tiacTJTIAhtEK0ipWkOTJE0fJQ5N0UOdA7RI4MAF8QOoA&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYoEEkJemy0eAcgK6qoDCAFutAGsylBm3TgwAXxCSgA&tokens=false

Repro Code

function incorrectlyDetectingNeededAssertionWithVoidReturnVal(arg?: number): number | void {
  if (arg) {
    return arg;
  }
}

const testError: number = incorrectlyDetectingNeededAssertionWithVoidReturnVal()!;

ESLint Config

module.exports = {
  parser: "@typescript-eslint/parser",
  rules: {
    "@typescript-eslint/no-unnecessary-type-assertion": "error"
  },
};

tsconfig

{
  "compilerOptions": {
    "strictNullChecks": true
  }
}

Expected Result

The rule should correctly detect the type assertion as necessary since the function may return void and we are trying to assign the result of the function to a constant that is a number.

Actual Result

Linter fails as it detects the type assertion as unnecessary. By omitting the non-null operator, TSC (correctly) reports an error (which you can see in a line below)

Additional Info

The rule works as expected if the function potentially returns null or undefined - it only reports false positives if the function returns void. It also reports false positives when using class methods that potentially return void.

The rule also works correctly when checking the usage of non-null operator with variables / constants - it does not matter whether they are potentially null, undefined or void.

Metadata

Metadata

Assignees

No one assigned

    Labels

    accepting prsGo ahead, send a pull request that resolves this issuebugSomething isn't workinglocked due to agePlease open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing.package: eslint-pluginIssues related to @typescript-eslint/eslint-plugin

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy