microsoft / microsoft/TypeScript

Type a standard import with jsdoc only

Open
#58,675 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Domain: JavaScript Domain: JSDoc Suggestion
Dominant language
Go
Stars
111k
Forks
14.4k
Avg merge
1d 19h
Merged PRs (30d)
117

Description

🔍 Search Terms

ecmascript module import jsdoc

✅ Viability Checklist
⭐ Suggestion

Right now I can type an require call with

/**
 * @type {typeof import("./accounts").userAccount}
 */
var x = require("./accounts").userAccount;

from https://www.typescriptlang.org/docs/handbook/jsdoc-supported-types.html#import-types

But when I my code uses an import statement
import { shortNameFromImport } from "textmodule";
I did not found a way to type that when the module resolution does found a hit.

@type in the line before can not be used because there is no let or const in this line.

/** @type {import('textmodule')} */
import { shortNameFromImport } from "textmodule";

And the normal way to type a variable with parenthesis can not be used because it is not valid JS code:

import { /** @type {import('textmodule')} */ (shortNameFromImport ) } from "textmodule";

Both variants without parenthesis do not work...

import { /** @type {import('textmodule')} */ shortNameFromImport } from "textmodule";
import { shortNameFromImport /** @type {import('textmodule')} */ } from "textmodule";
📃 Motivating Example

With typescript code I can do some magic with path in the tsconfig.json

"paths": {
  "textmodule": [ "./textmodule.ts" ]
},

But I wanted to have a JSDoc only solution.

💻 Use Cases

See above

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 with the JSDoc supported types documentation, especially the import-types section, and compare its require example with the named import examples in this issue. Trace how the TypeScript checker handles JSDoc on JavaScript import declarations, then define tests for the supported annotation and confirm that JavaScript output remains unchanged.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, typescript
Domain
compilers
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.