-
Notifications
You must be signed in to change notification settings - Fork 349
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
Add .js suffix to imports #601
Comments
When importing a cross-referenced file, a line like this is generated: import { MyMessage } from '../myother/myother' .. assuming there's a proto at ../myother/myother.proto But if we add the suffix '.pb.ts' to the generated files: import { MyMessage } from '../myother/myother.pb' Is not recognized as a TypeScript import by tsc because of the .pb suffix. To fix this, we can just add .js, and the TypeScript compiler recognizes that we actually mean the .ts file: import { MyMessage } from '../myother/myother.pb.js' Fixes stephenh#601 Signed-off-by: Christian Stewart <christian@paral.in>
When importing a cross-referenced file, a line like this is generated: import { MyMessage } from '../myother/myother' .. assuming there's a proto at ../myother/myother.proto But if we add the suffix '.pb.ts' to the generated files: import { MyMessage } from '../myother/myother.pb' Is not recognized as a TypeScript import by tsc because of the .pb suffix. To fix this, we can just add .js, and the TypeScript compiler recognizes that we actually mean the .ts file: import { MyMessage } from '../myother/myother.pb.js' Fixes stephenh#601 Signed-off-by: Christian Stewart <christian@paral.in>
When importing a cross-referenced file, a line like this is generated: import { MyMessage } from '../myother/myother' .. assuming there's a proto at ../myother/myother.proto But if we add the suffix '.pb.ts' to the generated files: import { MyMessage } from '../myother/myother.pb' Is not recognized as a TypeScript import by tsc because of the .pb suffix. To fix this, we can just add .js, and the TypeScript compiler recognizes that we actually mean the .ts file: import { MyMessage } from '../myother/myother.pb.js' Fixes stephenh#601 Signed-off-by: Christian Stewart <christian@paral.in>
When importing a cross-referenced file, a line like this is generated: import { MyMessage } from '../myother/myother' .. assuming there's a proto at ../myother/myother.proto But if we add the suffix '.pb.ts' to the generated files: import { MyMessage } from '../myother/myother.pb' Is not recognized as a TypeScript import by tsc because of the .pb suffix. To fix this, we can just add .js, and the TypeScript compiler recognizes that we actually mean the .ts file: import { MyMessage } from '../myother/myother.pb.js' Fixes stephenh#601 Signed-off-by: Christian Stewart <christian@paral.in>
When importing a cross-referenced file, a line like this is generated: import { MyMessage } from '../myother/myother' .. assuming there's a proto at ../myother/myother.proto But if we add the suffix '.pb.ts' to the generated files: import { MyMessage } from '../myother/myother.pb' Is not recognized as a TypeScript import by tsc because of the .pb suffix. To fix this, we can just add .js, and the TypeScript compiler recognizes that we actually mean the .ts file: import { MyMessage } from '../myother/myother.pb.js' Fixes stephenh#601 Signed-off-by: Christian Stewart <christian@paral.in>
When importing a cross-referenced file, a line like this is generated: import { MyMessage } from '../myother/myother' .. assuming there's a proto at ../myother/myother.proto But if we add the suffix '.pb.ts' to the generated files: import { MyMessage } from '../myother/myother.pb' Is not recognized as a TypeScript import by tsc because of the .pb suffix. To fix this, we can just add .js, and the TypeScript compiler recognizes that we actually mean the .ts file: import { MyMessage } from '../myother/myother.pb.js' Fixes stephenh#601 Signed-off-by: Christian Stewart <christian@paral.in>
When importing a cross-referenced file, a line like this is generated: import { MyMessage } from '../myother/myother' .. assuming there's a proto at ../myother/myother.proto But if we add the suffix '.pb.ts' to the generated files: import { MyMessage } from '../myother/myother.pb' Is not recognized as a TypeScript import by tsc because of the .pb suffix. To fix this, we can just add .js, and the TypeScript compiler recognizes that we actually mean the .ts file: import { MyMessage } from '../myother/myother.pb.js' Fixes stephenh#601 Signed-off-by: Christian Stewart <christian@paral.in>
When importing a cross-referenced file, a line like this is generated: import { MyMessage } from '../myother/myother' .. assuming there's a proto at ../myother/myother.proto But if we add the suffix '.pb.ts' to the generated files: import { MyMessage } from '../myother/myother.pb' Is not recognized as a TypeScript import by tsc because of the .pb suffix. To fix this, we can just add .js, and the TypeScript compiler recognizes that we actually mean the .ts file: import { MyMessage } from '../myother/myother.pb.js' Fixes stephenh#601 Fix the resolution in ts-jest with the jest-ts-webcompat-resolver: See: kulshekhar/ts-jest#1057 (comment) Signed-off-by: Christian Stewart <christian@paral.in>
When importing a cross-referenced file, a line like this is generated: import { MyMessage } from '../myother/myother' .. assuming there's a proto at ../myother/myother.proto But if we add the suffix '.pb.ts' to the generated files: import { MyMessage } from '../myother/myother.pb' Is not recognized as a TypeScript import by tsc because of the .pb suffix. To fix this, we can just add .js, and the TypeScript compiler recognizes that we actually mean the .ts file: import { MyMessage } from '../myother/myother.pb.js' Fixes stephenh#601 Fix the resolution in ts-jest with the jest-ts-webcompat-resolver: See: kulshekhar/ts-jest#1057 (comment) Signed-off-by: Christian Stewart <christian@paral.in>
When importing a cross-referenced file, a line like this is generated: import { MyMessage } from '../myother/myother' .. assuming there's a proto at ../myother/myother.proto But if we add the suffix '.pb.ts' to the generated files: import { MyMessage } from '../myother/myother.pb' Is not recognized as a TypeScript import by tsc because of the .pb suffix. To fix this, we can just add .js, and the TypeScript compiler recognizes that we actually mean the .ts file: import { MyMessage } from '../myother/myother.pb.js' Fixes stephenh#601 Fix the resolution in ts-jest with the jest-ts-webcompat-resolver: See: kulshekhar/ts-jest#1057 (comment) Signed-off-by: Christian Stewart <christian@paral.in>
When importing a cross-referenced file, a line like this is generated: import { MyMessage } from '../myother/myother' .. assuming there's a proto at ../myother/myother.proto But if we add the suffix '.pb.ts' to the generated files: import { MyMessage } from '../myother/myother.pb' Is not recognized as a TypeScript import by tsc because of the .pb suffix. To fix this, we can just add .js, and the TypeScript compiler recognizes that we actually mean the .ts file: import { MyMessage } from '../myother/myother.pb.js' Fixes stephenh#601 Fix the resolution in ts-jest with the jest-ts-webcompat-resolver: See: kulshekhar/ts-jest#1057 (comment) Signed-off-by: Christian Stewart <christian@paral.in>
When importing a cross-referenced file, a line like this is generated: import { MyMessage } from '../myother/myother' .. assuming there's a proto at ../myother/myother.proto But if we add the suffix '.pb.ts' to the generated files: import { MyMessage } from '../myother/myother.pb' Is not recognized as a TypeScript import by tsc because of the .pb suffix. To fix this, we can just add .js, and the TypeScript compiler recognizes that we actually mean the .ts file: import { MyMessage } from '../myother/myother.pb.js' Fixes #601 Fix the resolution in ts-jest with the jest-ts-webcompat-resolver: See: kulshekhar/ts-jest#1057 (comment) Signed-off-by: Christian Stewart <christian@paral.in>
## [1.116.1](v1.116.0...v1.116.1) (2022-07-02) ### Bug Fixes * add .js suffix to proto cross-reference imports ([#602](#602)) ([8dc38af](8dc38af)), closes [#601](#601) [/github.com/kulshekhar/ts-jest/issues/1057#issuecomment-481406624](https://github.com//github.com/kulshekhar/ts-jest/issues/1057/issues/issuecomment-481406624)
🎉 This issue has been resolved in version 1.116.1 🎉 The release is available on: Your semantic-release bot 📦🚀 |
This breaks code during compilation on typescript project ( projects create with CRA with eslint integrated ) import { SomeInterface } from "./types.js"; This path (including .js) is not resolved by compiler as there is no such file, it expects import { SomeInterface } from "./types";
OR
import { SomeInterface } from "./types.ts"; If i can override this with any config setting please let me know |
@xpt1x the typescript compiler expects the .js suffix and will resolve it to the .ts file automatically. Maybe you're using an old version of TypeScript? ...And as we mentioned above importing the .ts directly doesn't work in the majority of cases. |
I have the same issue. |
@yoshiko-pg I'd like to figure out why, because many projects are using the .js import, mine included, not in esmodule mode, and it works fine. I guess it's the resolver part, depending on which resolver you have enabled... I'll have a look at adding a flag to control it. |
I am currently on I believe by default TS compiler only allows .ts /.tsx / .d.ts files You must be having |
@xpt1x no, that's not set. TypeScript resolves the .js extension in the import to .ts automatically. |
@xpt1x please update to typescript 4.7 or later. |
@paralin from what @yoshiko-pg reports, it sounds like it's not just the latest TypeScript that users need; there are other import-aware tools (Next.js and Jest) that don't know the I'm pretty convinced we need to flag the new behavior, with one of the following options:
Not to nit-pick, but to support my "restore old behavior" push, I believe when you said:
AFAIU, that's not accurate insofar as IMO importing extension-less paths actually works in the (far?) majority of cases, at least in the ecosystem today, and it's only the (probably very small) minority of paths that use the Granted, you meant have strictly meant "imports with the I know your strongest opinion is likely to keep the js suffix, but if you have a 2nd opinion after that one :-) on your preferred a/b/c/d above, and wouldn't mind opening a PR for it, I'd really appreciate it. Personally I think I'd probably go with b), b/c it avoids "yet another flag" syndrome with ts-proto definitely suffers from, and I think still supports your use case, and kinda punts on "official ESM flag/support" until we're ready to do that. |
Ah shoot, @paralin I was catching up on comments and missed this one; that sounds great, thanks! |
@stephenh The main concern I have is related to esm, almost all projects are now using .js imports: https://github.com/libp2p/js-libp2p-mplex/blob/master/src/index.ts#L3 Importing with .ts extension is not allowed. This is the error that typescript throws: So the best option is to add a useJsExtension flag which adds .js to any directly-imported .ts file. In my opinion. |
@stephenh maybe a string option - |
The problem has been completely resolved in v1.117.0. Thanks for the quick turnaround! |
Regarding importing |
When importing a cross-referenced file, a line like this is generated:
.. assuming there's a proto at ../myother/myother.proto
But if we add the suffix '.pb.ts' to the generated files:
Is not recognized as a TypeScript import by tsc because of the .pb suffix.
To fix this, we can just add .js, and the TypeScript compiler recognizes that we actually mean the .ts file:
And this is also required if we are using esm mode.
The fix:
The text was updated successfully, but these errors were encountered: