Skip to content

expectTypeOf

WARNING

During runtime this function doesn't do anything. To enable typechecking, don't forget to pass down --typecheck flag.

  • Type: <T>(a: unknown) => ExpectTypeOf

not

  • Type: ExpectTypeOf

You can negate all assertions, using .not property.

toEqualTypeOf

  • Type: <T>(expected: T) => void

This matcher will check if the types are fully equal to each other. This matcher will not fail if two objects have different values, but the same type. It will fail however if an object is missing a property.

ts
import {  } from 'vitest'

({ : 1 }).<{ : number }>()
({ : 1 }).({ : 1 })
({ : 1 }).({ : 2 })
({ : 1, : 1 })..<{ : number }>()

toMatchTypeOf

  • Type: <T>(expected: T) => void

This matcher checks if expect type extends provided type. It is different from toEqual and is more similar to expect's toMatchObject(). With this matcher, you can check if an object “matches” a type.

ts
import {  } from 'vitest'

({ : 1, : 1 }).({ : 1 })
<number>().<string | number>()
<string | number>()..<number>()

extract

  • Type: ExpectTypeOf<ExtractedUnion>

You can use .extract to narrow down types for further testing.

ts
import {  } from 'vitest'

type <> =  | [] | { ?: ; ?: ; ?:  }

interface CSSProperties { ?: string; ?: string }

function <>(: ): <> {
  return {}
}

const : CSSProperties = { : '1px', : '2px' }

(())
  .<{ ?: any }>() // extracts the last type from a union
  .<{ ?: CSSProperties; ?: CSSProperties; ?: CSSProperties }>()

(())
  .<unknown[]>() // extracts an array from a union
  .<CSSProperties[]>()

WARNING

If no type is found in the union, .extract will return never.

exclude

  • Type: ExpectTypeOf<NonExcludedUnion>

You can use .exclude to remove types from a union for further testing.

ts
import {  } from 'vitest'

type <> =  | [] | { ?: ; ?: ; ?:  }

interface CSSProperties { ?: string; ?: string }

function <>(: ): <> {
  return {}
}

const : CSSProperties = { : '1px', : '2px' }

(())
  .<unknown[]>()
  .<{ ?: unknown }>() // or just .exclude<unknown[] | { xs?: unknown }>()
  .<CSSProperties>()

WARNING

If no type is found in the union, .exclude will return never.

returns

  • Type: ExpectTypeOf<ReturnValue>

You can use .returns to extract return value of a function type.

ts
import {  } from 'vitest'

(() => {})..()
((: number) => [, ])..([1, 2])

WARNING

If used on a non-function type, it will return never, so you won't be able to chain it with other matchers.

parameters

  • Type: ExpectTypeOf<Parameters>

You can extract function arguments with .parameters to perform assertions on its value. Parameters are returned as an array.

ts
import {  } from 'vitest'

type  = () => void
type  = (: string) => void

<>()..<[]>()
<>()..<[string]>()

WARNING

If used on a non-function type, it will return never, so you won't be able to chain it with other matchers.

TIP

You can also use .toBeCallableWith matcher as a more expressive assertion.

parameter

  • Type: (nth: number) => ExpectTypeOf

You can extract a certain function argument with .parameter(number) call to perform other assertions on it.

ts
import {  } from 'vitest'

function (: number, : string) {
  return [, ]
}

().(0).()
().(1).()

WARNING

If used on a non-function type, it will return never, so you won't be able to chain it with other matchers.

constructorParameters

  • Type: ExpectTypeOf<ConstructorParameters>

You can extract constructor parameters as an array of values and perform assertions on them with this method.

ts
import {  } from 'vitest'

()..<[] | [string | number | Date]>()

WARNING

If used on a non-function type, it will return never, so you won't be able to chain it with other matchers.

TIP

You can also use .toBeConstructibleWith matcher as a more expressive assertion.

instance

  • Type: ExpectTypeOf<ConstructableInstance>

This property gives access to matchers that can be performed on an instance of the provided class.

ts
import {  } from 'vitest'

()..('toISOString')

WARNING

If used on a non-function type, it will return never, so you won't be able to chain it with other matchers.

items

  • Type: ExpectTypeOf<T>

You can get array item type with .items to perform further assertions.

ts
import {  } from 'vitest'

([1, 2, 3])..<number>()
([1, 2, 3])...<string>()

