question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

In JSDoc, `?` of conditional is frequently parsed as postfix-`?`

See original GitHub issue

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 the Y? (meaning Y|null) syntax from JSDoc.

Code

/**
 * @template {{}} T
 * @param {T} o
 * @returns {T extends Readonly<infer U> ? (keyof U)[] : string[]}
 */
function Object_keys(o) {
	let keys = Object.keys(o);
	// @ts-ignore: Type assertion for stripping `Readonly<…>`
	return keys;
}

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:

  1. When encountering a top-level (not within parenthesis) ? 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.)
  2. Prohibit the JSDoc ? 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.

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:17
  • Comments:11 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
sandersncommented, Jul 29, 2020

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 [?string?] as long as they’re around.

I guess we could still get rid of the jsdoc interpretation of string? as string | null, if we made sure it wasn’t too big of a breaking change.

1reaction
sandersncommented, Jul 28, 2020

That may be as close as we get to closing this bug then, because tuples now use postfix-? to indicate optional elements: [number,string?]. That means we can’t get rid of postfix-? parsing entirely.

Read more comments on GitHub >

github_iconTop Results From Across the Web

JSDoc Reference - TypeScript: Documentation
The meaning is usually the same, or a superset, of the meaning of the tag given at jsdoc.app. The code below describes the...
Read more >
JSDoc: Is conditional ignore possible? - Stack Overflow
You can implement your own jsdoc plugin to test an ignore condition and set its value in doclet.ignore property.
Read more >
pratt-expr - npm Package Health Analysis | Snyk
To cover the most common, simple cases of prefix, postfix, ... const elseArm = parser.parse(this.precedence - 1); return new ConditionalExpr(cond, thenArm, ...
Read more >
graphics.stanford.edu/~danielh//js2-hacked.el
This is unusual for Emacs modes but common in IDEs like Eclipse. ... Parser variables (deflocal js2-parsed-errors nil "List of errors ...
Read more >
Code Issues - Embold Help Center
The exception is caught by value. It could be caught as a (const) reference which is usually recommended in C++. Robustness. clarifyCalculation, Suspicious ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found