Skip to content

Question marks (JSDoc types) allowable in instantiation expression type arguments  #51802

Closed

Description

@jcalz

Bug Report

🔎 Search Terms

instantiation expressions, question mark, jsdoc nullable, any, Java wildcard

🕗 Version & Regression Information

⏯ Playground Link

Playground link with relevant code

💻 Code

function foo<T>(x: T): T { return x }
const WhatFoo = foo<?> // (x: any) => any 😕
const HuhFoo = foo<string?> // (x: string | null) => string | null 😲
const NopeFoo = foo<?string> // (x: string | null) => string | null 😵
const ComeOnFoo = foo<?string?> //  (x: string | null) => string | null ¿🤪?

class Bar<T> { constructor(public x: T) { } }
const WhatBar = Bar<?>; // Bar<any> 😕
const HuhBar = Bar<string?>; // Bar<string | null> 😲
const NopeBar = Bar<?string> // Bar<string | null> 😵
const ComeOnBar = Bar<?string?> // Bar<string | null> ¿🤪?

🙁 Actual behavior

No compiler errors. The question marks seem to be treated like JSDoc nullable types and a question mark by itself seems to be interpreted as any (not sure if that's true for JSDoc either).

🙂 Expected behavior

Compiler errors on every question mark, saying that it's not valid, perhaps with a "JSDoc types are not allowed here" message.


This Stack Overflow question is asking about how to use the Java wildcard syntax in TypeScript, and I'm like 🤯. Apparently this was suggested by VSCode type hints. I don't know much about JSDoc but I assume some support for it has accidentally leaked into instantiation expression checking.

Activity

  1. mike-lischke commented on Dec 7, 2022

    @mike-lischke

    Note that without the question marks my code does not compile.

  2. jcalz commented on Dec 7, 2022

    @jcalz
    ContributorAuthor

    Nor should it. You can change ? to any to get it to compile (with all the caveats that come from using any).

  3. mike-lischke commented on Dec 7, 2022

    @mike-lischke

    any is forbidden in my code base and unknown doesn't compile.

  4. fatcerberus commented on Dec 7, 2022

    @fatcerberus

    If your codebase forbids use of any (and there are definitely good reasons to do so!) then using an undocumented TS bug to instantiate a ModuleBase<any, any> feels like cheating and probably shouldn't pass code review.

  5. jcalz commented on Dec 7, 2022

    @jcalz
    ContributorAuthor

    Mike Lischke (@mike-lischke) That's not really relevant to this issue, unfortunately. By using ? you are essentially using any. I understand that your workflow depends on spacebar heating but this issue is about the (presumable) bug.

  6. MartinJohns commented on Dec 7, 2022

    @MartinJohns
    Contributor

    any is forbidden in my code base and unknown doesn't compile.

    You control the linter. Don't let the linter control you.

  7. mike-lischke commented on Dec 7, 2022

    @mike-lischke

    If your codebase forbids use of any (and there are definitely good reasons to do so!) then using an undocumented TS bug to instantiate a ModuleBase<any, any> feels like cheating and probably shouldn't pass code review.

    I just detected the possibility of using question marks and asked about it on SO, which in turn let to this issue here. Additionally, it solved my problem at hand and it's not yet clear if it is a bug ¯\(ツ)/¯

  8. fatcerberus commented on Dec 7, 2022

    @fatcerberus

    and it's not yet clear if it is a bug ¯(ツ)/¯

    That's fair, though it definitely feels like a bug since ? doesn't work as an alias for any in any other context and TS doesn't have existential types (what Foo<?> would be most likely to denote if that syntax were officially supported).

    We'll see what the maintainers say.

  9. ahejlsberg commented on Dec 7, 2022

    @ahejlsberg
    Member

    This is definitely a bug. We're forgetting to visit child nodes in checkExpressionWithTypeArguments.

  10. locked as resolved and limited conversation to collaborators on Oct 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions