-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Generic Conditional Type resolved to any
when passed an error any
type
#59981
Comments
Related to #58960, possible cause outlined here (#58960 (comment)) |
any
when generic is an implicit any
from typescript errorany
when passed an error any
type
The problem stems from how TypeScript infers types from modules. If "some-module" exports a default value with any (but the type isnβt explicitly declared), TypeScript might not treat it exactly the same as any. To resolve this, you should explicitly declare the default export as any in your module.
declare module "some-module" {
const _default: any;
export default _default;
}
type IsAny<T> = 0 extends 1 & T ? true : false;
type ImplicitAny = typeof import("some-module").default;
type Result = IsAny<ImplicitAny>; // true
type ExpectedResult = IsAny<any>; // true β― Here's you can check by ts live-code : // Declare the module "some-module" with an explicit 'any' type for the default export
declare module "some-module" {
const _default: any;
export default _default;
}
// A utility type to check if... |
@Eazash you have already found the other issue about it, this is no different from it. I think it would be best to close this issue to keep the discussion focused in the original thread about this change. |
@Andarist In my opinion, there is a difference. The other issue is referring to the "Unresolved |
Both are just the same for the compiler. It still feels to me that you could move this discussion to the other issue and try to advocate there that your use case is different than the one presented there (IMHO it's still the same but that's beyond this argument :P). As Ryan said there, once you have an error "all bets are off" π
This isn't technically true. Sometimes it returns both :D type Test = any extends string ? 1 : 2
// ^? type Test = 1 | 2 EDIT:// I now realized the other issue is closed, so I'll give you that. I think it doesn't make sense to comment much on closed issues so perhaps a new one is appropriate here. I anticipate it will be closed with the same conclusion though. |
@lucaimbalzano The playground I linked to is a simplified version of a downstream bug (see nuxt/nuxt#29021). The intention was to show that the implicit
In this scenario, "some-module" does not have default exports. Also "some-module" would be any number of third-party modules, explicitly defining
Thank you, I stand corrected. However, the bug still persists for a conditional like |
The propagating behavior of the error You should write this instead if you'd prefer the other behavior type PossibleDefault<T> = T extends { default: infer U } ? U : any;
type ImplicitAny = PossibleDefault<typeof import("some-module")>; |
This issue has been marked as "Not a Defect" and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
π Search Terms
"generic", "conditional type", "is any", "implicit any", "error", "module has no export member"
π Version & Regression Information
β― Playground Link
https://www.typescriptlang.org/play/?ts=5.5.4#code/C4TwDgpgBAkgzgQQHYgDwBUB8UC8UAMUEAHsBEgCZxQCMUAZFOlAPxTABOArtAFxQAzAIYAbOBABQEihADGIoR2gBbAPYUuI6ACI4q5RAC0ajVu1QA3gF8JoSLGVgRAS1nPgyELnbgIqgVDOjqocwAAUuvpGJpoQ2gCUAHQywprAUnbQAEoQcGne8J6oMI4ubh4omBIA9NVQ9Q0AeiwZvlAAosSQsmQUOXkiwAWIKKhClTV19c1AA
π» Code
π Actual behavior
The conditional type resolves to
any
π Expected behavior
The conditional type should resolve to either TrueType or FalseType. In this specific example, it should resolve to
true
Additional information about the issue
While I was unable to use
every-ts
, after manually bisecting I've foudn that the first release with this buggy behaviour is 5.5.0-dev.20240524The text was updated successfully, but these errors were encountered: