Skip to content

Bug: [no-unnecessary-type-assertion] Poor error message for missing type parameter constraint #6951

Open
@JoshuaKGoldberg

Description

@JoshuaKGoldberg

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.0.3&sourceType=module&code=KYDwDg9gTgLgBDAnmYcAKBDWiDSBLGAZRmgwHNUBeOAbwCg44AbPAZxgB4AVOagVwB2AawEQA7gIB8ACgCUALnRQIAWzbAOAWQxgO7KHgFkANHC6TJAbjoBfa3QAmwAMZMsqZxAHs4DgEaKmNj4RCRQ5MD2oJCwcBisiALOcABmgs4weF7MwABuwEwA4sAwcrQMcFAlfFACcNIYYhgEvn4AdCzscrJxrHDauvqGJnAAqoYwABwAglDhiFa2dEA&eslintrc=N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6Jge1tiacTJTIAhtEK0ipWkOTJE0fJQ5N0UOdA7RI4MAF8QOoA&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYoEEkJemy0eAcgK6qoDCAFutAGsylBm3QAacDUh4AhpgDm6PBSjoSRdAA8VUgL4g9QA

Repro Code

export type PartyKitStorage = {
  list<T = unknown>(): Promise<Map<string, T>>;
};

declare const db: PartyKitStorage;

export async function levelGet() {
  return (await db.list()) as Map<string, Uint8Array>;
}

ESLint Config

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

tsconfig

{
  "compilerOptions": {
    "strict": true,
    "target": "esnext"
  }
}

Expected Result

The as assertion is changing the type of db.list from the default inferred list<unknown> to list<Uint8Array>. Saying "This assertion is unnecessary since it does not change the type of the expression." is incorrect.

I think ideally I'd want the lint complaint to indicate that the as should be replaced with an explicit <Uint8Array> on db.list... but I suspect that's quite a lot of work (what if there are many type parameters, and/or they have odd constraints?). So perhaps we should special case the rule's message for cases with type parameters? Not sure.

Actual Result

This assertion is unnecessary since it does not change the type of the expression.

Additional Info

Very similar to #5487 as filed by @srmagura.

Found in the wild trying to enable recommended-requiring-type-checking on https://github.com/partykit/partykit/blob/baa74442a567b303ef0f93edcf3baa6622e0a749/packages/y-partykit/src/storage.ts#L54-L57 (partykit/partykit#125 (comment)).

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