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

Unresolved any behaves different from any #58960

Closed
remcohaszing opened this issue Jun 21, 2024 · 7 comments
Closed

Unresolved any behaves different from any #58960

remcohaszing opened this issue Jun 21, 2024 · 7 comments
Labels
Not a Defect This behavior is one of several equally-correct options

Comments

@remcohaszing
Copy link

🔎 Search Terms

“unresolved any”

🕗 Version & Regression Information

  • This changed between versions 5.4.5 and 5.5.2

⏯ Playground Link

https://www.typescriptlang.org/play/?&filetype=ts#code/PTAEBcE8AcFNQAoCcD20DOoC8oCGA7SAKCjlADEUVtFUMiSZ4BZAEQA0BhFfcWX5Gkw4AFAAZQsAB598AE0wBGUADIKVUAH5QAbwC+oAFzqUASiJA

💻 Code

// type Props = any

type MDXContentProps = (0 extends 1 & Props ? {} : Props)

🙁 Actual behavior

The type of MDXContentProps is any

🙂 Expected behavior

The type of MDXContentProps is {}

Additional information about the issue

When you hover over a type that doesn’t exist, the editor shows /*unresolved*/ any. In TypeScript 5.4, this type was equivalent to any. In TypeScript 5.5, it behaves slightly different. This can be seen in the playground by toggling the line type Props = any and hovering the MDXContentProps type.

If an unresolved any is assigned to another type, that type becomes equivalent to an unresolved any, except the /*unresolved*/ label is gone when hovering over it.


I use this pattern in MDX analyzer (https://github.com/mdx-js/mdx-analyzer/blob/[email protected]/packages/language-service/lib/virtual-code.js#L96). This allows users to optionally define props for their MDX file. This used to work great, but now everything is just inferred to any.

If there’s a better way to detect whether a type is defined, I would love to hear about it.

@RyanCavanaugh RyanCavanaugh added the Not a Defect This behavior is one of several equally-correct options label Jun 21, 2024
@RyanCavanaugh
Copy link
Member

If you're ts-ignoreing something in declaration space, all bets are off. Do not take dependencies on discoveries made this way.

@so1ve
Copy link

so1ve commented Jun 21, 2024

Any way to detect if a type exists?

@uhyo
Copy link
Contributor

uhyo commented Jun 22, 2024

Even though it is not a defect, I'm very curious which PR caused this change. 👀

@Andarist
Copy link
Contributor

This changed in 5.5.0-dev.20240524, here is the diff. Likely "culprit" would be: #58610

@remcohaszing
Copy link
Author

If you're ts-ignoreing something in declaration space, all bets are off.

I think this is fair. My example abuses ts-ignore to check whether a type exists (due to the lack of a better alternative). I think it would be totally fair for TypeScript to change this type to unknown for example, or even a special type unresolved.

However, TypeScript uses a type that looks like any on inspection, but doesn’t behave like any. This I would call a defect. Another practical example is that this type could come from a dependency if the user uses skipLibCheck true.

The following mechanism appears to work between TypeScript versions to check whether a type is any, unknown, or unresolved any. (playground)

interface Fallback {
    fallback: string
}

type Preferred = any

export type Wanted = void extends Preferred ? Fallback : Preferred

remcohaszing added a commit to mdx-js/mdx-analyzer that referenced this issue Jun 23, 2024
This solution solves an upstream regression and is backwards compatible
for our use case.

Refs microsoft/TypeScript#58960
remcohaszing added a commit to mdx-js/mdx-analyzer that referenced this issue Jun 23, 2024
This solution solves an upstream regression and is backwards compatible
for our use case.

Refs microsoft/TypeScript#58960
remcohaszing added a commit to mdx-js/mdx-analyzer that referenced this issue Jun 23, 2024
This solution solves an upstream regression and is backwards compatible
for our use case.

Refs microsoft/TypeScript#58960
@typescript-bot
Copy link
Collaborator

This issue has been marked as "Not a Defect" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Jun 25, 2024
@remcohaszing
Copy link
Author

remcohaszing commented Jun 25, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Not a Defect This behavior is one of several equally-correct options
Projects
None yet
Development

No branches or pull requests

6 participants