expectTypeOf
WARNING
在运行时,此函数不执行任何操作。如需启用类型检查,请务必传递 --typecheck
标志。
- 类型:
<T>(a: unknown) => ExpectTypeOf
not
- 类型:
ExpectTypeOf
您可以使用 .not
属性来否定所有断言。
toEqualTypeOf
- 类型:
<T>(expected: T) => void
此匹配器用于检查类型是否完全相等。即使两个对象的值不同但类型相同,此匹配器也不会失败。然而,如果对象缺少属性,它将失败。
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
- 类型:
<T>(expected: T) => void
此匹配器检查期望的类型是否扩展了提供的类型。它与 toEqual
不同,更类似于 expect 中的 toMatchObject()
。使用此匹配器,您可以检查对象是否“匹配”某个类型。
import { expectTypeOf } from 'vitest';
expectTypeOf({ a: 1, b: 1 }).toMatchTypeOf({ a: 1 });
expectTypeOf<number>().toMatchTypeOf<string | number>();
expectTypeOf<string | number>().not.toMatchTypeOf<number>();
extract
- 类型:
ExpectTypeOf<ExtractedUnion>
您可以使用 .extract
来缩小类型,以进行进一步测试。
import { expectTypeOf } from 'vitest';
type ResponsiveProp<T> = T | T[] | { xs?: T; sm?: T; md?: T };
interface CSSProperties {
margin?: string;
padding?: string;
}
function getResponsiveProp<T>(_props: T): ResponsiveProp<T> {
return {};
}
const cssProperties: CSSProperties = { margin: '1px', padding: '2px' };
expectTypeOf(getResponsiveProp(cssProperties))
.extract<{ xs?: any }>() // 从联合类型中提取特定类型
.toEqualTypeOf<{
xs?: CSSProperties;
sm?: CSSProperties;
md?: CSSProperties;
}>();
expectTypeOf(getResponsiveProp(cssProperties))
.extract<unknown[]>() // 从联合类型中提取数组类型
.toEqualTypeOf<CSSProperties[]>();
WARNING
如果联合类型中未找到匹配的类型,.extract
将返回 never
。
exclude
- 类型::
ExpectTypeOf<NonExcludedUnion>
您可以使用 .exclude
从联合类型中排除类型以进行进一步测试。
import { expectTypeOf } from 'vitest';
type ResponsiveProp<T> = T | T[] | { xs?: T; sm?: T; md?: T };
interface CSSProperties {
margin?: string;
padding?: string;
}
function getResponsiveProp<T>(_props: T): ResponsiveProp<T> {
return {};
}
const cssProperties: CSSProperties = { margin: '1px', padding: '2px' };
expectTypeOf(getResponsiveProp(cssProperties))
.exclude<unknown[]>()
.exclude<{ xs?: unknown }>() // 或者直接 .exclude<unknown[] | { xs?: unknown }>()
.toEqualTypeOf<CSSProperties>();
WARNING
如果联合类型中未找到匹配的类型,.exclude
将返回 never
。
returns
- 类型:
ExpectTypeOf<ReturnValue>
您可以使用 .returns
提取函数类型的返回值。
import { expectTypeOf } from 'vitest';
expectTypeOf(() => {}).returns.toBeVoid();
expectTypeOf((a: number) => [a, a]).returns.toEqualTypeOf([1, 2]);
WARNING
如果用于非函数类型,它将返回 never
,从而导致无法与其他匹配器进行链式调用。
parameters
- 类型:
ExpectTypeOf<Parameters>
您可以使用 .parameters
提取函数参数,并对其值执行断言。返回的参数是一个数组。
import { expectTypeOf } from 'vitest';
type NoParam = () => void;
type HasParam = (s: string) => void;
expectTypeOf<NoParam>().parameters.toEqualTypeOf<[]>();
expectTypeOf<HasParam>().parameters.toEqualTypeOf<[string]>();
WARNING
如果用于非函数类型,它将返回 never
,从而导致无法与其他匹配器进行链式调用。
TIP
您还可以使用 .toBeCallableWith
匹配器作为更具表达力的断言。
parameter
- 类型:
(nth: number) => ExpectTypeOf
您可以使用 .parameter(number)
调用来提取特定的函数参数,并对其执行其他断言。
import { expectTypeOf } from 'vitest';
function foo(a: number, b: string) {
return [a, b];
}
expectTypeOf(foo).parameter(0).toBeNumber();
expectTypeOf(foo).parameter(1).toBeString();
WARNING
如果用于非函数类型,它将返回 never
,从而导致无法与其他匹配器进行链式调用。
constructorParameters
- 类型:
ExpectTypeOf<ConstructorParameters>
您可以使用此方法将构造函数参数提取为值数组,并对其执行断言。
import { expectTypeOf } from 'vitest';
expectTypeOf(Date).constructorParameters.toEqualTypeOf<
[] | [string | number | Date]
>();
WARNING
如果用于非函数类型,它将返回 never
,从而导致无法与其他匹配器进行链式调用。
TIP
您还可以使用 .toBeConstructibleWith
匹配器作为更具表达力的断言。
instance
- 类型:
ExpectTypeOf<ConstructableInstance>
此属性允许访问可在所提供类的实例上执行的匹配器。
import { expectTypeOf } from 'vitest';
expectTypeOf(Date).instance.toHaveProperty('toISOString');
WARNING
如果用于非函数类型,它将返回 never
,从而导致无法与其他匹配器进行链式调用。
items
- 类型:
ExpectTypeOf<T>
您可以使用 .items
获取数组项的类型,以执行进一步的断言。
import { expectTypeOf } from 'vitest';
expectTypeOf([1, 2, 3]).items.toEqualTypeOf<number>();
expectTypeOf([1, 2, 3]).items.not.toEqualTypeOf<string>();
resolves
- 类型:
ExpectTypeOf<ResolvedPromise>
此匹配器提取 Promise
的已解析值,从而允许您对其执行其他断言。
import { expectTypeOf } from 'vitest';
async function asyncFunc() {
return 123;
}
expectTypeOf(asyncFunc).returns.resolves.toBeNumber();
expectTypeOf(Promise.resolve('string')).resolves.toBeString();
WARNING
如果用于非 Promise 类型,它将返回 never
,从而导致无法与其他匹配器进行链式调用。
guards
- 类型:
ExpectTypeOf<Guard>
此匹配器提取守卫值(例如,v is number
),从而允许您对其执行断言。
import { expectTypeOf } from 'vitest';
function isString(v: any): v is string {
return typeof v === 'string';
}
expectTypeOf(isString).guards.toBeString();
WARNING
如果值不是类型守卫函数,则返回 never
,从而导致无法与其他匹配器进行链式调用。
asserts
- 类型:
ExpectTypeOf<Assert>
此匹配器提取断言值(例如,assert v is number
),从而允许您对其执行断言。
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
如果值不是类型断言函数,则返回 never
,从而导致无法与其他匹配器进行链式调用。
toBeAny
- 类型:
() => void
使用此匹配器,您可以检查所提供的类型是否为 any
类型。若类型过于具体,则测试会失败。
import { expectTypeOf } from 'vitest';
expectTypeOf<any>().toBeAny();
expectTypeOf({} as any).toBeAny();
expectTypeOf('string').not.toBeAny();
toBeUnknown
- 类型:
() => void
此匹配器检查所提供的类型是否为 unknown
类型。
import { expectTypeOf } from 'vitest';
expectTypeOf().toBeUnknown();
expectTypeOf({} as unknown).toBeUnknown();
expectTypeOf('string').not.toBeUnknown();
toBeNever
- 类型:
() => void
此匹配器检查所提供的类型是否为 never
类型。
import { expectTypeOf } from 'vitest';
expectTypeOf<never>().toBeNever();
expectTypeOf((): never => {}).returns.toBeNever();
toBeFunction
- 类型:
() => void
此匹配器检查所提供的类型是否为 function
。
import { expectTypeOf } from 'vitest';
expectTypeOf(42).not.toBeFunction();
expectTypeOf((): never => {}).toBeFunction();
toBeObject
- 类型:
() => void
此匹配器检查所提供的类型是否为 object
。
import { expectTypeOf } from 'vitest';
expectTypeOf(42).not.toBeObject();
expectTypeOf({}).toBeObject();
toBeArray
- 类型:
() => void
此匹配器检查所提供的类型是否为 Array<T>
。
import { expectTypeOf } from 'vitest';
expectTypeOf(42).not.toBeArray();
expectTypeOf([]).toBeArray();
expectTypeOf([1, 2]).toBeArray();
expectTypeOf([{}, 42]).toBeArray();
toBeString
- 类型:
() => void
此匹配器检查所提供的类型是否为 string
。
import { expectTypeOf } from 'vitest';
expectTypeOf(42).not.toBeString();
expectTypeOf('').toBeString();
expectTypeOf('a').toBeString();
toBeBoolean
- 类型:
() => void
此匹配器检查所提供的类型是否为 boolean
。
import { expectTypeOf } from 'vitest';
expectTypeOf(42).not.toBeBoolean();
expectTypeOf(true).toBeBoolean();
expectTypeOf<boolean>().toBeBoolean();
toBeVoid
- 类型:
() => void
此匹配器检查所提供的类型是否为 void
。
import { expectTypeOf } from 'vitest';
expectTypeOf(() => {}).returns.toBeVoid();
expectTypeOf<void>().toBeVoid();
toBeSymbol
- 类型:
() => void
此匹配器检查所提供的类型是否为 symbol
。
import { expectTypeOf } from 'vitest';
expectTypeOf(Symbol(1)).toBeSymbol();
expectTypeOf<symbol>().toBeSymbol();
toBeNull
- 类型:
() => void
此匹配器检查所提供的类型是否为 null
。
import { expectTypeOf } from 'vitest';
expectTypeOf(null).toBeNull();
expectTypeOf<null>().toBeNull();
expectTypeOf(undefined).not.toBeNull();
toBeUndefined
- 类型:
() => void
此匹配器检查所提供的类型是否为 undefined
。
import { expectTypeOf } from 'vitest';
expectTypeOf(undefined).toBeUndefined();
expectTypeOf<undefined>().toBeUndefined();
expectTypeOf(null).not.toBeUndefined();
toBeNullable
- 类型:
() => void
此匹配器检查所提供的类型是否可以为 null
或 undefined
。
import { expectTypeOf } from 'vitest';
expectTypeOf<undefined | 1>().toBeNullable();
expectTypeOf<null | 1>().toBeNullable();
expectTypeOf<undefined | null | 1>().toBeNullable();
toBeCallableWith
- 类型:
() => void
此匹配器确保可以使用一组参数调用所提供的函数。
import { expectTypeOf } from 'vitest';
type NoParam = () => void;
type HasParam = (s: string) => void;
expectTypeOf<NoParam>().toBeCallableWith();
expectTypeOf<HasParam>().toBeCallableWith('some string');
WARNING
如果用于非函数类型,它将返回 never
,从而导致无法与其他匹配器进行链式调用。
toBeConstructibleWith
- 类型:
() => void
此匹配器确保可以使用一组构造函数参数创建新实例。
import { expectTypeOf } from 'vitest';
expectTypeOf(Date).toBeConstructibleWith(new Date());
expectTypeOf(Date).toBeConstructibleWith('01-01-2000');
WARNING
如果用于非函数类型,它将返回 never
,从而导致无法与其他匹配器进行链式调用。
toHaveProperty
- 类型:
<K extends keyof T>(property: K) => ExpectTypeOf<T[K]>
此匹配器检查所提供的对象上是否存在某个属性。如果存在,它还会返回该属性类型的相同匹配器集,从而允许您进行链式断言。
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();