Closed
Description
π 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:
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
- 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.
- What shortcomings exist with current approaches? They appear uninformative at first glance.
- 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.