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

Support TypeScript import type #5

Closed
devidw opened this issue May 14, 2023 · 7 comments
Closed

Support TypeScript import type #5

devidw opened this issue May 14, 2023 · 7 comments

Comments

@devidw
Copy link

devidw commented May 14, 2023

Hey @beenotung

Thanks a lot, creating this tool

When using NodeNext import type imports also need to resolve in .js extensions, otherwise ts compiler will throw:

gen/api.ts:16:36 - error TS2835: Relative import paths need explicit file extensions in EcmaScript imports when '--moduleResolution' is 'node16' or 'nodenext'. Did you mean './configuration.js'?

16 import type { Configuration } from './configuration';

I see that these are currently ignored in the script via:

if (importCode.includes('import type')) continue

When I am removing this line, they are fixed as well.

Am I missing something, why we don't want to replace those as well by default?

In this case, we could maybe expose a CLI option to include/exclude those imports, if this makes sense to you?

🙌

@beenotung
Copy link
Owner

beenotung commented May 14, 2023

The program is not modifying import type because those import are supposed to be removed when transpiled into js file.

Is it the case that, your transpiler / bundler is preserving the import type statements in the output?

@devidw
Copy link
Author

devidw commented May 14, 2023

Running with bare bones, vanilla typescript compiler:

"typescript": "^5.0.0"

tsconfig.json

{
	"compilerOptions": {
		"strict": true,
		"target": "ESNext",
		"module": "NodeNext",
		"moduleResolution": "nodenext"
	}
}

build command

tsc

Which will throw for all non-types imports (fixed by this package), but will still throw for type-imports:

gen/common.ts:17:34 - error TS2835: Relative import paths need explicit file extensions in EcmaScript imports when '--moduleResolution' is 'node16' or 'nodenext'. Did you mean './base.js'?

17 import type { RequestArgs } from "./base";

@beenotung
Copy link
Owner

I'm working on --process-import-type and --preserve-import-type flag to address this issue.

I'm thinking what should the default behavior be. It seems easier to process them by default, and only preserve them as is if the project is not affected by this issue and want to minimize "git changes"

@devidw
Copy link
Author

devidw commented May 14, 2023

Wonderful, thanks a lot ✨
Yeah, that sounds good

@beenotung
Copy link
Owner

beenotung commented May 14, 2023

The patch is released in v1.4.0 🎉
Thanks for your suggestion.
Please feel free to reopen this issue if it is not fixed in your case

@devidw
Copy link
Author

devidw commented May 14, 2023

Wow, that was fast, thanks a bunch - works great! 🙏

@alonesuperman
Copy link

tsconfig.json

{
	"compilerOptions": {
		"strict": true,
		"target": "ESNext",
		"module": "NodeNext",
		"moduleResolution": "nodenext"
	}
}

maybe u can change to "moduleResolution":"Bundler"

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

No branches or pull requests

3 participants