-
-
Notifications
You must be signed in to change notification settings - Fork 42
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ export interface ModuleImportsSet { | |
defaultImports: Set<string>; | ||
nsImport: string | null; | ||
namedImports: Map<string, string>; | ||
typeImports: Map<string, string>; | ||
requireImports: Set<string>; | ||
reExports: Map<string, string>; | ||
} | ||
|
@@ -328,6 +329,21 @@ function generateImports(libraryName: string, imports: ModuleImportsSet): string | |
Array.from(imports.requireImports).sort().forEach((importName: string) => result.push(`import ${importName} = require('${libraryName}');`)); | ||
Array.from(imports.defaultImports).sort().forEach((importName: string) => result.push(`import ${importName} ${fromEnding}`)); | ||
|
||
// 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); | ||
} | ||
|
||
if (imports.typeImports.size !== 0) { | ||
result.push(`import { type ${ | ||
Array.from(imports.typeImports.entries()) | ||
.map(([localName, importedName]: [string, string]) => renamedImportValue(importedName, localName)) | ||
.sort() | ||
.join(', type ') | ||
Comment on lines
+339
to
+343
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it looks like it can be replaced with just |
||
} } ${fromEnding}`); | ||
} | ||
|
||
if (imports.namedImports.size !== 0) { | ||
result.push(`import { ${ | ||
Array.from(imports.namedImports.entries()) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import type { TestCaseConfig } from '../test-case-config'; | ||
|
||
const config: TestCaseConfig = {}; | ||
|
||
export = config; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
require('../run-test-case').runTestCase(__dirname); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import type { Diagnostic, AffectedFileResult as RenamedImport } from "typescript"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
|
||
export type MyType = { | ||
value: Diagnostic; | ||
alias: RenamedImport<number> | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { type AffectedFileResult as RenamedImport, type Diagnostic } from 'typescript'; | ||
|
||
export type MyType = { | ||
value: Diagnostic; | ||
alias: RenamedImport<number>; | ||
}; | ||
|
||
export {}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"extends": "../tsconfig.json", | ||
"compilerOptions": { | ||
"verbatimModuleSyntax": true | ||
} | ||
} |
There was a problem hiding this comment.
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):
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:
In this case the output might be the following:
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
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).
There was a problem hiding this comment.
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.