-
Notifications
You must be signed in to change notification settings - Fork 12.8k
In JSDoc, ?
of conditional is frequently parsed as postfix-?
#27424
New issue
Have a question about this project? No Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “No 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? No Sign in to your account
Comments
?
operator when parsing conditional types
(2) is probably the best option if the implementation isn't too messy. |
I also just ran into this bug (v3.4.3), was very confused about how the |
After discussion with the JSDoc people, who talked to Closure people, here's option (4):
|
?
operator when parsing conditional types?
of conditional is frequently parsed as postfix-?
👍 for (4) |
Found same problem when reprinting AST from jsdoc |
I’ve found that moving the /**
* @template {{}} T
* @param {T} o
* @returns {T extends Readonly<infer U>
? (keyof U)[] : string[]} */
function Object_keys(o) {
return /** @type {any} */ (Object.keys(o));
} |
It works correctly both with and without the new line. |
That may be as close as we get to closing this bug then, because tuples now use postfix-? to indicate optional elements: |
Well, And the I would however expect that Note that |
I would like to get rid of jsdoc-style postfix-? semantics entirely, including the parsing. It's an old syntax copied from Closure that Closure included for backward compatibility. Its remnants are going to cause minor errors like I guess we could still get rid of the jsdoc interpretation of |
TypeScript Version: 3.2.0-dev.20180927
Search Terms: JSDoc conditional types
Using Conditional Types in JSDoc comments confuses the TypeScript parser since the
T extends Y ? A : B
syntax looks similar to theY?
(meaningY|null
) syntax from JSDoc.Code
Expected behavior:
No error should be reported.
The type of
Object_keys
should be:<T extends {}>(o: T): T extends Readonly<infer U> ? (keyof U)[] : string[]
.Actual behavior:
TypeScript compiler shows an error ("?" expected) and mistakes the existing
?
operator for JSDoc<type>|null
syntax.The actual type therefor ends up being:
<T extends {}>(o: T): T extends Readonly<infer U> | null ? (keyof U)[] : string[]
.Possible fixes:
?
token in an extends clause, scan ahead and check whether it is followed by an|
or&
token (type intersection or union operators). If yes, treat it as|null
and keep processing; otherwise, assume it's the start of a conditional type declaration. (Requires at least an LF(2) parser.)?
operator in the top-level of an extends clause, always causing it to be treated as the start of the conditional type declaration.Playground Link: None, Playground does not seem to support JavaScript with JSDoc instead of TypeScript as input.
The text was updated successfully, but these errors were encountered: