Skip to content

Generic is "any-like" for relational operators #58938

@Rudxain

Description

@Rudxain

🔎 Search Terms

generics templates comparison compare less-than more-than unknown unsound function inline

🕗 Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about relational operators and comparisons

⏯ Playground Link

https://www.typescriptlang.org/play/?noUncheckedIndexedAccess=true&noUnusedLocals=true&noUnusedParameters=true&target=10&jsx=0&useUnknownInCatchVariables=true&exactOptionalPropertyTypes=true&noFallthroughCasesInSwitch=true&noImplicitOverride=true&noPropertyAccessFromIndexSignature=true&suppressImplicitAnyIndexErrors=true&ts=5.6.0-dev.20240619&noStrictGenericChecks=true#code/MYewdgzgLgBMC2AHGBeGAeAKgGgHwAoAPALhhxgE9TMBKVXGQjSgWACh2wBXAGx+e592CRPkE9sMcTXbsuYACYBTAGYBLMEoXN5y9ZoXCk+Xao1bJp-Vpkc2AZQrwARiB7466GI5duPR0R9XdxpJIL8aWyA

💻 Code

const cmp = <T,>(x: T, y: T) => x < y

null < null
cmp(null, null)

undefined < undefined
cmp(undefined, undefined)

Symbol() < Symbol()
cmp(Symbol(), Symbol())

🙁 Actual behavior

Error messages:

  1. "The value 'null' cannot be used here.(18050)"
  2. "The value 'undefined' cannot be used here.(18050)"
  3. "The '<' operator cannot be applied to type 'symbol'.(2469)"

The errors show up for both operands of the relational operator except symbols, which only error on LHS.

The arrow fn cmp doesn't trigger any errors, even at call sites

🙂 Expected behavior

cmp should trigger an error on definition, because even though both params are same-type, they can't always be compared (symbols throw at runtime)

Additional information about the issue

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions