Typescript Test Your Generic Type Part 2
Tags: webdev, tutorial, testing, typescript
Published: 2022-06-29 13:29:17

read the part 1 [here](https://dev.to/tylim88/typescript-test-your-generic-type-part-1-4jbc)

now we know how to create tests for our type, but it is not done yet, there are edge cases that we need to take care of, and we need to understand some theories before we can solve them.

now take a look again at our Expect type

```ts
type Expect<T, U>= T extends U ? U extends T ? true : false : false
```
notice the `T` and `U`?

they are known as **naked type parameters**.

You can think of them as some kind of variable.

so if there are naked type parameters, then there are also non-naked type parameters and they look like this, examples:

1. keyof T
2. U[]
3. [U]
4. SomeGeneric< T >

naked parameter distributes over a union, while non-naked and types do not distribute over a union(just like normal types), to understand this, let's take a look at the code below

normal:
```ts
type A = "a" | "b" extends "a" ? true : false // false 
type B = "a" extends "a" | "b" ? true : false // true 
```
pretty straightforward

`"a" | "b"` cannot be narrowed down from `"a"`, because `"a" | "b"` is wider, so `"a" | "b"` could not extend `"a"` and vice versa

naked parameter:
```ts
type C<T,U> = T extends U ? true : false
type r1 = C<"a" | "b", "a"> // boolean
type r2 = C<"a", "a" | "b"> // true
```

in the case of r1, `"a" | "b"` try to distribute over `"a"`: `("a" extends "a"? true : false) | ("b" extends "a"? true : false)`

 `"a"` extends `"a"` is `true`, `"b"` extends `"a"` is `false`, in the end, we get `true | false` which is equivalent to `boolean`

in the case of r2, `"a"` is not a union, so there is no distribution, and since `"a"` can be narrowed down from `"a" | "b"`, hence we get `true`

[playground](KYAJwFdoAuKAMxQBtzoB6fpx58oAKFCQoAIQRU8hEhXmYc6OoxZR2XXgKGboYieGgBhADwAVADQBVAHxyrUAkTKU7teszbC9JlIMAIxylqjoWEi4NvJOglDYAPZJ3BAoxIHQDABMYRYRsRGq0UjxQsmp6cRAA)

By this point, we know what could go wrong with Expect, this time let us use [Odd Number Type](https://dev.to/tylim88/typescript-odd-number-type-1fj) as our test subject

```ts
type OddNumber<
    X extends number,
    Y extends unknown[] = [1],
    Z extends number = never
> = Y['length'] extends X
    ? Z | Y['length']
    : OddNumber<X, [1, 1, ...Y], Z | Y['length']>

type Expect<T, U> = T extends U ? (U extends T ? true : false) : false

// eslint-disable-next-line @typescript-eslint/no-unused-vars
export const assert = <T extends true>() => {
	//
}

assert<Expect<OddNumber<5>,1 | 3 | 5>>() // true, pass test
// @ts-expect-error
assert<Expect<OddNumber<5>,1>>() // false, fail test
```
![type test 1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nsy0iqw0oeo6eom1khlj.png)

[playground](https://www.typescriptlang.org/play?#code/C4TwDgpgBA8gJnAcgVwLYCMICcA8AoKQqADSggA9gIA7OAZymrUywBoCiBNMym+qZNQDW1APYB3agG0AulAC8UKQEYZ7IlABaPKrQZMM2BYwgA3bHgB8xzlIDkAGxoBzYAAs7cirv7EOhAH4tKAAfKFtHF3dPfygALlgEFENcYlYlZXTMqAA6PM41YLCIp2pXDxlLPDxQSCgAUXJIAGNgHAAVdIBVa0V2nT4GLqgggAph70GofqDgLGRoBIAzAEMHOggASnioVfWIaoB6Q7I6BwBLamAAWjhzuhX0J2vqb2uL16gAAVqIOmasOcwDc-h9gIcxNdBMgNnBrqYVlg6HgKGBRFhgFBmqJqHRMSs6BsMcYOgM9FA5gtLKNtvJrABvPAASGOeAAvtUCUS2o0Wm14EhmNgcABWSysZShKAAZilYup22OFPmEHSYC5FL+wDwSp+dGuqIgrQNWCw6LwXOwPKaRv5SSFuDFEssCqgSr2G3Sq3ODk1eOq1SAA)

as expected, it does not work.

so how can we fix it?

Actually the hint is already out there: non naked type parameters.

The solution is to convert the naked type parameter to the non-naked type parameter and the safest way is by turning them into arrays.

```ts
type Expect<T, U>= T[] extends U[] ? U[] extends T[] ? true : false : false
```

let's try again:
![type test 2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q2hcna7sr637t86us9ux.png)

[playground](https://www.typescriptlang.org/play?#code/C4TwDgpgBA8gJnAcgVwLYCMICcA8AoKQqADSggA9gIA7OAZymrUywBoCiBNMym+qZNQDW1APYB3agG0AulAC8UKQEYZ7IlABaPKrQZMM2BYwgA3bHgB8xzlIDkAGxoBzYAAs7cirv7EOhAH4tKAAfKFtHF3dPfygALlgEFENcYlYlZXTMqAA6PM41YLCIp2pXDxlLPDxQSCgAUXJIAGNgHAAVdIBVa0V22R0+Bi6BoIAKEa9ePSh+uSDgLGRoBIAzAEMHOggASnioDa2IaoB6E7I6BwBLamAAWjgrunX0Jzvqbzvrj6gAAVqIHRmlgrmB7oDvsATmI7oJkNs4HdTOssHQ8BQwKIsMAoM1RNQ6Dj1nRttjjB1BjNFstLGM9vJrABvPAASDOeAAvtViaS2o0Wm14EhmNgcABWSysZShKAAZhlEtpezOUGpEHSYB5qsBwDwKv+dDuGIgrSNWCwWLwPOwfKaJsFSRFuAlUssSqgKsO23SGyuDm1hOq1SAA)

verify again with the example from part 1

![type test 3](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vm0v1yipssokn6xfu1ge.png)

[playground](https://www.typescriptlang.org/play?#code/C4TwDgpgBA4hwGED2BXAdsA8gMwMooCNdgAnASzQHMAeAKAEhjyqB9KCAD2AjQBMBnKP1IVKAGgb4iIquy48BQmeIbJ0wOdz6D0AazRIA7mgDaAXSgBeKOdoA+K1Cai2nLYoAGAEgDew5pQAvr5SzlTBPhTYECRQACoAhmQANoEeDAD8sPBqGDihytSJKWJOhGHiNgCMpQB09bnAZnYMAFxQjSYA5Mk8lMAAFl1mtLSgkFAAohyQAMbARaUAqg7WceaaCoJLG1kAFDsWblvxu1CkKNDt2AnJ-BAAlFDXt-ejAPTv7PzJFMAAtLwyPwEgRev80G5-r9IVAAALjCD8WbkMAApEw4DvAz-dAoe68f4ANwSJH4tE4YCQJA0syQaGEUAS-HuNMcRU22nOJEudj2T0sDh8DE+tECo2ZrIW0zmCzgiFQeTw5UKXQS-3+6s1WoSXVKaq6dlKAFY7Hynp9uZdSmBJeckcBaJaEfx-pSIPM3SQSNTaJKYtKZh65TlFVhldIAtQ1RqdTq9VADUaoFUzfyoJabncIKUbil7cJRrQgA)

great both of them are working!

this is the end of part 2, in part 3(not yet ready) we will going through more edge cases and further refine our Expect type