Skip to content

Bug: [no-unnecessary-condition] false positives on type predicates with generic types #10142

@andreww2012

Description

@andreww2012

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.5.4&fileType=.ts&code=PTAEDECcHsFtQBYBckAcDOAuEBzAlkggK4BGAdAMZzDp4B2AJpAKbrSTHrBICeqzAWgBmrJMBIAbaCWAAJAKIBBACI1oRSBWY1mSAXWh19RCRICGk5mQZkk6ALAAoZgA9U7JKF79QAZV0AcoYBJuaWADwAQmbozAAqfMwANKAA0sw86KCuSMyMWQDWGdBCoNGxCT4AvKBFPCVlMfGJAHygNQDeTqCgANrpPKD0tcWl5c38ALqYaRnZLrn5s5ndPaAA-KBBdCGmFhLMUU2VzP0Zky2rPTPjJ2c8kwDcTgC%2Bz45Oru6QnlR06J4DDtQnh0Ah2qBwnEWgAKABuZgkRGYMzioAAPqA6KEMaAiIxmEJ6MwGABKGYIpHMIZZNFVNqU5GgACENWxpnenzcHlAfwBiBi212ElBCHAeGYEgYEKhKVS80WDEKo1A0Jhq2kACtUUlVnUZqldY5yaAtTS-IFgqF9oc4nK2vSsVbTKKYVrenVJqTHqAQJCBAInEA&eslintrc=N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6Jge1tiacTJTIAhtEK0yHJgBNK%2BSpPRgA2pETRoHaJAA0mKGQAW-ANYAVEogAK0RDLJD8KRfjiIwAXwC64DyHdA&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYoEEkJemy0eAcgK6qoDCAFutAGsylBm3TgwAXxCSgA&tokens=false

Repro Code

// From https://github.com/sindresorhus/type-fest/blob/HEAD/source/set-non-nullable.d.ts
export type SetNonNullable<BaseType, Keys extends keyof BaseType = keyof BaseType> = {
  [Key in keyof BaseType]: Key extends Keys
    ? NonNullable<BaseType[Key]>
    : BaseType[Key];
};

export const nonNullish = <T>(value: T | null | undefined): value is T => value != null;

export const hasNonNullishField = <T, K extends keyof T>(
  obj: T,
  key: K,
): obj is SetNonNullable<T, K> => nonNullish(obj[key]); // <--

ESLint Config

{
  "rules": {
    "@typescript-eslint/no-unnecessary-condition": ["error", { "checkTypePredicates": true }]
  }
}

tsconfig

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

Expected Result

The type of obj[key] in the example can be nullish and therefore the error should not be reported.

Actual Result

The error is reported.

Additional Info

No response

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-plugintriageWaiting for team members to take a lookworking 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

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy