Skip to content

Bug: [no-unnecessary-type-assertion] False report with an 'as' assertion #10257

@FleetAdmiralJakob

Description

@FleetAdmiralJakob

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.6.2&fileType=.tsx&code=MYewdgzgLgBNCGUCmAuGAiJAPADgSwCckATdGAHwxyTGLzAHMyBeKmux9AbgCgeAzAK5hgUPOBgBbePQAUAShgBvHjBhEoggmGWq1MKAE9qaAAYASJQmQBfAEpIAjoKTRTMeBBgWlR6iH44KEQkeycXNz0bXhsgA&eslintrc=N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6Jge1tiacTJTIAhtEK0ipWkOTJE0fJQ5N0YANrhsOOdA7RImgLoaAviGNA&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYoEEkJemy0eAcgK6qoDCAFutAGsylBm3TgwAXxCSgA&tokens=false

Repro Code

const state: "expired" | "pending" = "pending";

function main() {
  return {
    type: `${state}Request` as `${typeof state}Request`
  };
}

ESLint Config

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

tsconfig

{
  "compilerOptions": {
    // ...
  }
}

Expected Result

The code should be fine as the type assertion changes the type from string to "expiredRequest" | "pendingRequest"

Actual Result

There was an error 5th line saying:

This assertion is unnecessary since it does not change the type of the expression. 5:11 - 5:56

Additional Info

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    accepting prsGo ahead, send a pull request that resolves this issuebugSomething isn't workingpackage: eslint-pluginIssues related to @typescript-eslint/eslint-plugin

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions