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

Inline IIFE return type when assigned to a variable of the same name. #56036

Closed
5 tasks done
DanKaplanSES opened this issue Oct 9, 2023 · 4 comments
Closed
5 tasks done
Labels
Not a Defect This behavior is one of several equally-correct options

Comments

@DanKaplanSES
Copy link

DanKaplanSES commented Oct 9, 2023

πŸ” Search Terms

IIFE, Quick Info, Type Display, Type Inference

βœ… Viability Checklist

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals

⭐ Suggestion

When an IIFE's return type shares the same name as the variable it's assigned to, the type information of the variable appears self-referential (const Foo: typeof Foo); I'd like to recommend inlining the variable's type to be equivalent to the IIFE's return type.

πŸ“ƒ Motivating Example

This may be a "garbage-in, garbage-out" situation, but I'm working with legacy JS code that frequently uses this pattern:

image

As code:

export const Foo = (() => {
  //         ^?
  //          Wouldn't `class Foo` be just as accurate yet more informative?
  class Foo {
    //  ^?
  }

  return Foo;
  //     ^?
})();

// compare to Bar, it has a type of `class Bar`
export class Bar {
  //         ^?
}

const x = Foo;
//    ^?

Expected

const Foo's type would be class Foo

Actual

const Foo's type is typeof Foo

πŸ’» Use Cases

  1. What do you want to use this for? This feature would reduce cognitive load in certain legacy code bases, reducing the effort in porting JS to TS. I'm expecting my use case to be too niche to be considered, but I think inlining the return type of IIFEs could make the tooltip more helpful in other (/most?) scenarios.
  2. What shortcomings exist with current approaches? They appear uninformative at first glance.
  3. What workarounds are you using in the meantime? I tinkered around with the code enough to understand what the typeof Foo really refers to. Alternatively, you can rename the variable or the IIFE's return type. But in a legacy code base, that could introduce risk.
@MartinJohns
Copy link
Contributor

But class Foo doesn't exist at that scope.

@DanKaplanSES
Copy link
Author

But class Foo doesn't exist at that scope.

True. I don't have a lot of language design experience; does that introduce significant problems and/or challenges? I'm not tied to the solution in my "Expected" section if there's a better one. Personally, if it inlined the "shape" of the class, I'd like that better, but I saw other issues requesting that quick infos go in the opposite direction, so I didn't propose that above.

@fatcerberus
Copy link

fatcerberus commented Oct 9, 2023

if it inlined the "shape" of the class, I'd like that better

That’s not currently feasible; see #41581 and #41587

@RyanCavanaugh
Copy link
Member

This would arguably be more confusing - there isn't a class Foo in scope here; you can't use it as a type.

@RyanCavanaugh RyanCavanaugh added the Not a Defect This behavior is one of several equally-correct options label Oct 9, 2023
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

4 participants