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
proto-loader-gen-types
: Import internal files with extension
#2693
Conversation
I don't really understand this. TypeScript is a compiled language. The runtime shouldn't care about the raw TypeScript content, and if it doesn't like the compiler output, that should be fixable by changing the compiler options. Edit: I see that Deno supports TypeScript natively. So my objection is more that this code is valid TypeScript according to the TypeScript compiler, so it should be valid TypeScript everywhere. I dislike the idea of catering to variants like this because that is potentially an endless rabbit hole of compatibility code. |
It is only valid typescript if you use legacy settings. Modern node resolution settings require that you use From the TS docs: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-7.html#type-in-packagejson-and-new-extensions
From the Deno docs: https://docs.deno.com/runtime/manual/advanced/typescript/overview#determining-the-type-of-file
That quote means if you try to import a typescript file with no extension, it will be read by a javascript runtime which will fail because it cannot read typescript. There is no option to change this configuration, you must use ..so TL;DR if you don't support this feature, your code cannot run in modern Node projects and can not run in Deno at all. I suspect other runtimes will be unsupported as well. |
If I understand that TypeScript update page correctly, the extension should always be |
I am not sure how you could come to that conclusion. If you use the I proposed this change because it would be a simple change with no breakage or refactoring. However if you are looking for the most correct solution to this, I think it is to use |
Node should have no involvement here. These are type-only imports of type-only files. They should be completely omitted in any compiled code that Node would see. |
I think I understand now. It seems you are correct, if you use the So what you are saying is that you prefer to always use the |
I would say that it's not so much that this is supported on Node, as that it is never executed on Node. It looks like it is possible for this code to use a single extension that works everywhere, and if so, we should just do that. Then providing an option to use a different extension is superfluous. |
proto-loader-gen-types
: Add option to choose import extensionproto-loader-gen-types
: Import internal files with extension
Should be ready for merge now :) |
Please run the |
I also updated the example in the docs to use the same import syntax :) |
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.
Thank you for your contribution.
Thanks for helping me get this merged! |
This is now out in version 0.7.11 |
Yesaterday we suddenly observed new eslint errors in our (unchanged, only rebuilt) code. It took us some time to find out that
This is how we are calling proto-loader-gen-types: These are the most relevant parts for importing the grpc stuff: import * as grpc from '@grpc/grpc-js';
import * as protoLoader from '@grpc/proto-loader';
import { ProtoGrpcType as DMErrorCode } from './api/dm_errorcode';
import { ProtoGrpcType as UEDM } from './api/UEDM';
import { UEDMClient } from './api/UE/DM/UEDM'; I guess we have to change something so the types are known again? But I am not sure what exactly. Do you have any idea? |
@versat have you tried importing the types as types? -import { ProtoGrpcType as DMErrorCode } from './api/dm_errorcode';
+import type { ProtoGrpcType as DMErrorCode } from './api/dm_errorcode.ts';
-import { ProtoGrpcType as UEDM } from './api/UEDM';
+import type { ProtoGrpcType as UEDM } from './api/UEDM.ts';
-import { UEDMClient } from './api/UE/DM/UEDM';
+import type { UEDMClient } from './api/UE/DM/UEDM.ts'; |
I have adapted the imports, but now I am getting even more of these errors. |
I'm reverting this. It shouldn't be a minor change if it breaks compatibility with TypeScript 4. We can re-evaluate what the best approach is after that. |
@murgatroid99 I can make a boolean that enables the |
I think it could also be that a |
Different runtimes have different requirements for which extension you need to put on relative file imports. It is problematic that the type generator only supports bare imports. This is for example not supported in Deno.
This PR adds an option to specify which file extension should be used.
I have tested the changes in my own repo, and everything works for me.
Closes #2401