Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
Rudxain opened this issue Jun 20, 2024 · 4 comments
Closed

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

Rudxain opened this issue Jun 20, 2024 · 4 comments
Labels
Duplicate An existing issue was already created

Comments

@Rudxain
Copy link

Rudxain commented Jun 20, 2024

🔎 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

@Rudxain Rudxain changed the title generic is "any-like" for relational operators Generic is "any-like" for relational operators Jun 20, 2024
@fatcerberus
Copy link

Technically both parameters aren't even guaranteed to be the same type, because T might be a union.

@RyanCavanaugh
Copy link
Member

See #52807; this is a rather breaky change to make and we're evaluating what the right course of action is. There are a lot of unconstrained (or insufficiently constrained) type parameters out there

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Jun 21, 2024
@Rudxain
Copy link
Author

Rudxain commented Jun 22, 2024

Should I close this issue (as dupe) and open one about the weird single-sidedness of symbols? Or should I keep it as-is?

@typescript-bot
Copy link
Collaborator

This issue has been marked as "Duplicate" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Jun 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants