Comments (8)
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" π
A conditional type by definition should return from either the true or false branch.
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.
from typescript.
@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 any
created from an "No export error" changes the behaviour of the conditional type.
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.
In this scenario, "some-module" does not have default exports. Also "some-module" would be any number of third-party modules, explicitly defining any
is not an option
This isn't technically true. Sometimes it returns both :D
Thank you, I stand corrected. However, the bug still persists for a conditional like type IsTwo<T> = T extends 2 ? true : false
and IsTwo<ImplicitAny>
is supposed to be a boolean but becomes any
(see this playgroud)
from typescript.
Related to #58960, possible cause outlined here (#58960 (comment))
from typescript.
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.
Here's how:
- Explicitly Declare the Module's Default Export as any:
declare module "some-module" {
const _default: any;
export default _default;
}
- Type Checking with IsAny: Now, when you check IsAny, it should correctly detect the any type:
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...
from typescript.
@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.
from typescript.
@Andarist In my opinion, there is a difference. The other issue is referring to the "Unresolved any
" when a type doesn't exist, and how that exhibits different behavior. This issue is describing how an error any behaves differently from a regular any
. Unlike unresolved there is no label/comment on hover, yet it breaks the contract of a conditional type. A conditional type by definition should return from either the true or false branch. By returning any
it is quietly introducing a third branch.
from typescript.
The propagating behavior of the error any
is intentional, so that when you have one type error, it doesn't result in (potentially) tens of thousands of downstream errors.
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")>;
from typescript.
This issue has been marked as "Not a Defect" and has seen no recent activity. It has been automatically closed for house-keeping purposes.
from typescript.
Related Issues (20)
- Cannot access protected field but private field inside nested class
- No error for non-existing css side effect import with `noUncheckedSideEffectImports` flag on HOT 8
- CommonJS module output broken after updating from 5.5.4 to 5.6.2 HOT 7
- Type inference when using `extend` in a generic is not consistent HOT 3
- Check when a function that uses `this` is referenced without binding it HOT 2
- Built-in URL is not compatible with `@types/node` URL
- [suggest] constructor init delegation with "strictPropertyInitialization" HOT 2
- Potential memory leak or dead recursive during auto completion HOT 2
- Incorrect type deducted for accumulator in reduce HOT 2
- moduleResolution: 'bundler' with subpath import seem to not support directory module and extensionless import HOT 4
- Source mappings are missing for serialized properties
- Missing overload method on union types HOT 7
- JSDoc `@import` of "tslib" fails under `--moduleResolution bundler`
- Interface that extends another no longer constrains types like the original HOT 1
- Proposal: Allow isolated declarations to infer results of constructor calls HOT 10
- Property 'difference' does not exist on type 'Set' HOT 3
- Type narrowing of condition leads to unexpected error for assignment HOT 3
- Increase the distinction between class and interface properties when defining a class or interface
- Add adaptation to class and interface property prompts HOT 1
- Weird behavior with recursive generic types HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from typescript.