Closed
Description
Bug Report
🔎 Search Terms
instantiation expressions, question mark, jsdoc nullable, any, Java wildcard
🕗 Version & Regression Information
- This is the behavior in every version since Instantiation expressions #47607 introduced instantiation expressions (TypeScript 4.7 and up)
⏯ 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.