8000 vitest/docs/api/assert-type.md at main · romhml/vitest · GitHub
[go: up one dir, main page]

Skip to content

Latest commit

 

History

History
22 lines (16 loc) · 729 Bytes

assert-type.md

File metadata and controls

22 lines (16 loc) · 729 Bytes

assertType

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

  • Type: <T>(value: T): void

You can use this function as an alternative for expectTypeOf to easily assert that the argument type is equal to the generic provided.

import { assertType } from 'vitest'

function concat(a: string, b: string): string
function concat(a: number, b: number): number
function concat(a: string | number, b: string | number): string | number

assertType<string>(concat('a', 'b'))
assertType<number>(concat(1, 2))
// @ts-expect-error wrong types
assertType(concat('a', 2))
0