Skip to content

Enhancement: [no-unsafe-*] misses any violations on union and object types #10158

Closed as not planned
@ronami

Description

@ronami

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.2&fileType=.tsx&code=PTAEDsHsFoFdwM4EMBmBTaSBOBzWBbNcAFwC4AocgEzQGMAbbNUWyRY0JU0Ab1BUiRuScAE9QAXwDclFPFrEAlm36CAFAA9ufAUNAJiWReBySAlNtV6DRk5N7lQoLGmKws4UBpkTKIUACqiKjM2HiEJKCQKKDEogAOzAAGOoLCYlKSSZwICIo44GhUsZCcoPHYSITEaFhRMXGJoClW3DbGOJkSSQB05LpqSGYy1HSMLixsBqAARumiANoAuqAAPhAEM7UjcuAKyp4z2Jpthh3Laxv4W1gW%2BmcmF%2Bvgm7UOTi5uHl4%2BfmBByHQnFwBCIHGisQSyRe1ze6xEiyW2SQuXyhWKxFKSHKlWqbwhjWhrzq63ajyRfSOWDUM2Gf30inwinG9HEAHdFMQABbNKBwYLoaCfdzgJKUGgMJiTdgsSy6eaSEZoDTxSBYDi7fYqLloej0SBqO6pawPUwSd6gfwAkLOVwi%2BqQppJBHZFBYSD4fjyJQqDnc21fTyE5pknC9RwB%2B1IX6jSUTVgyqjzJ5XG5KlVqjXeg6gNlq%2BhUQ2nWw4FMwm4Wq0C5jC74EqHNBHLV3uz2an2eP082tBhtJUPNvofO3fGY%2BIA&eslintrc=N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6Jge1tieQEMAZolp9kySgHMmAW0Qt0URNGgdokADThseIqWQVqdBszaduvQcOiJ8saEwWQlKtVoC%2BIN0A&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYoEEkJemy0eAcgK6qoDCAFutAGsylBm3TgwAXxCSgA&tokens=false

Repro Code

// no-unsafe-argument:

declare const a: { foo: any };

function foo(x: { foo: string }): { foo: string } {
  return x;
}

// Unsafe argument of type `{ foo: any; }` assigned to a parameter of type `{ foo: string; }`.
foo(a);

declare const b: any[] | number;

function bar(x: string[] | number): string[] | number {
  return x;
}

// Unsafe argument of type `number | any[]` assigned to a parameter of type `number | string[]`.
bar(b);

// similarly with `no-unsafe-return`

declare const c: { foo: any };

export function hello(): { foo: string } {
  // Unsafe return of type `{ foo: any }` from function with return type `{ foo: string }`.
  return a;
}

declare const d: any[] | number;

export function world(): string[] | number {
  // Unsafe return of type `any[]` from function with return type `number | string[]`.
  return b;
}

ESLint Config

module.exports = {
  parser: "@typescript-eslint/parser",
  rules: {
    "@typescript-eslint/no-unsafe-assignment": "error",
    "@typescript-eslint/no-unsafe-return": "error",
  },
};

tsconfig

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

Expected Result

I expected the rule to fail on these, similarly to how it fails on similar boxed values like Promise<any>, or any[], along with a combination of boxed values.

Actual Result

The code example is reporting no lint errors.

Additional Info

The no-unsafe-* rule family misses some edge case checks, mostly around boxed values (like arrays, objects, promises, etc.). While you get a warning for most any values wrapped in a box, I found some the rule misses around union and object types.

I made a simplified implementation to test-run on typescript-eslint's own repo and found one issue here (matching string[] | undefined with any[]).

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-pluginwontfixThis will not be worked on

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions