Git Product home page Git Product logo

Comments (8)

Andarist avatar Andarist commented on September 27, 2024 1

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.

Eazash avatar Eazash commented on September 27, 2024 1

@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

@Andarist

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.

Eazash avatar Eazash commented on September 27, 2024

Related to #58960, possible cause outlined here (#58960 (comment))

from typescript.

lucaimbalzano avatar lucaimbalzano commented on September 27, 2024

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:

  1. Explicitly Declare the Module's Default Export as any:
declare module "some-module" {
  const _default: any;
  export default _default;
}
  1. 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...

Playground Link

from typescript.

Andarist avatar Andarist commented on September 27, 2024

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

Eazash avatar Eazash commented on September 27, 2024

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

RyanCavanaugh avatar RyanCavanaugh commented on September 27, 2024

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.

typescript-bot avatar typescript-bot commented on September 27, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.