expectTypeOf & assertType in Vitest

From the Vitest cheat sheet ยท Type Testing ยท verified Nov 2026

expectTypeOf & assertType

Check parameter, return, and object types at compile time

typescript
import { assertType, expectTypeOf, test } from 'vitest'

test('types', () => {
  expectTypeOf(add).parameters.toEqualTypeOf<[number, number]>()
  expectTypeOf(add).returns.toBeNumber()
  expectTypeOf(user).toExtend<{ name: string }>()
  assertType<string>(formatName(user))
})
๐Ÿ“Œ *.test-d.ts files are type-checked, never executed
โš ๏ธ toMatchTypeOf is deprecated; use toExtend or toMatchObjectType
๐Ÿ” toMatchObjectType wants plain object types; interfaces can fail
๐Ÿ’ก @ts-expect-error turns 'this should not compile' into a test
expectTypeOfassertTypetypescript

Continue with Vitest

Save the full cheat sheet or work through every related task.

More Vitest tasks

Back to the full Vitest cheat sheet