New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve errors. Add actual and expected types to MismatchArgs #13
Conversation
…d type Updates `MismatchArgs` to, rather than create the tuple `[never]`, creates a tuple with an object literal containing the `Actual` type and the expected type. How the expected type is shown depends on the specific match. I will most likely post screenshots showing the errors for the various matchers in the PR conversation. I left `toBeCallableWith` and `toBeConstructibleWith` alone as these do not use `MismatchArgs` and I couldn't work out how to use Variadic Tuple Types to make it work. These error messages are easier to understand anyways.
This is nice! DX looks nicer here. I haven't run this locally yet, and I have the same question that was raised on the type-fest PR: this doesn't help with CLI error messages, right? Not a blocker, it would still be an improvement. Some other questions/comments: Can we build something into MismatchArgs and its first typearg so that we don't have to repeat the "core" type? e.g. toBeNumber: (...MISMATCH: MismatchArgs<Extends<Actual, number>, ExpectedResult, Actual, {extends: number}>) Would be nicer something like this: toBeNumber: (...MISMATCH: MismatchArgs<ShouldExtend<Actual, number>, ExpectedResult>) Where ShouldExtend could be a wrapper for Extend which adds the extra info for these errors. Another thing worth noting, esp if the CLI errors aren't improved - you can get intellisense to show you the types on hover, but by hovering on the method, not in the error message. I'll share a screenshot when I can. Also, in the previous repo this library lived in, I had a PR for throw types, worth taking a look if you're interested: mmkal/ts#152. There are also some other template string type tricks in there that maybe we could borrow from. |
@trevorade thanks again for this. I do like the idea of another helper type here, especially because I think it'd be good to be able to play around a bit with the The discussion on your type-fest PR got me thinking more generally about whether there wasn't some semi-low-hanging fruit where a bit more effort/complexity on the library side could improve the DX fairly significantly. And I also wanted to capture exactly what error messages looked like so it wasn't as necessary to share screenshots about what they looked like. And the existing tests only have valid code - so I added a file |
Oh also I invited you to be a collaborator on this repo, so if you'd like to create a branch here instead of on your fork, feel free. |
Thx for the collaborator invite! I appreciate it. I like the idea of capturing error messages in I'll get a chance to work on this more tomorrow. |
Fixes #17 Closes #36 (by documenting that `toMatchTypeOf` ~= `toExtend`) Closes #37 (by documenting that helper types are not part of the API surface) Closes #32 Closes #4 Closes #6 (by documenting that you _can't_ use `.not.toBeCallableWith(...)`) Closes #38 Use generics to give better error messages than "Arguments for the rest parameter 'MISMATCH' were not provided" for most `toEqualTypeOf` cases and many `toMatchTypeOf` cases. This trades off some implementation complexity for better end-user error messages. Essentially, write a special `<Expected extends ...>` constraint for each overload of each method, which does some crazy conditional logic, which boil down to: - `<Expected extends Actual>` for `toEqualTypeOf` (when we aren't in a `.not` context) - `<Expected extends Satisfies<Actual>>` for `toMatchTypeOf` Anyone interested, have a look at the snapshot updates in `errors.test.ts.snap` to see what the real world difference is. Each of these constraints apply only when we know it's going to "fail" - i.e. `Satisfies<...>` is a fairly meaningless helper type that is used to try to show errors at the type-constraint level rather than the `...MISMATCH: MismatchArgs<...>` level which won't give good error messages. When using `.not`, the constraint just becomes `extends unknown`, and you'll have to squint as before. See also: #14 for the better long-term solution, _if_ the TypeScript team decide to merge the throw types PR. See also: #13 for a hopefully-complementary improvement to the information on hover, which will improve the cases this doesn't cover. TODO: - [x] See if the `expectTypeOf({a: 1}).toMatchTypeOf({a: 'one'})` case can also be improved. - [x] Document. The constraints are a bit different to what most users would be used to, so it's worth highlighting the best way to read error messages and clarify when they might default to "Arguments for the rest parameter 'MISMATCH' were not provided" Note: I have publish v0.17.0-1 based on this PR and will hopefully be able to use [that version in vitest](vitest-dev/vitest#4206) as a test before merging.
@trevorade now that #16 is merged I think this is out of date. But if there's an improvement on top of #16 you'd like to make, let me know. It should probably be in a new PR though since this is conflicted now, so I'll close for now. Note - I will aim to cut a 1.0.0 release this week if all goes well, so ideally any changes that don't happen before then are non-breaking so I don't have to go to 2.0.0 embarrassingly quickly! |
Updates
MismatchArgs
to, rather than create the tuple[never]
, creates a tuple with an object literal containing theActual
type and the expected type.How the expected type is shown depends on the specific match. I will most likely post screenshots showing the errors for the various matchers in the PR conversation.
I left
toBeCallableWith
andtoBeConstructibleWith
alone as these do not useMismatchArgs
and I couldn't work out how to use Variadic Tuple Types to make it work. These error messages are easier to understand anyways.If microsoft/TypeScript#40468 is merged, we could drop
MismatchArgs
in favor of having a return that, rather than returntrue
, uses a typethrow
if the type does not match.