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

fix: dont strip type keyword from types imported from node modules #321

Closed
wants to merge 2 commits into from

Conversation

egilsster
Copy link

@egilsster egilsster commented May 5, 2024

Fixes #320

@egilsster egilsster marked this pull request as ready for review May 5, 2024 10:09
@egilsster egilsster changed the title fix: support type only imports fix: dont strip type keyword from types imported from node modules May 5, 2024
Copy link
Owner

@timocov timocov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @egilsster, thanks for your contribution, but it seems it might not solve the problem completely and might cause other issues instead (please see my review comment, lets discuss it there)

Comment on lines +339 to +343
result.push(`import { type ${
Array.from(imports.typeImports.entries())
.map(([localName, importedName]: [string, string]) => renamedImportValue(importedName, localName))
.sort()
.join(', type ')
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it looks like it can be replaced with just import type {} syntax instead?

Comment on lines +332 to +336
// For each type-only import, check if it exists in the `namedImports` map and remove it
// otherwise we might end up with a type import and a regular import in the bundle.
for (const key of imports.typeImports.keys()) {
imports.namedImports.delete(key);
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is what I was afraid of. this will produce wrong output in the following case (or similar, I didn't check exactly this code but it should give you the idea):

// index.ts
import { MyClass } from 'some-lib';
import { ParentType } from './other';

export class Foo extends MyClass {
    field: ParentType;
}
// other.ts
import { type MyClass } from 'some-lib';

export type ParentType = MyClass;

Technically if a type was used as a "value", then it should be imported as a "value" even if there are imports of as a "type". But on the other side, there are cases where you might have mixed imports and mixed usage, and then re-export of "type" import but if we import it as "value" then it might be re-exported with a wrong meaning:

// index.ts
import type { MyClass } from 'some-lib';
import { FooBar } from './other';

export type MyClassType = MyClass;
// other.ts
import { MyClass } from 'some-lib';

export class FooBar extends MyClass {}

In this case the output might be the following:

import { MyClass } from 'some-lib';

export type MyClassType = MyClass;
export class FooBar extends MyClass {}

which looks correct, but it might have implications in other places (I need to play with this more to give you an example if there is one). Or it could be

import type { MyClass } from 'some-lib';

export type MyClassType = MyClass;
export class FooBar extends MyClass {}

Which is broken because 'FooBar' cannot be used as a value because it was imported using 'import type'.

That's why #290 exists and why initially I thought that #320 is a duplicate and then re-opened, but it seems I'm leaning to think about it as about a duplicate aging based on said above...

The issue is that compiler doesn't tell you whether a symbol/node was originally value but was used a type or anything like that (see microsoft/TypeScript#57032 for more context).

Copy link
Author

@egilsster egilsster May 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great reply! Haven't had time to digest but can hopefully sit down and think about this and push something. Appreciated!

It does look like we kinda need what's noted in the TypeScript issue. Will dig around and see what we can use otherwise.

@@ -0,0 +1,6 @@
import type { Diagnostic, AffectedFileResult as RenamedImport } from "typescript";
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh please try to avoid using types from any of dependencies - they tend to change between releases and it will cause tests to fail without changing (almost) anything. If you need some specific pattern of a package, you can add it here https://github.com/timocov/dts-bundle-generator/tree/master/tests/e2e/test-cases/node_modules or use any existing one there

@egilsster
Copy link
Author

Don't have any bandwidth nor motivation to tackled this as of now so closing it to remove noise. It'll still get referenced in the issue so the comments are still beneficial.

@egilsster egilsster closed this Oct 7, 2024
@egilsster egilsster deleted the type-imports branch October 7, 2024 09:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Output strips type keyword from type import
2 participants