Skip to content

Cannot import properties from module.exports as types in JSDoc #29485

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

Open
jordanbtucker opened this issue Jan 18, 2019 · 3 comments
Open

Cannot import properties from module.exports as types in JSDoc #29485

jordanbtucker opened this issue Jan 18, 2019 · 3 comments
Labels
Bug A bug in TypeScript Domain: JavaScript The issue relates to JavaScript specifically
Milestone

Comments

@jordanbtucker
Copy link

TypeScript Version: 3.3.0-dev.20190118

Search Terms: jsdoc import module.exports has no exported member

Code

a.js

class A {}

module.exports = {A}

b.js

// @ts-check
class B {
	// In the JSDoc below, VSCode intellisense does not recognize parameter a as type A.
    // Instead it shows the error:
    //   Namespace '".../a".export=' has no exported member 'A'

	/**
	 * @param {import('./a').A} a
	 */
	constructor (a) {
		this.a = a
	}
}

Expected behavior
Parameter a of the constructor should be recognized as type A, which is accessible via the A property of module.exports from a.js.

Actual behavior
Intellisense reports the error: Namespace '".../a".export=' has no exported member 'A'.

Notes
@param {import('./a')} a does not give an error, but it resolves to type {A: typeof A} instead of A.

If a.js is modified with module.exports = A, then import('./a') resolves to A, however this restricts a.js to only exporting one type.

@weswigham weswigham added Bug A bug in TypeScript Domain: JavaScript The issue relates to JavaScript specifically labels Jan 23, 2019
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Mar 14, 2019
@ghost
Copy link

ghost commented Apr 29, 2019

This bug is currently blocking our team from adopting ts-check into our Javascript codebase, as we have .js files that export classes, and those classes can't be used in our ts-checked files until this bug is resolved.

As a workaround, we could define our own .d.ts files for the modules in question, but this is a lot of what we feel is duplicate work - the types are already defined in jsdoc format inline in the files.

@lukehesluke
Copy link

lukehesluke commented Aug 5, 2019

I also had problems with this. A workaround which I have (unfortunately) used is:

InstanceType<import('./a')['A']>

import('./a')['A'] has type typeof A (i.e. the type of the class), so InstanceType<> needs to be used to get the instance

However I would much prefer it to work as import('./a').A

@iczero
Copy link

iczero commented Oct 26, 2019

I have found that using exports.Thing = Thing is recognized properly, while using

module.exports = { Thing };

is not.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Domain: JavaScript The issue relates to JavaScript specifically
Projects
None yet
Development

No branches or pull requests

5 participants