Description
Several IDEs (including PHPStorm/WebStorm and VS Code) are using the typescript type definitions to enhance their autocompletion for Javascript as well.
While investigating #815, I made a small experiment: I generated a index.d.ts file in webpack-encore to have type definitions for the Encore public API (which is defined and documented in index.js). And this solved the type support for PHPStorm 2020.2.
Given that the type declaration can be fully generated from the existing JSDoc, I suggest that we bundle it in the package.
For the record, here is the command I ran (at the root of the package): tsc --declaration --allowJs --emitDeclarationOnly index.js
.
To make this production-ready, a few changes would be necessary:
- use a proper semver constraint for the typescript devDependency rather than
>=2.9
to be able to usetsc
in our publication process (no impact on projects using encore as that constraint is not enforced for enabling typescript features) - improve JSDoc types for arrays by specifying the value type (Typescript does not like unspecified Array types).
- (optionally) improve the JSDoc for callbacks to document their actual signature rather than using
{function}
(but this will be hard to go fully there: configuration callbacks are dealing with options of loaders, but we cannot refer to the type definitions shipped in these loaders for any optional dependency) - (optionally) improve the JSDoc for webpack-encore options to document the available options (for arguments where we mix loader options and Encore options, that's not possible for the same reason than above, but we have many methods with a separate argument for Encore options)
- add a
prepublishOnly
script doing that type generation before publishing the releases to npm
All the improve JSDoc
steps could actually be done even without shipping typescript type declarations, as they would still proper better type definitions. And actually, I have part of them done locally as part of my experiment.
What do you think @Lyrkan @weaverryan ?
My own vote would be in favor of including them, given that we don't need to maintain the .d.ts
file manually.