wikimedia-gadgets / types-mediawiki

TypeScript definitions for MediaWiki JS interface
GNU General Public License v3.0
23 stars 9 forks source link

`wg` variables are not autocompleted #37

Closed jwbth closed 7 months ago

jwbth commented 8 months ago

Relatively minor one, but the one thanks to which I decided to install this package: wg variables are only autocompleted (in VS Code) when you type mw.config.values.wg, but not when you type mw.config.get('wg.

image Not sure this is easy to fix, but I would welcome it.

Derugon commented 8 months ago

A proposed solution to this issue is implemented in commit https://github.com/wikimedia-gadgets/types-mediawiki/pull/36/commits/e47e9ae4cc932950314dc54e75902419391f7022, from MR https://github.com/wikimedia-gadgets/types-mediawiki/pull/36.

This fix involves using an additional ExtensibleMap<V, TX> interface, which simulates both a Map<V> for known keys and a Map<{ [k: string]: TX; }> for unknown keys.

There may be a simpler way to do it, Basically we could define mw.config as Map<V> & Map<{ [k: string]: TX; }>, but this would manage the 2 method implementations as overloads, so when writing

mw.config.get(["skin", "someUnknownKey", "stylepath"])

we would get autocompletion when writing "skin", but not when writing "stylepath" since after having written "someUnknownKey", the linter will switch from the Map<V> definition of the get method to the Map<{ [k: string]: TX; }> definition of this same method. After inlining type variables this 2nd signature simplifies to

get<S extends string[]>(selection: S, fallback?: any): { [P in S]: TX; };

so we no longer get autocompletion, but worse: properties of the result object will all have type TX, instead of string for skin and stylepath.

Derugon commented 7 months ago

PR #36 got merged, so the fix mentioned above has been applied (for the next npm release).

siddharthvp commented 7 months ago

In v1.6.0, this works for TypeScript files. It still doesn't work for JavaScript files though. Unlike TS autocompletion which is powered by typescript-language-server, JS autocompletion is IDE-dependent. So don't think there's much we can do about that.

siddharthvp commented 7 months ago

It still doesn't work for JavaScript files though. ... JS autocompletion is IDE-dependent

It wasn't working in WebStorm, but it does work in VS Code. @jwbth you're in luck!