-
-
Notifications
You must be signed in to change notification settings - Fork 70
Open
Description
Hi, first of all, thanks for tsd, looks useful!
Looking at the Assertions -> expectType section in the readme, it appears that expectType<T>(expression: T) should assert that the types are identical:
expectType<T>(expression: T)
Asserts that the type of
expressionis identical to typeT.
Scoping only to the context of the tsd TypeScript types (editor integration, tsc checks), "identical" is misleading: the type equality is only in a single direction, not bidirectional.
For example, the argument below with the excess property x is not reported as a TypeScript error (contrasted against expect-type, which correctly errors):
import { expectType } from "tsd";
import { expectTypeOf } from "expect-type";
type User = { id: number; name: string; email?: string; };
type UserExcess = { id: number; name: string; email?: string; y: string; };
type User2 = { id: number; name: string; email?: string; };
type UserExcess2 = { id: number; name: string; email?: string; x: string; };
expectType<User>({} as unknown as User2); // Correct: No error reported
expectType<UserExcess>({} as unknown as User2); // Correct: Error reported
expectType<User>({} as unknown as UserExcess2); // 💥 Incorrect: No error reported (false negative)
// expect-type `expectTypeOf().toEqualTypeOf()` gets this right
expectTypeOf<User2>().toEqualTypeOf<User>(); // Correct: No error reported
expectTypeOf<User2>().toEqualTypeOf<UserExcess>(); // Correct: Error reported
expectTypeOf<UserExcess2>().toEqualTypeOf<User>(); // Correct: Error reported
Suggested Solution
- Change the "identical" in the documentation to "assignable" or "compatible" or similar to avoid the word "identical" (or describe that it's only single-direction with other words)
- Possibly also: I saw that the implementation of
expectTypeis the same as the implementation ofexpectAssignable- should one of those two be dropped? - Possibly also: Offer a true "identical" utility (maybe named
expectTypeStrict?) which will check in both directions, likeexpectTypeOf<>().toEqualTypeOf<>()fromexpect-type
Metadata
Metadata
Assignees
Labels
No labels