Suppose I have this function
function myFunction(): { foo: number; bar: string } {
return { foo: 1, bar: 'bar' };
}
and I want to test it using jest.
it('myFunction', () => {
expect(myFunction()).toStrictEqual({ foo: 1, bar: 2 /*oops wrong type!*/ });
});
And this will compile despite bar
being passed as a number
instead of string
such as defined in myFunction
.
This is a shame because this could be caught at compile-time instead of run-time.
I could also write
it('test', () => {
expect(myFunction()).toStrictEqual<ReturnType<typeof myFunction>>({
foo: 1,
bar: 1, /* this time, the mistake is caught at compile-time :) */
});
});
but this is verbose, redundant, and a pain when using Promise
.
I am looking for a trick to implicitely do this. Is there a helper that would allow to type check on the returned or resolved type by default?
Aucun commentaire:
Enregistrer un commentaire