-
Notifications
You must be signed in to change notification settings - Fork 5
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
Checking errors via @ts-expect-error
?
#65
Comments
Sorry for the delay - I like the idea! For visibility, microsoft/TypeScript#19139 is a long-standing feature request on the TypeScript side to add this kind of check natively. It's blocked because TypeScript's error codes and diagnostic message texts are not stable. Having a userland tool such as A few things we should figure out...
My instinct is to make the rule strict by default so we don't have a plethora of odd user forms. Perhaps only the three following formats:
...where if What do you think @rauschma? Another side note: microsoft/TypeScript#38370 tracks quirks in the TypeScript side around parsing lines. Beware to any implementer of this issue that it's easy to get the edge cases wrong. (trust me, I know! 😄) Edit 12/4: Oh, and...
|
For reference, this is what an error message looks like in the TS Playground:
This is what it looks like at the command line (tsc):
Thoughts:
Examples (suffixed error numbers): // @ts-expect-error: Property 'hello' does not exist on type '"abc"'. (TS2339)
// @ts-expect-error: (TS2339)
// @ts-expect-error: Property 'hello' does not exist [...] (TS2339)
// @ts-expect-error: Property 'hello' does not exist [...] Examples (prefixed error numbers): // @ts-expect-error TS2339: Property 'hello' does not exist on type '"abc"'.
// @ts-expect-error TS2339
// @ts-expect-error TS2339: Property 'hello' does not exist [...]
// @ts-expect-error: Property 'hello' does not exist [...] What are your thoughts? |
Note that there's no api in TS to speculatively ask questions. This is important because it means that the diagnostics and types you get are what you get. Put another way - if TS suppresses an error - it's gone. You would need to manually re-check the code without the suppression in order to get the error. Which would make this a lot harder (and slower) to do than with a custom comment. I'd assume this is why they used custom comments in the dtslint project. |
@bradzacher Right! That reminds me of the workaround that I used when I implemented similar functionality via the tsc API: I replaced each |
Here's the code in literate-ts that I used to match errors in Effective TypeScript. The sequence was:
IIRC, this never worked perfectly, but worked well enough to be useful in final editing. |
This limitation is a little irksome. Filed microsoft/TypeScript#51749. |
In my TypeScript book, I’m using
@ts-expect-error
with error messages to point out if there are errors – e.g.:Benefit: Very similar to
assert.throws()
in that type checking doesn’t fail if everything is as expected.All examples from my book: https://gist.github.com/rauschma/4d1ee06e47e03545d4c4b31b59700786
The text was updated successfully, but these errors were encountered: