Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upGitHub is where the world builds software
Millions of developers and companies build, ship, and maintain their software on GitHub — the largest and most advanced development platform in the world.
Allow to explicitly pass parameter types via JSDoc #27387
Comments
|
I would also like to see something like this. Currently it's really hard to use functions with generic types from JS. |
|
Another use case with React Hooks: // useState<string> is derived correctly
const [aString, setAString] = useState("default value");Removing default value there is no way to pass type info: // useState<any>
const [aString, setAString] = useState(); |
|
Anything on this? Also stuck on the useState example as shared above: // Doesn't set type properly
const [error, setError] = /** @type {React.useState<?string>} */ useState(null);
// Nor does this
/** @type {[?string, React.Dispatch<React.SetStateAction<?string>>]} */
const [error, setError] = /** @type {React.useState<?string>} */ useState(null);
// we have an error we want to set now.
setError('Error!') // Shows a type errorEdit: Ok this might be a hack, but in the case of the useState example it works: const [error, setError] = useState(/** @type {?string} */ (null));implied types for error are i think what's happening "under the hood" is we are forcing the "type" that useState is being passed. |
|
For functions that take an argument of the generic type, casting the argument itself is reasonably practical. We've settled on these patterns with const [thing, setThing] = useState(/** @type {SomeType|null} */ (null));
const widgetRef = useRef(/** @type {HTMLElement|null} */(null));In cases where there is no argument for everything else to be inferred from, it gets much more verbose unfortunately. What would be helpful is being able to do something like: const aSet = /** @type {?<string>} */(new Set());Where the |
Something like that would be awesome, it would majorly simplify the code required for - const Component = /** @type {React.ForwardRefExoticComponent<React.RefAttributes<Props>>} */ (React.forwardRef(() => { ... }))
+ const Component = /** @type {?<Props>} */ (React.forwardRef(() => { ... })) |

Formed in 2009, the Archive Team (not to be confused with the archive.org Archive-It Team) is a rogue archivist collective dedicated to saving copies of rapidly dying or deleted websites for the sake of history and digital heritage. The group is 100% composed of volunteers and interested parties, and has expanded into a large amount of related projects for saving online and digital history.

Search Terms
jsdoc generics type parameters constraints
Suggestion
It seems like it is not possible via JSDoc to explicitly tell compiler which type parameters to pass to a generic function.
Use Cases
In TypeScript it is possible to explicitly pass type parameters such as
mongoose.model<Model, Schema>('modelName', Schema)while I could not find a way to do same with JSDoc.Examples
mongoose.modelhas two signatures, first one with one type parameter and the other with two. To make use of the second signature we must pass types explicitly.My apologies if this is already possible, but I've spend almost a week battling this.
Related: microsoft/TypeScript-Node-Starter#101
Checklist
My suggestion meets these guidelines: