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 'import type' syntax wrongly transformed when generate declaration from js file #46575

Closed
xlfsummer opened this issue Oct 29, 2021 · 6 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@xlfsummer
Copy link

Bug Report

🔎 Search Terms

Import type syntax error after generate .d.ts from js
Import type error after generate .d.ts using JSdoc

🕗 Version & Regression Information

💻 Code

// index.js
/** @type {import('./type').Foo} */
export const foo;
// type.ts
export type Foo = string;

demo repo (using 'import-types' branch, not 'master')

🙁 Actual behavior

output index.d.ts

/** @type {import('./type').Foo} */
export const foo: import('./type').Foo;

import is a method for loading async resource in js/ts, not import a type. it should be transformed to import .. from .. .

🙂 Expected behavior

output index.d.ts

import {Foo} from './type';
/** @type {import('./type').Foo} */
export const foo: Foo;
@fatcerberus
Copy link

import is a method for loading async resource in js/ts, not import a type.

import("foo") doesn’t mean that when used in a type position—in that case it means the type of import * as … from "foo", as resolved by TypeScript.

@xlfsummer
Copy link
Author

import is a method for loading async resource in js/ts, not import a type.

import("foo") doesn’t mean that when used in a type position—in that case it means the type of import * as … from "foo", as resolved by TypeScript.

Yes. In a type position, we use import(...) to import all exports in another file. When we generate type declaration files, we should not directly copy that from source to dist. We should replace the import(...) by import ... from ... and place it to the top of the output file.

@orta orta added Breaking Change Would introduce errors in existing code By Design Deprecated - use "Working as Intended" or "Design Limitation" instead labels Oct 30, 2021
@orta
Copy link
Contributor

orta commented Oct 30, 2021

JSDoc is used to only give type-hints in TypeScript and JavaScript files, not to direct how the syntax should change. I'm afraid any changes here would be massively breaking to the ecosystem, and break the TypeScript design guides which were mentioned in the issue template.

@xlfsummer
Copy link
Author

But I think it's weird to have each single feature works fine but break when use them both. (because import() returns Promise value in ts)

I think maybe we could handle this by replace import() to any? (at least it won't break types)

@andrewbranch andrewbranch added Working as Intended The behavior described is the intended behavior; this is not a bug and removed By Design Deprecated - use "Working as Intended" or "Design Limitation" instead Breaking Change Would introduce errors in existing code labels Nov 1, 2021
@andrewbranch
Copy link
Member

Working as intended for now, but there’s an issue tracking an investigation into whether we can do this: #44044

(As an aside, it feels slightly weird to me that the JSDoc type tag gets preserved in declaration emit.)

@typescript-bot
Copy link
Collaborator

This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

5 participants