-
Notifications
You must be signed in to change notification settings - Fork 3.3k
[TS] Generated Typescript code has .js
extension
#7661
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
Comments
I'm afraid this is not so simple. Imports with .js extensions in ts code are not "wrong" as far I know and some environments require it. However, ts files with js extension are wrong. Will need to investigate but there is a larger rework of TS/JS generation work in progress already in #7510 which might affect this. |
Sorry I was not very clear, the key problem is that |
@stephanemagnenat but that is common practice and required by many module loaders. I agree it's not an optimal situation but have a look at microsoft/TypeScript#40878 for some history. If you use modules in a browser directly for example it will require the full path of the module, see https://stackoverflow.com/questions/55251956/how-does-javascript-import-find-the-module-without-an-extension. |
I see, thank you for the explanation! |
@bjornharrtell: Can this be made at least configurable? There are plenty of projects where this will start throwing errors because of the added extension. |
@Leandros mm, I'm open to add it as an option if really desired but I'm also curious of why/when it is cause of problem because I don't quite understand it. It's usually the other way round, no extension work "sometimes". |
I don't have a great example why, but it has something to do with the module resolution it could be that for example your local development is using TypeScript to discover it's files which will use a different methode of finding the right extension and a plugin like Eslint or Jest (running on Node.js) might use Node's module resolution (this I have not checked) and would fail to get the But I agree having an option to force an extension (or no extension) would be welcome to resolve some possible errors while working on a project. |
flatbuffers imports files with just extension which makes Jest fail. google/flatbuffers#7661
I support this. |
@bjornharrtell I'm importing the generated TypeScript files inside a TypeScript backend written in NestJS. In my case, Generally, in the JavaScript/TypeScript world, it's common to avoid specifying any extensions for importing. More info here: https://github.com/import-js/eslint-plugin-import/blob/main/docs/rules/extensions.md |
I've been working with JavaScript and TypeScript in lots of different projects and contexts and haven't encountered any issue with specifying the full module path with extension (after transpilation), only the other way around. So my own interest in this is low. But I will not oppose a PR that adds an option to generate without extension, so feel free to contribute. |
Since upgrading to flatc-22.9.29 our TS builds have been breaking because of this change. Previous versions didn't specify an extension and worked fine. |
This issue is stale because it has been open 6 months with no activity. Please comment or label |
not stale |
In some development environment, there are not TS code file emitted. For example, ts-node has it disabled by default. The same default is used in React projects, at least if you use it with tools like react-scripts. So yes, having the .js extension in the imports, breaks the code in environments like this. PS: I would be curios in which situation, you have a TS module |
Since #7748 was implemented you can opt out of generating with js extension. Closing. |
Nice, but there is a bug in the implementation. When you have an import in one of your schema, the ts file will still contain an export with a .js extension: If I have the following schemas:
After running // automatically generated by the FlatBuffers compiler, do not modify
export { Color } from './color';
export * as Sample from './sample.js'; |
@surdu can you please report that as a separate issue? |
Sure thing. Just created #8105 |
Uh oh!
There was an error while loading. Please reload this page.
Using Flatbuffers 22.11.22, and doing:
The generated
.ts
files have a.js
extension, leading to imports like this:while previously (tested with version 2.0.6) they properly correctly had no extension:
The generated files themselves properly end with
.ts
.You'll find a minimal test case here:
https://github.com/stephanemagnenat/flatbuffers-6739
Just type
make
and look insrc/all_generated.ts
.The text was updated successfully, but these errors were encountered: