Skip to content

Commit

Permalink
improve vanilla array handling
Browse files Browse the repository at this point in the history
  • Loading branch information
mmkal committed Oct 3, 2023
1 parent 89d81cc commit 76e6ece
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
14 changes: 8 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,14 @@ export type UsefulKeys<T> = T extends any[]
// This swaps "leaf" types with a literal message about what the actual and expected types are.
// Needs to check for Not<IsAny<Actual>> because otherwise LeafTypeOf<Actual> returns never, which extends everything 🤔
export type MismatchInfo<Actual, Expected> = And<[Extends<PrintType<Actual>, '...'>, Not<IsAny<Actual>>]> extends true
? {
[K in UsefulKeys<Actual> | UsefulKeys<Expected>]: MismatchInfo<
K extends keyof Actual ? Actual[K] : never,
K extends keyof Expected ? Expected[K] : never
>
}
? And<[Extends<any[], Actual>, Extends<any[], Expected>]> extends true
? Array<MismatchInfo<Extract<Actual, any[]>[number], Extract<Expected, any[]>[number]>>
: {
[K in UsefulKeys<Actual> | UsefulKeys<Expected>]: MismatchInfo<
K extends keyof Actual ? Actual[K] : never,
K extends keyof Expected ? Expected[K] : never
>
}
: StrictEqualUsingBranding<Actual, Expected> extends true
? Actual
: `Expected: ${PrintType<Expected>}, Actual: ${PrintType<Exclude<Actual, Expected>>}`
Expand Down
5 changes: 2 additions & 3 deletions test/__snapshots__/errors.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,8 @@ test/usage.test.ts:999:999 - error TS2349: This expression is not callable.
999 expectTypeOf([1, 2, 3]).items.toBeString()
~~~~~~~~~~
test/usage.test.ts:999:999 - error TS2344: Type 'number[]' does not satisfy the constraint '{ [x: number]: never; }'.
'number' index signatures are incompatible.
Type 'number' is not assignable to type 'never'.
test/usage.test.ts:999:999 - error TS2344: Type 'number[]' does not satisfy the constraint 'never[]'.
Type 'number' is not assignable to type 'never'.
999 expectTypeOf<any[]>().toEqualTypeOf<number[]>()
~~~~~~~~
Expand Down

0 comments on commit 76e6ece

Please sign in to comment.