microsoft / microsoft/TypeScript
JSDOC generics bug for callbacks when args are present
Open
Nobody has claimed this yet.
Bug
Domain: JSDoc
Help Wanted
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
🔎 Search Terms
"Generics", "JSDOC", "Callback"
🕗 Version & Regression Information
TS 5.7.3
v5.8.0-dev.20250131
⏯ Playground Link
💻 Code
In the following example, define() should return VC<S>, but when arg callback has args it does not resolve S and returns VC<any>.
/**
* @template S
* @param {(arg0: { observer: EO }) => S} callback
* @param {Options} [options]
* @returns {VC<S>}
*/
/*
* @type { <S>(fn: (arg0: { observer: EO; }) => S, options?: Options) => VC<S> }
*/
function define(callback, options) {
const { name } = options ?? {}
const observer = new EO()
const state = callback({ observer })
return new VC(state)
}
/**
* @template S
*/
class VC {
/** @type {S} */
state
/**
* @param {S} state
*/
constructor(state) {
this.state = state
}
}
/** @typedef {{ name?: string }} Options */
class EO {}
// Arg `callback` with args - JSDOC doesn't resolve S.
// JSDOC const v1: VC<any>
// TS const v1: VC<boolean>
const v1 = define((arg0) => true, { name: 'default' })
// Arg `callback` without args - JSDOC does resolve S.
// JSDOC const v2: VC<boolean>
// TS const v2: VC<boolean>
const v2 = define(() => true, { name: 'default' })
🙁 Actual behavior
JSDOC doesn't resolve S
🙂 Expected behavior
JSDOC should resolve S in the same way as TS.
Additional information about the issue
No response
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the linked TypeScript Playground repro and compare the inferred types of v1 and v2 with the expected TypeScript results. Trace the JSDoc generic callback inference path in the compiler, then add a regression test showing that v1 is inferred as VC when the callback has an argument.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, typescript
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100