resolves

  • Type: ExpectTypeOf<ResolvedPromise>

This matcher extracts resolved value of a Promise, so you can perform other assertions on it.

ts
import {  } from 'vitest'

async function () {
  return 123
}

()...()
(.('string'))..()

WARNING

If used on a non-promise type, it will return never, so you won't be able to chain it with other matchers.

guards

  • Type: ExpectTypeOf<Guard>

This matcher extracts guard value (e.g., v is number), so you can perform assertions on it.

ts
import {  } from 'vitest'

function (: any):  is string {
  return typeof  === 'string'
}
()..()

WARNING

Returns never, if the value is not a guard function, so you won't be able to chain it with other matchers.

asserts

  • Type: ExpectTypeOf<Assert>

This matcher extracts assert value (e.g., assert v is number), so you can perform assertions on it.

ts
import {  } from 'vitest'

function (: any): asserts  is number {
  if (typeof  !== 'number')
    throw new ('Nope !')
}

()..()

WARNING

Returns never, if the value is not an assert function, so you won't be able to chain it with other matchers.

toBeAny

  • Type: () => void

With this matcher you can check, if provided type is any type. If the type is too specific, the test will fail.

ts
import {  } from 'vitest'

<any>().()
({} as any).()
('string')..()

toBeUnknown

  • Type: () => void

This matcher checks, if provided type is unknown type.

ts
import {  } from 'vitest'

().()
({} as unknown).()
('string')..()

toBeNever

  • Type: () => void

This matcher checks, if provided type is a never type.

ts
import { expectTypeOf } from 'vitest'

expectTypeOf<never>().toBeNever()
expectTypeOf((): never => {}).returns.toBeNever()

toBeFunction

  • Type: () => void

This matcher checks, if provided type is a function.

ts
import { expectTypeOf } from 'vitest'

expectTypeOf(42).not.toBeFunction()
expectTypeOf((): never => {}).toBeFunction()

toBeObject

  • Type: () => void

This matcher checks, if provided type is an object.

ts
import {  } from 'vitest'

(42)..()
({}).()

toBeArray

  • Type: () => void

This matcher checks, if provided type is Array<T>.

ts
import {  } from 'vitest'

(42)..()
([]).()
([1, 2]).()
([{}, 42]).()

toBeString

  • Type: () => void

This matcher checks, if provided type is a string.

ts
import {  } from 'vitest'

(42)..()
('').()
('a').()

toBeBoolean

  • Type: () => void

This matcher checks, if provided type is boolean.

ts
import {  } from 'vitest'

(42)..()
(true).()
<boolean>().()

toBeVoid

  • Type: () => void

This matcher checks, if provided type is void.

ts
import {  } from 'vitest'

(() => {})..()
<void>().()

toBeSymbol

  • Type: () => void

This matcher checks, if provided type is a symbol.

ts
import {  } from 'vitest'

((1)).()
<symbol>().()

toBeNull

  • Type: () => void

This matcher checks, if provided type is null.

ts
import {  } from 'vitest'

(null).()
<null>().()
()..()

toBeUndefined

  • Type: () => void

This matcher checks, if provided type is undefined.

ts
import {  } from 'vitest'

().()
<undefined>().()
(null)..()

toBeNullable

  • Type: () => void

This matcher checks, if you can use null or undefined with provided type.

ts
import {  } from 'vitest'

<1 | undefined>().()
<1 | null>().()
<1 | undefined | null>().()

toBeCallableWith

  • Type: () => void

This matcher ensures you can call provided function with a set of parameters.

ts
import {  } from 'vitest'

type  = () => void
type  = (: string) => void

<>().()
<>().('some string')

WARNING

If used on a non-function type, it will return never, so you won't be able to chain it with other matchers.

toBeConstructibleWith

  • Type: () => void

This matcher ensures you can create a new instance with a set of constructor parameters.

ts
import {  } from 'vitest'

().(new ())
().('01-01-2000')

WARNING

If used on a non-function type, it will return never, so you won't be able to chain it with other matchers.

toHaveProperty

  • Type: <K extends keyof T>(property: K) => ExpectTypeOf<T[K>

This matcher checks if a property exists on the provided object. If it exists, it also returns the same set of matchers for the type of this property, so you can chain assertions one after another.

ts
import {  } from 'vitest'

const  = { : 1, : '' }

().('a')
()..('c')

().('a').()
().('b').()
().('a')..()

Released under the MIT License.