Skip to content

Instantly share code, notes, and snippets.

@thetarnav
Last active January 30, 2024 18:12
Show Gist options
  • Select an option

  • Save thetarnav/8615ad758c307a556c45f7b3ccc4a596 to your computer and use it in GitHub Desktop.

Select an option

Save thetarnav/8615ad758c307a556c45f7b3ccc4a596 to your computer and use it in GitHub Desktop.
expect type helper
/*
From TypeStrong/ts-expect
MIT License
https://github.com/TypeStrong/ts-expect/blob/master/src/index.ts
*/
/**
* Checks that `Subtype` is assignable to `Type`.
*
* ```ts
* assertType<TypeOf<number, 123>>(true)
* assertType<TypeOf<123, number>>(false)
* ```
*/
export type TypeOf<Type, Subtype> =
Exclude<Subtype, Type> extends never ? true : false
/**
* Checks that `A` is equal to the same type as `B`.
*
* ```ts
* assertType<TypeEqual<123, 123>>(true)
* assertType<TypeEqual<123, number>>(false)
* assertType<TypeEqual<number, 123>>(false)
* assertType<TypeEqual<number, number>>(true)
* ```
*/
export type TypeEqual<A, B> = (
<T>() => T extends A ? 1 : 2
) extends (
<T>() => T extends B ? 1 : 2
) ? true : false
/**
* Asserts the `value` type is assignable to the generic `Type`.
*
* ```ts
* assertType<number>(123)
* assertType<boolean>(true)
* ```
*/
export const assertType = <Type>(_: Type): void => void 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment