expectTypeOf
WARNING
A runtime, questa funzione non esegue alcuna operazione. Per abilitare il controllo dei tipi, non dimenticare di specificare il flag --typecheck
.
- Tipo:
<T>(a: unknown) => ExpectTypeOf
not
- Tipo:
ExpectTypeOf
È possibile negare tutte le asserzioni utilizzando la proprietà .not
.
toEqualTypeOf
- Tipo:
<T>(expected: T) => void
Questo matcher verifica se i tipi sono esattamente uguali tra loro. Non fallirà se due oggetti hanno valori diversi ma lo stesso tipo. Tuttavia, fallirà se a un oggetto manca una proprietà.
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
- Tipo:
<T>(expected: T) => void
Questo matcher verifica se il tipo fornito estende il tipo atteso. È diverso da toEqual
ed è più simile a toMatchObject()
di expect. Con questo matcher, puoi verificare se un oggetto "corrisponde" a un tipo.
import { expectTypeOf } from 'vitest';
expectTypeOf({ a: 1, b: 1 }).toMatchTypeOf({ a: 1 });
expectTypeOf<number>().toMatchTypeOf<string | number>();
expectTypeOf<string | number>().not.toMatchTypeOf<number>();
extract
- Tipo:
ExpectTypeOf<ExtractedUnion>
È possibile usare .extract
per affinare i tipi da un'unione per ulteriori verifiche.
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 }>() // estrae il tipo specificato da un'unione
.toEqualTypeOf<{
xs?: CSSProperties;
sm?: CSSProperties;
md?: CSSProperties;
}>();
expectTypeOf(getResponsiveProp(cssProperties))
.extract<unknown[]>() // estrae un array da un'unione
.toEqualTypeOf<CSSProperties[]>();
WARNING
Se nessun tipo viene trovato nell'unione, .extract
restituirà never
.
exclude
- Tipo:
ExpectTypeOf<NonExcludedUnion>
È possibile usare .exclude
per rimuovere tipi da un'unione per ulteriori verifiche.
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 }>() // o semplicemente .exclude<unknown[] | { xs?: unknown }>()
.toEqualTypeOf<CSSProperties>();
WARNING
Se nessun tipo viene trovato nell'unione, .exclude
restituirà never
.
returns
- Tipo:
ExpectTypeOf<ReturnValue>
È possibile usare .returns
per estrarre il tipo di valore di ritorno di una funzione.
import { expectTypeOf } from 'vitest';
expectTypeOf(() => {}).returns.toBeVoid();
expectTypeOf((a: number) => [a, a]).returns.toEqualTypeOf([1, 2]);
WARNING
Se usato su un tipo non-funzione, restituirà never
, impedendo la concatenazione con altri matcher.
parameters
- Tipo:
ExpectTypeOf<Parameters>
È possibile estrarre gli argomenti di una funzione con .parameters
per eseguire asserzioni sui loro tipi. I parametri vengono restituiti come un array.
import { expectTypeOf } from 'vitest';
type NoParam = () => void;
type HasParam = (s: string) => void;
expectTypeOf<NoParam>().parameters.toEqualTypeOf<[]>();
expectTypeOf<HasParam>().parameters.toEqualTypeOf<[string]>();
WARNING
Se usato su un tipo non-funzione, restituirà never
, impedendo la concatenazione con altri matcher.
TIP
È possibile anche usare il matcher .toBeCallableWith
per un'asserzione più espressiva.
parameter
- Tipo:
(nth: number) => ExpectTypeOf
È possibile estrarre un argomento specifico di una funzione usando .parameter(number)
per eseguire ulteriori asserzioni su di esso.
import { expectTypeOf } from 'vitest';
function foo(a: number, b: string) {
return [a, b];
}
expectTypeOf(foo).parameter(0).toBeNumber();
expectTypeOf(foo).parameter(1).toBeString();
WARNING
Se usato su un tipo non-funzione, restituirà never
, impedendo la concatenazione con altri matcher.
constructorParameters
- Tipo:
ExpectTypeOf<ConstructorParameters>
È possibile estrarre i parametri del costruttore come un array di valori ed eseguire asserzioni su di essi con questo metodo.
import { expectTypeOf } from 'vitest';
expectTypeOf(Date).constructorParameters.toEqualTypeOf<
[] | [string | number | Date]
>();
WARNING
Se usato su un tipo non-funzione, restituirà never
, impedendo la concatenazione con altri matcher.
TIP
È possibile anche usare il matcher .toBeConstructibleWith
per un'asserzione più espressiva.
instance
- Tipo:
ExpectTypeOf<ConstructableInstance>
Questa proprietà fornisce accesso ai matcher che possono essere eseguiti su un'istanza della classe fornita.
import { expectTypeOf } from 'vitest';
expectTypeOf(Date).instance.toHaveProperty('toISOString');
WARNING
Se usato su un tipo non-funzione, restituirà never
, impedendo la concatenazione con altri matcher.
items
- Tipo:
ExpectTypeOf<T>
È possibile accedere al tipo degli elementi di un array con .items
per eseguire ulteriori asserzioni.
import { expectTypeOf } from 'vitest';
expectTypeOf([1, 2, 3]).items.toEqualTypeOf<number>();
expectTypeOf([1, 2, 3]).items.not.toEqualTypeOf<string>();
resolves
- Tipo:
ExpectTypeOf<ResolvedPromise>
Questo matcher estrae il valore risolto di una Promise
, permettendo di eseguire altre asserzioni su di esso.
import { expectTypeOf } from 'vitest';
async function asyncFunc() {
return 123;
}
expectTypeOf(asyncFunc).returns.resolves.toBeNumber();
expectTypeOf(Promise.resolve('string')).resolves.toBeString();
WARNING
Se usato su un tipo non-promise, restituirà never
, impedendo la concatenazione con altri matcher.
guards
- Tipo:
ExpectTypeOf<Guard>
Questo matcher estrae il tipo di guardia (ad esempio, v is number
), permettendo di eseguire asserzioni su di esso.
import { expectTypeOf } from 'vitest';
function isString(v: any): v is string {
return typeof v === 'string';
}
expectTypeOf(isString).guards.toBeString();
WARNING
Restituisce never
se il valore non è una funzione di guardia, impedendo la concatenazione con altri matcher.
asserts
- Tipo:
ExpectTypeOf<Assert>
Questo matcher estrae il tipo di asserzione (ad esempio, asserts v is number
), permettendo di eseguire asserzioni su di esso.
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
Restituisce never
se il valore non è una funzione di asserzione, impedendo la concatenazione con altri matcher.
toBeAny
- Tipo:
() => void
Con questo matcher puoi verificare se il tipo fornito è di tipo any
. Se il tipo è troppo specifico, il test fallirà.
import { expectTypeOf } from 'vitest';
expectTypeOf<any>().toBeAny();
expectTypeOf({} as any).toBeAny();
expectTypeOf('string').not.toBeAny();
toBeUnknown
- Tipo:
() => void
Questo matcher verifica se il tipo fornito è di tipo unknown
.
import { expectTypeOf } from 'vitest';
expectTypeOf().toBeUnknown();
expectTypeOf({} as unknown).toBeUnknown();
expectTypeOf('string').not.toBeUnknown();
toBeNever
- Tipo:
() => void
Questo matcher verifica se il tipo fornito è di tipo never
.
import { expectTypeOf } from 'vitest';
expectTypeOf<never>().toBeNever();
expectTypeOf((): never => {}).returns.toBeNever();
toBeFunction
- Tipo:
() => void
Questo matcher verifica se il tipo fornito è una funzione
.
import { expectTypeOf } from 'vitest';
expectTypeOf(42).not.toBeFunction();
expectTypeOf((): never => {}).toBeFunction();
toBeObject
- Tipo:
() => void
Questo matcher verifica se il tipo fornito è un oggetto
.
import { expectTypeOf } from 'vitest';
expectTypeOf(42).not.toBeObject();
expectTypeOf({}).toBeObject();
toBeArray
- Tipo:
() => void
Questo matcher verifica se il tipo fornito è Array<T>
.
import { expectTypeOf } from 'vitest';
expectTypeOf(42).not.toBeArray();
expectTypeOf([]).toBeArray();
expectTypeOf([1, 2]).toBeArray();
expectTypeOf([{}, 42]).toBeArray();
toBeString
- Tipo:
() => void
Questo matcher verifica se il tipo fornito è una stringa
.
import { expectTypeOf } from 'vitest';
expectTypeOf(42).not.toBeString();
expectTypeOf('').toBeString();
expectTypeOf('a').toBeString();
toBeBoolean
- Tipo:
() => void
Questo matcher verifica se il tipo fornito è booleano
.
import { expectTypeOf } from 'vitest';
expectTypeOf(42).not.toBeBoolean();
expectTypeOf(true).toBeBoolean();
expectTypeOf<boolean>().toBeBoolean();
toBeVoid
- Tipo:
() => void
Questo matcher verifica se il tipo fornito è void
.
import { expectTypeOf } from 'vitest';
expectTypeOf(() => {}).returns.toBeVoid();
expectTypeOf<void>().toBeVoid();
toBeSymbol
- Tipo:
() => void
Questo matcher verifica se il tipo fornito è un simbolo
.
import { expectTypeOf } from 'vitest';
expectTypeOf(Symbol(1)).toBeSymbol();
expectTypeOf<symbol>().toBeSymbol();
toBeNull
- Tipo:
() => void
Questo matcher verifica se il tipo fornito è null
.
import { expectTypeOf } from 'vitest';
expectTypeOf(null).toBeNull();
expectTypeOf<null>().toBeNull();
expectTypeOf(undefined).not.toBeNull();
toBeUndefined
- Tipo:
() => void
Questo matcher verifica se il tipo fornito è undefined
.
import { expectTypeOf } from 'vitest';
expectTypeOf(undefined).toBeUndefined();
expectTypeOf<undefined>().toBeUndefined();
expectTypeOf(null).not.toBeUndefined();
toBeNullable
- Tipo:
() => void
Questo matcher verifica se il tipo fornito può essere null
o undefined
.
import { expectTypeOf } from 'vitest';
expectTypeOf<undefined | 1>().toBeNullable();
expectTypeOf<null | 1>().toBeNullable();
expectTypeOf<undefined | null | 1>().toBeNullable();
toBeCallableWith
- Tipo:
() => void
Questo matcher verifica che la funzione fornita possa essere chiamata con un determinato insieme di parametri.
import { expectTypeOf } from 'vitest';
type NoParam = () => void;
type HasParam = (s: string) => void;
expectTypeOf<NoParam>().toBeCallableWith();
expectTypeOf<HasParam>().toBeCallableWith('some string');
WARNING
Se usato su un tipo non-funzione, restituirà never
, impedendo la concatenazione con altri matcher.
toBeConstructibleWith
- Tipo:
() => void
Questo matcher verifica che sia possibile creare una nuova istanza con un determinato insieme di parametri del costruttore.
import { expectTypeOf } from 'vitest';
expectTypeOf(Date).toBeConstructibleWith(new Date());
expectTypeOf(Date).toBeConstructibleWith('01-01-2000');
WARNING
Se usato su un tipo non-funzione, restituirà never
, impedendo la concatenazione con altri matcher.
toHaveProperty
- Tipo:
<K extends keyof T>(property: K) => ExpectTypeOf<T[K>
Questo matcher verifica se una proprietà esiste sull'oggetto fornito. Se esiste, restituisce anche lo stesso insieme di matcher per il tipo di questa proprietà, permettendo di concatenare ulteriori asserzioni.
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();