TypeScript: Error message related to JSDoc for non-JSDoc syntax error
TypeScript Version:
- 3.4.0-dev.20190130
- 3.2.4
Search Terms:
- ts8020
- “jsdoc types can only be used”
- “JSDoc types can only be used inside documentation comments”
Code
function isString(str: string): str is string! {
return typeof str === 'string';
}
Expected behavior:
Error message should not mention JSDoc syntax. IMHO it should be TS1144: '{' or ';' expected., which is the case with this code for example:
function isString(str: string): str is string? {
return typeof str === 'string';
}
Actual behavior:
Error message states that this is JSDoc syntax (am not sure it even is). Full output of tsc:
> tsc test.ts
test.ts:1:40 - error TS8020: JSDoc types can only be used inside documentation comments.
1 function isString(str: string): str is string! {
~~~~~~~
Found 1 error.
Playground Link: here
Related Issues:
- PR #17250
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 3
- Comments: 17 (5 by maintainers)
I don’t understand why the suggested error message is better. Maybe we shouldn’t mention JSDoc to begin with and that’s the issue. The error message should probably be something like
and
Generalized to
when I mistakenly declared a function parameter as
options:? ParseUrlOptionsI also got theJSDoc types can only be used inside documentation commentserror message which was very confusing to me, I don’t think JSDoc should be mentioned instead of the actual error.Yeah coming from Flow (which uses
?forMaybetypes), I found the mention of JSDoc confusing.+1 for saying which character is unexpected and perhaps a “did you mean” comment like @DanielRosenwasser suggests above.
Not to necrobump this. but @DanielRosenwasser has made some good suggestions here. This issue is still present, and confusing initially, for folks coming from Flow. Hope Daniel’s ideas are implemented at some point in the future.
@DanielRosenwasser My words exactly, JSDoc shouldn’t be mentioned here 🙂
@pbrahmbhatt3 I’m not sure if your PR fixes the same bug that this issue is about
Another example case where I ran into this just now was with the following code:
My brain was thinking of optional parameter syntax (though got that wrong too) when I ran into this:
(I’m also a little confused/unsure as to why it thinks the type is
string | nullhere, which sort of led me on a little bit of a rabbithole since the type I was expecting based on how optional parameters work wasstring | undefined)+1 to removing a mention of JSDoc (or lessening it); I had gotten this error from typing
let val: Type!instead oflet val!: Type; the reference to JSDoc confused me until I found this issue.+1