Last active
February 1, 2025 12:16
-
-
Save rauschma/7f23068c0dd00bba3f59df955101e421 to your computer and use it in GitHub Desktop.
assert-type: API for asserting types
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This code is now an npm package: https://github.com/rauschma/asserttt |
@hcschuetz: Helpful, thanks!
While it's cool to see how some type testing utils can be implemented in user code, I'd much prefer it to be provided by TypeScript itself.
A agree! But it looks like the TypeScript team does not: microsoft/TypeScript#27024
While the name
UnsupportedValueError
fits case 1, I'd suggest the nameUnexpectedValueError
, which would fit both cases.
I was thinking of a switch over operating systems and someone passing in a value (=operating system) that is not supported. In a way, these three words all seem applicable: unsupported, illegal, unexpected.
“Unexpected” does seem like the best choice, because the term “illegal” has its own issues.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
While it's cool to see how some type testing utils can be implemented in user code, I'd much prefer it to be provided by TypeScript itself. This could provide more helpful error messages than "Type 'false' does not satisfy the constraint 'true'." for code like this:
(and we could probably omit the
type _ =
boilerplate). Furthermore theAssertEqual
problem could probably be solved easily on that level.And a minor remark on
UnsupportedValueError
. I see two primary error situations:Color
gets an additional value, sayBlue
, butcolorToString
does not yet handle it. It is very nice that the constructor parameter of typenever
causes a compile-time error in this case (assuming that the tool chain does not just ignore type annotations).Some ill-typed code elsewhere invokes
colorToString
with an illegal argument, say "#ffffff". In this case a run-time error is actually thrown.While the name
UnsupportedValueError
fits case 1, I'd suggest the nameUnexpectedValueError
, which would fit both cases.