Skip to content
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

JSDOC generics bug for callbacks when args are present #61090

Open
simonyarde opened this issue Jan 31, 2025 · 2 comments Β· May be fixed by #61013
Open

JSDOC generics bug for callbacks when args are present #61090

simonyarde opened this issue Jan 31, 2025 · 2 comments Β· May be fixed by #61013
Labels
Bug A bug in TypeScript Help Wanted You can do this
Milestone

Comments

@simonyarde
Copy link

πŸ”Ž Search Terms

"Generics", "JSDOC", "Callback"

πŸ•— Version & Regression Information

TS 5.7.3
v5.8.0-dev.20250131

⏯ Playground Link

https://www.typescriptlang.org/play/?filetype=js#code/PQKgUGBSDKAiDyBhABAcwKYDt0CcCWAxgM7IBGArqsgGYD2OyBAhgDYulMEDWJA7gBZZkTHKhIj0yAA450RLABcIASUzIFgmrTa1eeTFXQAPJgFspLdABpkAAwAm6avvQAKAJS3kRfrXIt7ZFkFchw1WwA1RAAeaAA+WxsKBWQBIRFUMFtmNg5uL34mcVESPBT7WjlkTFoU2SJtADdJW2gvJkxA4NDMEkiYjoBPBIA6CBBgCFBwZBBkAAEFdHMWJiXkaDBZhakRM2QAb1cMgAYALkPkWlJ5HGacC4BReGQAX3dkAF44jdfGVnYnC4yC2c3muxw+wO8CkCjwtF6fwA2rRYfDegBdEHbebdMIkA5RWJxV6gyagUELBSDKSSA7IYmuaiYC7HUTnS7XW73J7wADcbw+3w2NlRcIRRAA-BcYeLekKfkT4m8yWBqORMAQ5chHM5sK4coDuKK0RKPgctsgrQQJSl6ZgzJI-p8rqbeshJZLDqSrdbbVcbrh7l9quheMhnh5LX7eikiAo1pIXYa8lxXPSuUHcILo0F0CEwqHw1FXPHE+4wKSwNNKYtlhZExtVQRVkQSFFDrnplSaXToH8Jrmy0suyAZr6cRCof3vAn1rnZpMJzbYzhyFr6KW5+hzQvfRo8EQRsOk7PE7nSVXu4te7rDvbHdLZ-gDG8-rL0SRB2AW0USM9vSmYBkAAQVEOwUyBLw9A0YQSmQABaZAYAQFAKjkTAAHI6jkJpJGgMZgGAlCkGXf1GgARguIkhjiatgIAFWgCdfRXeNkEo6iYlIWhtHQDo6LYlJKJDXUXFcNlUBOBV1DXaxLgdUx0AuTDdSYfwFEwnN6NA8DsgBVNoLKXxyBSDISCQki0MqEh6jwjZCOIuBSNY8iACYuOiHi+IEnSmJYmN2MaDzkCJbzLF8oSOLc0SnHEjwvh+BQ5JsB8lJUtSNK094wCAA

πŸ’» 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

@Andarist
Copy link
Contributor

When you report an issue like this it's always good to provide the equivalent TS code so the maintainers can quickly verify the report. I've already done that for this short example, so here it is: TS playground

@simonyarde
Copy link
Author

Thanks @Andarist

@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Jan 31, 2025
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Jan 31, 2025
@RyanCavanaugh RyanCavanaugh added the Help Wanted You can do this label Jan 31, 2025
@Andarist Andarist linked a pull request Jan 31, 2025 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Help Wanted You can do this
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants