microsoft / microsoft/TypeScript

Cannot import properties from module.exports as types in JSDoc

Open
#29,485 3 comments 13 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Domain: JavaScript
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the a.js and b.js example with TypeScript's JSDoc checking enabled. Read the handling of import('./a') types and module.exports, then verify that the A property is recognized as the constructor parameter type without changing the single-export behavior. Done means the reported error is gone for this reproduction.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, typescript
Domain
compilers
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.