Skip to content

How to import generic function type declaration from one javascript file to another? #29964

Open
@ArenSH

Description

@ArenSH

TypeScript Version: 3.3.3

Search Terms: import, typedef, callback jsCheck

I have an issue with importing generic function type declared with @callback in another file.

Here is example:

In file1.js I've defined generic function with template parameter

    // file1.js
    /**
     * @template T
     * @template {Error} E
     * @callback CallbackWithResult
     * @param {E|null} error
     * @param {T} [result]
     */

And in another file I've imported that declaration

    // file2.js

    /** @typedef {import('./file1').CallbackWithResult} CallbackWithResult */
    
    /**
     * @param {CallbackWithResult<number>} callback
     */
    function doSomething(callback) {
       callback(null, 42);
    }

Attempting to check file2.js gives following errors

    file2.js:4:15 - error TS2314: Generic type 'CallbackWithResult' requires 2 type argument(s).
    
    4 /** @typedef {import('./file1').CallbackWithResult} CallbackWithResult */
                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    file2.js:7:12 - error TS2315: Type 'CallbackWithResult' is not generic.
    
    7  * @param {CallbackWithResult<number>} callback
                 ~~~~~~~~~~~~~~~~~~~~~~~~~~

Question is: how to properly import generic declarations without defining another set of template variables?

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptHelp WantedYou can do this

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions