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 Does not Pick up Type imported using Require #14056

Closed
mjbvz opened this issue Feb 14, 2017 · 5 comments · Fixed by #16316
Closed

JSDoc Does not Pick up Type imported using Require #14056

mjbvz opened this issue Feb 14, 2017 · 5 comments · Fixed by #16316
Assignees
Labels
Domain: JSDoc Relates to JSDoc parsing and type generation Fixed A PR has been merged for this issue Suggestion An idea for TypeScript VS Code Tracked There is a VS Code equivalent to this issue

Comments

@mjbvz
Copy link
Contributor

mjbvz commented Feb 14, 2017

From microsoft/vscode#20238

TypeScript Version: 2.1.6

Code

foo.js:

export class Foo {
    /**
     * @param {string} name
     * @param {number} id
     */
    constructor(name, id) {
        this.name = name;
        this.id = id
    }
}

index.js:

const {Foo} = require('./foo');

/**
 * @param {Foo} param
 */
function func(param) {

}

Expected behavior:
The type of param is Foo

Actual behavior:
The type of param is any.

If I change the import to import {Foo} from './foo', the type of param is correct

@mhegazy
Copy link
Contributor

mhegazy commented Feb 14, 2017

Foo is not a type. it is a constructor function. I suppose we can check if we could not look up a type, but the value points to a constructor function, use that instead.

@mhegazy mhegazy added Suggestion An idea for TypeScript Domain: JSDoc Relates to JSDoc parsing and type generation labels Feb 14, 2017
@MicahZoltu
Copy link
Contributor

@mhegazy see the repro case over in microsoft/vscode#21933 in particular, notice how to behavior is different for var foo = new Foo(). This suggests to me that the engine understands that Foo is a type (with a constructor), not just a function, but it doesn't seem to follow the same rules when told via the annotation. Perhaps I'm misunderstanding to difference between a type and a constructor function?

@MicahZoltu
Copy link
Contributor

MicahZoltu commented Apr 10, 2017

For record keeping and improving searchability of this issue, the same problem occurs with local variables, it is not limited to function parameters:

var Foo = require("./foo");

/** @type {Foo} */
var bar; // VSCode (via TypeScript engine) considers `bar` to be of type `any`

Also note that the recommended workaround of using the import syntax only applies if you are writing ES2015+, in which case you really should be using import syntax anyway. For code written in ES5 I don't believe there is any workaround to this issue.

@Zarel
Copy link

Zarel commented May 10, 2017

@mhegazy

Foo is not a type. it is a constructor function.

If I define Foo as a class within the file, it's a type. Why is it not a type if I import it from a different file? How do I make it a type?

I've googled this a lot and I can't find any documentation on how to make it a type. Now that TypeScript emits errors when I try to do this, I think that error is a good place to provide guidance on how to make it a type.

@mhegazy
Copy link
Contributor

mhegazy commented May 10, 2017

Based on the discussion in #15747, we want to look up the reference as a value of the look up as a type failed, and add special handling for classes.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Domain: JSDoc Relates to JSDoc parsing and type generation Fixed A PR has been merged for this issue Suggestion An idea for TypeScript VS Code Tracked There is a VS Code equivalent to this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants