You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
exportconstFoo=(()=>{// ^?// Wouldn't `class Foo` be just as accurate yet more informative?classFoo{// ^?}returnFoo;// ^?})();// compare to Bar, it has a type of `class Bar`exportclassBar{// ^?}constx=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.
The text was updated successfully, but these errors were encountered:
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.
π Search Terms
IIFE, Quick Info, Type Display, Type Inference
β Viability Checklist
β 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:
Expected
const Foo
's type would beclass Foo
Actual
const Foo
's type istypeof Foo
π» Use Cases
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.The text was updated successfully, but these errors were encountered: