Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “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? Sign in to your account

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

Closed
jcalz opened this issue Dec 7, 2022 · 9 comments · Fixed by #51804
Closed

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

jcalz opened this issue Dec 7, 2022 · 9 comments · Fixed by #51804
Assignees
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue

Comments

@jcalz
Copy link
Contributor

jcalz commented Dec 7, 2022

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.

@mike-lischke
Copy link

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

@jcalz
Copy link
Contributor Author

jcalz commented Dec 7, 2022

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

@mike-lischke
Copy link

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

@fatcerberus
Copy link

fatcerberus commented Dec 7, 2022

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.

@jcalz
Copy link
Contributor Author

jcalz commented Dec 7, 2022

@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.

@MartinJohns
Copy link
Contributor

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

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

@mike-lischke
Copy link

mike-lischke commented Dec 7, 2022

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 ¯\(ツ)

@fatcerberus
Copy link

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.

@ahejlsberg
Copy link
Member

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

@ahejlsberg ahejlsberg self-assigned this Dec 7, 2022
@ahejlsberg ahejlsberg added the Bug A bug in TypeScript label Dec 7, 2022
@ahejlsberg ahejlsberg added this to the TypeScript 5.0.0 milestone Dec 7, 2022
@typescript-bot typescript-bot added the Fix Available A PR has been opened for this issue label Dec 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants