10000 Bug: [no-unnecessary-type-assertion] false positive on type narrowing · Issue #11242 · typescript-eslint/typescript-eslint · GitHub
[go: up one dir, main page]

Skip to content
Bug: [no-unnecessary-type-assertion] false positive on type narrowing #11242
Closed as not planned
@cainlevy

Description

@cainlevy

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.8.3&fileType=.tsx&code=C4TwDgpgBAYgdlAvFA3gWAFBSgHgEpQQAewEcAJgM5SXABOAlnAOZQA%2BUArhRAGZMRyAPgAUASgBcUPAG5MAX0wBjAPZxaUYFPhIo4pEK49%2BcQXIyZe3JcAZqoAGxWUIAeToAZZxHFTajFnYoOE4AWwAjCDpUTGwGXj0AWQBDYAALADo6ZIoVUP0hZAAGDIBWMRisbCg6CGBOOgRgfWTqfyZmc2x5QgcXSuqauoam 73D7 8S6oRQxJoA&eslintrc=N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6Jge1tiacTJTIAhtEK0ipWkOTJE0fJQ5N0UOdA7RI4MAF8QOoA&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYoEEkJemy0eFYDArugDTg10NM8AOXapUAYQAW6aAGsylDul4BfECqA&tokens=false

Repro Code

type Fn = {
  <R extends string | undefined>(): R;
}
const t: Fn = () => undefined;

function loseOrLose(): string | number {
  if (Math.random() >= 0.5) {
    // no-unnecessary-type-assertion thinks the "as string" is unnecessary
    return t() as string;
  } else {
    // but without `as string` typescript fails
    return t();
  }
}

ESLint Config

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

tsconfig

{
  "compilerOptions": {
    "strict": true,
    "strictNullChecks": true
  }
}

Expected Result

I expected that as string would be allowed, since TypeScript fails without it.

Actual Result

no-unnecessary-type-assertion claims the as string is unnecessary

Additional Info

This is similar to #11054, but I do not know if it has the same root cause.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething 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-pluginworking as intendedIssues that are closed as they are working as intended

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0