Typescript Test Your Generic Type Part 2

type Expect<T, U>= T extends U ? U extends T ? true : false : false
  1. keyof T
  2. U[]
  3. [U]
  4. SomeGeneric< T >
type A = "a" | "b" extends "a" ? true : false // false 
type B = "a" extends "a" | "b" ? true : false // true
type C<T,U> = T extends U ? true : false
type r1 = C<"a" | "b", "a"> // boolean
type r2 = C<"a", "a" | "b"> // true
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 Expect<T, U>= T[] extends U[] ? U[] extends T[] ? true : false : false

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store