Skip to content

Bug: [no-unsafe-type-assertion] Fails to report unsafe assertions involving constrained type parameters #10453

Closed
@controversial

Description

@controversial

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

Playground link

Repro Code

type Object = {
  foo: string;
}

export function myfunc<CustomObjectT extends Object>(input: CustomObjectT): CustomObjectT {
  const newObject: Object = { foo: 'hey' };
  // This is UNSAFE because CustomObjectT can/will be instantiated with a narrower subtype of Object
  // no-unsafe-type-assertion should (but doesn’t) catch this
  const newCustomObject = newObject as CustomObjectT;
  return newCustomObject;
}

ESLint Config

{
  "rules": {
    "@typescript-eslint/no-unsafe-type-assertion": "error"
  }
}

tsconfig

{
  "compilerOptions": {
    "strict": true
  }
}

Expected Result

The line with the type assertion involving the generic parameter (newObject as CustomObjectT) should report @typescript-eslint/no-unsafe-type-assertion by the same logic wherein Typescript considers such a conversion unsafe:

2322: Type 'Object' is not assignable to type 'CustomObjectT'.
  'Object' is assignable to the constraint of type 'CustomObjectT', but 'CustomObjectT' could be instantiated with a different subtype of constraint 'Object'.

The first example in the playground shows that this case is not an allowable assignment according to TS, which, to my understanding, should be the heuristic used by no-unsafe-type-assertion

Actual Result

No error is reported from @typescript-eslint/no-unsafe-type-assertion

Additional Info

cc @ronami

thanks for your hard work on this rule!

Metadata

Metadata

Assignees

No one assigned

    Labels

    accepting prsGo ahead, send a pull request that resolves this issuelocked 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-plugintriageWaiting for team members to take a look

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions