• TypeScript JSDoc generic member function

    From Julio Di Egidio@21:1/5 to All on Wed Oct 25 20:03:30 2023
    I cannot seem to find a way to tell TypeScript with JSDoc that a
    member function of some object/interface takes a type parameter.

    Here is a minimal example illustrating the problem:

    ```js
    /**
    * @template {unknown} T
    * @callback IGet
    * @param {string} key
    * @returns {T | undefined}
    */

    /**
    * @typedef IGetter
    * @type {object}
    * _@template {unknown} T // how to write this?
    * @property {IGet<T>} get
    */
    ```

    Error: Cannot find name 'T'.

    That of course can be done in TypeScript, but how to do it with
    JSDoc? Is it at all possible?

    Link to TypeScript playground: <https://www.typescriptlang.org/play?strictNullChecks=true&noUncheckedIndexedAccess=true&allowUnusedLabels=true&noUnusedLocals=true&noUnusedParameters=true&removeComments=true&target=1&module=1&pretty=true&isolatedModules=true&useUnknownInCatchVariables=
    true&exactOptionalPropertyTypes=true&noFallthroughCasesInSwitch=true&noImplicitOverride=true&noPropertyAccessFromIndexSignature=true&keyofStringsOnly=true&filetype=js#code/
    PQKhCgAIUgBAXApgWwA4BsCGTIG8CuAdgNaED2A7oQL6QAqUMsAxpuugEabPGQCSAcUTxGcVJgBOmZHgDO8CQEtCAc1rFEAT1GwJw-BMKy8dSAB9IRACaIAZssRXqjYOHCgI0OPE2pHd-iF4JAkdHz88Mg4AK0RmeGdIJKSYAH0EFAxsRDwiUkoaeiTgYEgAC0pIeDJICiUceDLFWQB+HVQJMj8JHzxBYQAeOgA+WhVhF3AgA>

    Julio

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From John Harris@21:1/5 to Julio Di Egidio on Thu Oct 26 11:28:00 2023
    On 25/10/2023 19:03, Julio Di Egidio wrote:
    I cannot seem to find a way to tell TypeScript with JSDoc that a
    member function of some object/interface takes a type parameter.

    Here is a minimal example illustrating the problem:

    ```js
    /**
     * @template {unknown} T
     * @callback IGet
     * @param {string} key
     * @returns {T | undefined}
     */

    /**
     * @typedef IGetter
     * @type {object}
         * _@template {unknown} T  // how to write this?
     * @property {IGet<T>} get
     */
    ```

    Error: Cannot find name 'T'.

    That of course can be done in TypeScript, but how to do it with
    JSDoc?  Is it at all possible?

    Link to TypeScript playground: <https://www.typescriptlang.org/play?strictNullChecks=true&noUncheckedIndexedAccess=true&allowUnusedLabels=true&noUnusedLocals=true&noUnusedParameters=true&removeComments=true&target=1&module=1&pretty=true&isolatedModules=true&
    useUnknownInCatchVariables=true&exactOptionalPropertyTypes=true&noFallthroughCasesInSwitch=true&noImplicitOverride=true&noPropertyAccessFromIndexSignature=true&keyofStringsOnly=true&filetype=js#code/
    PQKhCgAIUgBAXApgWwA4BsCGTIG8CuAdgNaED2A7oQL6QAqUMsAxpuugEabPGQCSAcUTxGcVJgBOmZHgDO8CQEtCAc1rFEAT1GwJw-BMKy8dSAB9IRACaIAZssRXqjYOHCgI0OPE2pHd-iF4JAkdHz88Mg4AK0RmeGdIJKSYAH0EFAxsRDwiUkoaeiTgYEgAC0pIeDJICiUceDLFWQB+HVQJMj8JHzxBYQAeOgA+WhVhF3AgA>

    Julio

    That's the trouble with documentation programs. Users have no control
    over the program's prejudices.

    John

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Julio Di Egidio@21:1/5 to Julio Di Egidio on Thu Oct 26 16:25:41 2023
    On 25/10/2023 20:03, Julio Di Egidio wrote:
    I cannot seem to find a way to tell TypeScript with JSDoc that a
    member function of some object/interface takes a type parameter.

    Here is a minimal example illustrating the problem:

    ```js
    /**
     * @template {unknown} T
     * @callback IGet
     * @param {string} key
     * @returns {T | undefined}
     */

    /**
     * @typedef IGetter
     * @type {object}
         * _@template {unknown} T  // how to write this?
     * @property {IGet<T>} get
     */
    ```

    Error: Cannot find name 'T'.

    That of course can be done in TypeScript, but how to do it with [TypeScript's] JSDoc?  Is it at all possible?

    I was wrong, that cannot be done in TypeScript either...

    Eventually, the only way I have found is to repeat the
    declaration in-line, as in:

    ```js
    /**
    * @typedef IGetter
    * @type {object}
    * @property {<T extends unknown>(key: string) => (T | undefined)} get
    */
    ```

    but I lose any chance to properly document that function, which
    is simply quite a disaster: and not the only one, also the return
    type gets messed up (becomes just `T`), to the point that I am
    compelled to do something altogether different, i.e. have a
    concrete object constructor.

    File under the misery of an entire industry.

    Julio

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)