-
Notifications
You must be signed in to change notification settings - Fork 325
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
Export type declarations for component source code #2835
Comments
here's an example @cloudratha did for the hackney fork of this lib. doing it here would probably look similar: |
Thanks @jhackett1 for raising this issue I know things have been quiet (or silent) from our end on this but it's certainly been talked about. For now, you're likely seeing problems because Microsoft have some more tips here for regular JavaScript:
Generated type declarations in futureThat said, we appreciate type declarations are a brilliant way of bringing documentation straight to the code What could we do in the future?
Mainly with 2) in mind, we've started taking a look at improving our JSDoc comments as we know the TypeScript compiler (and your editor or IDE's language server) will automatically pick these up Automatic type discovery nowWhat do we have already? If you're using an editor or IDE (Visual Studio Code, Sublime Text, WebStorm) with plugins that wrap the TypeScript standalone server (aka I had a quick try and our JSDoc comments (where we use them) are appearing already 😊 For anyone else searching I'll put these notes here Thanks again |
To add to this, our workflows over at the Planning Inspectorate would benefit from exported types. For now we are defining our own types to go along with the library. We use plain JavaScript instead of TypeScript, but TS is key to understanding the types a library needs, and is what vscode uses to check code too. We often have a need to know the type of a nunjucks macro function - i.e. what data can we pass to a particular macro; being able to check the types for that (and have it autocomplete etc...) would be useful. |
We're going to put this one back in the backlog for a bit, the time to prioritise our work. |
Just a note from TypeScript’s Publishing guide as they suggest types are published by either:
I’ve picked the easiest option 1) to generate types alongside source (#4094, #4154) so this caveat applies:
Which would then steer us away from DefinitelyTyped:
But to avoid accidentally-breaking type changes, perhaps this still leaves an optional Type checking NunjucksIn future, we could consider a custom filter or function (opt-in only) that enables Nunjucks type checking:
The filter could log warnings or throw errors at Nunjucks compile time when options are deprecated or invalid: govukAccordion({
rememberExpanded: false
} | validate('AccordionConfig')) AutocompleteUnfortunately, code autocomplete via types would only be available in JavaScript templating languages. We'd need to move our YAML documentation into JSDoc to support this and I've demonstrated JavaScript versions of govuk-frontend/shared/lib/components.js Lines 154 to 156 in 39c0a23
JavaScript versions of If support was needed, these wrappers could be used in JSX and other JavaScript templating languages |
To share an update on this, I've contributed a community |
it would be great if this library declared its types, to make it easier to understand what i can import from it and how it can be used.
at the moment, when i import it in a typescript project, i get an error:
i could perhaps make a pull request for this if it's wanted?
Context
Alternatives
i could define some a declaration file in my own project, but that would only solve the problem for me!
Additional information (if applicable)
for a library like this, including types is a useful form of self-documentation. it's nice to be able to import a library and see intellisense suggestions for what i can do with it without having to refer to a documentation website.
for example, this works, but my editor doesn't "know" that it works:
The text was updated successfully, but these errors were encountered: