Currently, I am working on a project where I utilize JSDoc for type annotations and typescript definition files to simplify the creation of those types. As part of this process, I am transitioning some generic types from JSDoc to typescript due to its ease of use.
One issue I encountered was when trying to directly assign a generic type to a variable, which resulted in an error prompting me to provide type arguments. For example:
/*
* @type {import('./api').WithDb}
*/
const withDb = handler => async args => {
This failed because WithDb requires two arguments, even though my intention was to indicate that it is a generic function. Fortunately, JSDoc allows for defining generic types using the @template
syntax, allowing me to re-annotate the function like so:
/*
* @template T
* @template K
* @type {import('./api').WithDb<T,K>}
*/
const withDb = handler => async args => {
Another variant could be:
/*
* @template T,K
* @type {import('./api').WithDb<T,K>}
*/
const withDb = handler => async args => {
However, this approach involves quite a bit of repetitive code that I would prefer to avoid. Is there an alternative method to bind the types other than using @type
?