You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When testing types, be aware of the difference between equality and assignability, particularly for function types.
For functions that use callbacks, test the inferred types of the callback parameters. Don't forget to test the type of this if it's part of your API.
Avoid writing your own type testing code. Use one of the standard tools instead.
For code on DefinitelyTyped, use dtslint. For your own code, use vitest, expect-type, or the Type Challenges approach. If you want to test type display, use eslint-plugin-expect-type.
constdouble=(x: number)=>2*x;declareletp: Parameters<typeofdouble>;assertType<[number,number]>(p);// ~ Argument of type '[number]' is not// assignable to parameter of type [number, number]declareletr: ReturnType<typeofdouble>;assertType<number>(r);// OK
constbeatles=['john','paul','george','ringo'];assertType<number[]>(map(beatles,function(name,i,array){// ~~~ Argument of type '(name: any, i: any, array: any) => any' is// not assignable to parameter of type '(u: string) => any'assertType<string>(name);assertType<number>(i);assertType<string[]>(array);assertType<string[]>(this);// ~~~~ 'this' implicitly has type 'any'returnname.length;}));
constanyVal: any=1;expectTypeOf(anyVal).toEqualTypeOf<number>();// ~~~~~~// Type 'number' does not satisfy the constraint 'never'.constdouble=(x: number)=>2*x;expectTypeOf(double).toEqualTypeOf<(a: number,b: number)=>number>();// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~// Type ... does not satisfy '"Expected: function, Actual: never"'interfaceABReadOnly{readonlya: string;b: number;}declareletab: {a: string,b: number};expectTypeOf(ab).toEqualTypeOf<ABReadOnly>();// ~~~~~~~~~~~~~// Arguments for the rest parameter 'MISMATCH' were not provided.expectTypeOf(ab).toEqualTypeOf<{a: string,b: number}>();// OK
typeTest3=Expect<Equals<1|2,2|1>>;// good!typeTest4=Expect<Equals<[a: 1,b: 2],[1,2]>>;// maybe not so goodtypeTest5=Expect<Equals<{x: 1}&{y: 2},{x: 1,y: 2}>>;// surprising// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~// Type 'false' does not satisfy the constraint 'true'.