Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

Open
Labels
BugA bug in TypeScript
Milestone
@ntninja

Description

@ntninja

TypeScript Version: 3.2.0-dev.20180927
Search Terms: JSDoc conditional types

UsingConditional Types in JSDoc comments confuses the TypeScript parser since theT extends Y ? A : B syntax looks similar to theY? (meaningY|null) syntax from JSDoc.

Code

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

Expected behavior:

No error should be reported.
The type ofObject_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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScript

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp