question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Feature request]type level equal operator

See original GitHub issue

original issue: #27024

Search Terms

  • Type System
  • Equal

Suggestion

T1 == T2

Use Cases

TypeScript type system is highly functional. Type level testing is required. However, we can not easily check type equivalence. I want a type-level equivalence operator there.

It is difficult for users to implement any when they enter. I implemented it, but I felt it was difficult to judge the equivalence of types including any.

Examples

type A = number == string;// false
type B = 1 == 1;// true
type C = any == 1;// false
type D = 1 | 2 == 1;// false
type E = Head<[1,2,3]> == 1;// true(see:#24897)
type F = any == never;// false
type G = [any] == [number];// false
type H = {x:1}&{y:2} == {x:1,y:2}// true
function assertType<_T extends true>(){}

assertType<Head<[1,2,3]> == 1>();
assertType<Head<[1,2,3]> == 2>();// Type Error

Checklist

My suggestion meets these guidelines:

  • This wouldn’t be a breaking change in existing TypeScript / JavaScript code
  • This wouldn’t change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn’t a runtime feature (e.g. new expression-level syntax)

workarounds

to summarize the discussion in #27024, the accepted solution was:

export type Equals<X, Y> =
    (<T>() => T extends X ? 1 : 2) extends
    (<T>() => T extends Y ? 1 : 2) ? true : false;

however there are many edge cases where it fails:

there were some other workarounds posted that attempted to address these problems, but they also had cases where they didn’t work properly.

what “equal” means

i think it’s important for it to treat structurally equal types as equal. for example { a: string, b: number; } should be considered equal to { a: string } & { b: number; } as they behave exactly the same

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:5
  • Comments:14 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
RyanCavanaughcommented, Mar 4, 2022
{ p: string | number; } should be considered equal to { p: string } | { p: number; }

👀

These types are very much different; T extends { p: string } ? true : false is true | false for one and false for the other

1reaction
geoffreytoolscommented, Jul 24, 2022

As much as I would like to have a reliable equality check, I have to agree with Ryan about people disagreeing on what should be equal.

For instance, I don’t think intersection of functions should be equal, because this operation is not commutative:

type A = ((a: number) => string) & ((a: 1|2|3) => 'Maria')
type B = ((a: 1|2|3) => 'Maria') & ((a: number) => string)

declare const fooA:A;
declare const fooB:B;

const barA = fooA(1) // string
const barB = fooB(1) // "Maria"

If you want an equal operator in order to catch bugs in your types, you will probably want Equal<A, B> to be false. If you want it for some other reason, maybe your definition of “equality” in that specific context will be different.

Concerning performance, if this kind of check only appears in a test suite, of course you want the said test suite to run fast, but it is not as critical as making the type checking of your production code fast, so I would not mind a slow workaround if it were reliable.

I have learned to use conditional types in times when using an intersection would have made logical sense, because intersections have lots of quirks. What would “equality” mean in an unsound type system?

Read more comments on GitHub >

github_iconTop Results From Across the Web

[Feature request]type level equal operator #27024 - GitHub
Here's a solution that makes creative use of the assignability rule for conditional types, which requires that the types after extends be " ......
Read more >
Advanced search reference - JQL operators - Atlassian Support
Your reference for operators that are used for advanced searching in Jira applications using Jira Query Language (JQL) in Jira Service Management.
Read more >
How AdWords API features are mapped to the Google Ads API
This guide aims to list each feature of the AdWords API, whether it's been ... In Google Ads bidding strategy type can only...
Read more >
about Comparison Operators - PowerShell | Microsoft Learn
Common features. String comparisons are case-insensitive unless you use the explicit case-sensitive operator. To make a comparison operator case ...
Read more >
NetSuite Applications Suite - query.Operator
Manufacturing Mobile for Production Operators · Collecting Mobile Data · Mobile Scanner Workflow · Manufacturing Mobile Work IDs · Recording an Assignment ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found