DEV Community

Ritik Bheda
Ritik Bheda

Posted on

Preparing for release 0.4

in the last post, we talked about the project, issue and about the online discussion. In this post, we will discuss about the difficulties ongoing and the progress after 1 week.

Points to be least Covered

  1. A type-level equivalence operator there.
  2. TypeScript type system is highly functional.
  3. Type level testing is required.
  4. However, we can not easily check type equivalence.

Difficulties

  1. how to compare to things where == operator is not there
  2. it is not simply comparing the values but comparing the type.

now since there is not == operator, for me the first question
was how do I compare it? I found on one of the reference mentioned by the issue Author (reference) that comparing at this level and comparing of types can be done by proper use extends. a code snippet for it is here:

(<G>() => G extends T ? 1 : 2)
Enter fullscreen mode Exit fullscreen mode

where G and T are type, we are comparing the type of T with G.

now, implementing that understanding of extends to a code that compares two types did take a lot of my time and the
implemented code look like this:

type isEqual<T, U> =
    (<G>() => G extends T ? 1 : 2) extends
    (<G>() => G extends U ? 1 : 2)
        ? true
        : false;
Enter fullscreen mode Exit fullscreen mode
  1. now this returns us a boolean but we need to have a function as our end user need a function to use. adding a assert function. adding a assert function did help me and find if the comparing gave out a positive response or no meaning are they equal or no. now, we know it is returning proper response when called, but now the question was how to call the assert function. this is also a very tricky part where I tried million trial and error functions to let my type isEqual to be able to call from tis to get the result. but unfortunately, I did not get any result. but what I did find is that using this assert function can help my task easier if I introduce one more function that I can finally export. So, the calling the assert function look like this
assertType<IsEqual<IsEqual<T, G>, true>>()
Enter fullscreen mode Exit fullscreen mode

where I had to call the isEqual twice.

  1. writing the final function to export This part was easier than the other parts above but it also gave me a good knowledge of typescript. I had to write functions with the same name of isEqualType and export them. writing functions this way was my first time experience and hence valuable for me. the find function looked something like this:
export function isEqualType<T, G>(): boolean;
export function isEqualType<T, G>(value : G): boolean;
export function isEqualType<T, G>(a: T, b: G){
    return assertType<IsEqual<IsEqual<T, G>, true>>();
} 
Enter fullscreen mode Exit fullscreen mode

I am now very close to completing the issue.

Top comments (0)