# expectTypeOf ::: warning During runtime this function doesn't do anything. To [enable typechecking](/guide/testing-types#run-typechecking), don't forget to pass down `--typecheck` flag. ::: - **Type:** `(a: unknown) => ExpectTypeOf` ## not - **Type:** `ExpectTypeOf` You can negate all assertions, using `.not` property. ## toEqualTypeOf - **Type:** `(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 { expectTypeOf } from 'vitest' expectTypeOf({ a: 1 }).toEqualTypeOf<{ a: number }>() expectTypeOf({ a: 1 }).toEqualTypeOf({ a: 1 }) expectTypeOf({ a: 1 }).toEqualTypeOf({ a: 2 }) expectTypeOf({ a: 1, b: 1 }).not.toEqualTypeOf<{ a: number }>() ``` ## toMatchTypeOf - **Type:** `(expected: T) => void` ::: warning DEPRECATED This matcher has been deprecated since expect-type v1.2.0. Use [`toExtend`](#toextend) instead. ::: This matcher checks if expect type extends provided type. It is different from `toEqual` and is more similar to [expect's](/api/expect) `toMatchObject()`. With this matcher, you can check if an object “matches” a type. ```ts import { expectTypeOf } from 'vitest' expectTypeOf({ a: 1, b: 1 }).toMatchTypeOf({ a: 1 }) expectTypeOf().toMatchTypeOf() expectTypeOf().not.toMatchTypeOf() ``` ## toExtend - **Type:** `(expected: T) => void` This matcher checks if expect type extends provided type. It is different from `toEqual` and is more similar to [expect's](/api/expect) `toMatchObject()`. With this matcher, you can check if an object "matches" a type. ```ts import { expectTypeOf } from 'vitest' expectTypeOf({ a: 1, b: 1 }).toExtend({ a: 1 }) expectTypeOf().toExtend() expectTypeOf().not.toExtend() ``` ## toMatchObjectType - **Type:** `() => void` This matcher performs a strict check on object types, ensuring that the expected type matches the provided object type. It's stricter than [`toExtend`](#toextend) and is the recommended choice when working with object types as it's more likely to catch issues like readonly properties. ```ts import { expectTypeOf } from 'vitest' expectTypeOf({ a: 1, b: 2 }).toMatchObjectType<{ a: number }>() // preferred expectTypeOf({ a: 1, b: 2 }).toExtend<{ a: number }>() // works but less strict // Supports nested object checking const user = { name: 'John', address: { city: 'New York', zip: '10001' } } expectTypeOf(user).toMatchObjectType<{ name: string; address: { city: string } }>() ``` ::: warning This matcher only works with plain object types. It will fail for union types and other complex types. For those cases, use [`toExtend`](#toextend) instead. ::: ## extract - **Type:** `ExpectTypeOf` You can use `.extract` to narrow down types for further testing. ```ts import { expectTypeOf } from 'vitest' type ResponsiveProp = T | T[] | { xs?: T; sm?: T; md?: T } interface CSSProperties { margin?: string; padding?: string } function getResponsiveProp(_props: T): ResponsiveProp { return {} } const cssProperties: CSSProperties = { margin: '1px', padding: '2px' } expectTypeOf(getResponsiveProp(cssProperties)) .extract<{ xs?: any }>() // extracts the last type from a union .toEqualTypeOf<{ xs?: CSSProperties; sm?: CSSProperties; md?: CSSProperties }>() expectTypeOf(getResponsiveProp(cssProperties)) .extract() // extracts an array from a union .toEqualTypeOf() ``` ::: warning If no type is found in the union, `.extract` will return `never`. ::: ## exclude - **Type:** `ExpectTypeOf` You can use `.exclude` to remove types from a union for further testing. ```ts import { expectTypeOf } from 'vitest' type ResponsiveProp = T | T[] | { xs?: T; sm?: T; md?: T } interface CSSProperties { margin?: string; padding?: string } function getResponsiveProp(_props: T): ResponsiveProp { return {} } const cssProperties: CSSProperties = { margin: '1px', padding: '2px' } expectTypeOf(getResponsiveProp(cssProperties)) .exclude() .exclude<{ xs?: unknown }>() // or just .exclude() .toEqualTypeOf() ``` ::: warning If no type is found in the union, `.exclude` will return `never`. ::: ## returns - **Type:** `ExpectTypeOf` You can use `.returns` to extract return value of a function type. ```ts import { expectTypeOf } from 'vitest' expectTypeOf(() => {}).returns.toBeVoid() expectTypeOf((a: number) => [a, a]).returns.toEqualTypeOf([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` You can extract function arguments with `.parameters` to perform assertions on its value. Parameters are returned as an array. ```ts import { expectTypeOf } from 'vitest' type NoParam = () => void type HasParam = (s: string) => void expectTypeOf().parameters.toEqualTypeOf<[]>() expectTypeOf().parameters.toEqualTypeOf<[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`](#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 { expectTypeOf } from 'vitest' function foo(a: number, b: string) { return [a, b] } expectTypeOf(foo).parameter(0).toBeNumber() expectTypeOf(foo).parameter(1).toBeString() ``` ::: 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` You can extract constructor parameters as an array of values and perform assertions on them with this method. ```ts import { expectTypeOf } from 'vitest' expectTypeOf(Date).constructorParameters.toEqualTypeOf<[] | [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`](#tobeconstructiblewith) matcher as a more expressive assertion. ::: ## instance - **Type:** `ExpectTypeOf` This property gives access to matchers that can be performed on an instance of the provided class. ```ts import { expectTypeOf } from 'vitest' expectTypeOf(Date).instance.toHaveProperty('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` You can get array item type with `.items` to perform further assertions. ```ts import { expectTypeOf } from 'vitest' expectTypeOf([1, 2, 3]).items.toEqualTypeOf() expectTypeOf([1, 2, 3]).items.not.toEqualTypeOf() ``` ## resolves - **Type:** `ExpectTypeOf` This matcher extracts resolved value of a `Promise`, so you can perform other assertions on it. ```ts import { expectTypeOf } from 'vitest' async function asyncFunc() { return 123 } expectTypeOf(asyncFunc).returns.resolves.toBeNumber() expectTypeOf(Promise.resolve('string')).resolves.toBeString() ``` ::: 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` This matcher extracts guard value (e.g., `v is number`), so you can perform assertions on it. ```ts import { expectTypeOf } from 'vitest' function isString(v: any): v is string { return typeof v === 'string' } expectTypeOf(isString).guards.toBeString() ``` ::: 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` This matcher extracts assert value (e.g., `assert v is number`), so you can perform assertions on it. ```ts import { expectTypeOf } from 'vitest' function assertNumber(v: any): asserts v is number { if (typeof v !== 'number') { throw new TypeError('Nope !') } } expectTypeOf(assertNumber).asserts.toBeNumber() ``` ::: 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 { expectTypeOf } from 'vitest' expectTypeOf().toBeAny() expectTypeOf({} as any).toBeAny() expectTypeOf('string').not.toBeAny() ``` ## toBeUnknown - **Type:** `() => void` This matcher checks, if provided type is `unknown` type. ```ts import { expectTypeOf } from 'vitest' expectTypeOf().toBeUnknown() expectTypeOf({} as unknown).toBeUnknown() expectTypeOf('string').not.toBeUnknown() ``` ## toBeNever - **Type:** `() => void` This matcher checks, if provided type is a `never` type. ```ts import { expectTypeOf } from 'vitest' expectTypeOf().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 { expectTypeOf } from 'vitest' expectTypeOf(42).not.toBeObject() expectTypeOf({}).toBeObject() ``` ## toBeArray - **Type:** `() => void` This matcher checks, if provided type is `Array`. ```ts import { expectTypeOf } from 'vitest' expectTypeOf(42).not.toBeArray() expectTypeOf([]).toBeArray() expectTypeOf([1, 2]).toBeArray() expectTypeOf([{}, 42]).toBeArray() ``` ## toBeString - **Type:** `() => void` This matcher checks, if provided type is a `string`. ```ts import { expectTypeOf } from 'vitest' expectTypeOf(42).not.toBeString() expectTypeOf('').toBeString() expectTypeOf('a').toBeString() ``` ## toBeBoolean - **Type:** `() => void` This matcher checks, if provided type is `boolean`. ```ts import { expectTypeOf } from 'vitest' expectTypeOf(42).not.toBeBoolean() expectTypeOf(true).toBeBoolean() expectTypeOf().toBeBoolean() ``` ## toBeVoid - **Type:** `() => void` This matcher checks, if provided type is `void`. ```ts import { expectTypeOf } from 'vitest' expectTypeOf(() => {}).returns.toBeVoid() expectTypeOf().toBeVoid() ``` ## toBeSymbol - **Type:** `() => void` This matcher checks, if provided type is a `symbol`. ```ts import { expectTypeOf } from 'vitest' expectTypeOf(Symbol(1)).toBeSymbol() expectTypeOf().toBeSymbol() ``` ## toBeNull - **Type:** `() => void` This matcher checks, if provided type is `null`. ```ts import { expectTypeOf } from 'vitest' expectTypeOf(null).toBeNull() expectTypeOf().toBeNull() expectTypeOf(undefined).not.toBeNull() ``` ## toBeUndefined - **Type:** `() => void` This matcher checks, if provided type is `undefined`. ```ts import { expectTypeOf } from 'vitest' expectTypeOf(undefined).toBeUndefined() expectTypeOf().toBeUndefined() expectTypeOf(null).not.toBeUndefined() ``` ## toBeNullable - **Type:** `() => void` This matcher checks, if you can use `null` or `undefined` with provided type. ```ts import { expectTypeOf } from 'vitest' expectTypeOf().toBeNullable() expectTypeOf().toBeNullable() expectTypeOf().toBeNullable() ``` ## toBeCallableWith - **Type:** `() => void` This matcher ensures you can call provided function with a set of parameters. ```ts import { expectTypeOf } from 'vitest' type NoParam = () => void type HasParam = (s: string) => void expectTypeOf().toBeCallableWith() expectTypeOf().toBeCallableWith('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 { expectTypeOf } from 'vitest' expectTypeOf(Date).toBeConstructibleWith(new Date()) expectTypeOf(Date).toBeConstructibleWith('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:** `(property: K) => ExpectTypeOf` 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 { expectTypeOf } from 'vitest' const obj = { a: 1, b: '' } expectTypeOf(obj).toHaveProperty('a') expectTypeOf(obj).not.toHaveProperty('c') expectTypeOf(obj).toHaveProperty('a').toBeNumber() expectTypeOf(obj).toHaveProperty('b').toBeString() expectTypeOf(obj).toHaveProperty('a').not.toBeString() ```