-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Suggestion: Remove imports of type information in .ts / .tsx file from generated .js output #24279
Comments
Imports only used in type position are elided. only imports that are used as values (i.e. execution depends on them) are kept in the generated output. In the case of a .tsx file, the factory function for a JSX element uses |
I suspect what's going on here is that If so, then the solution would be to declare the types for your use case (possibly using the DefinitivelyTyped types as a base):
|
@nattthebear is right, I'm loading React and ReactDOM separately into the page from a CDN:
I would be using the two imports that I mentioned simply to load the React related types, not to load the React code itself. Looking at the solution put forward by @nattthebear, using "declare var React", etc.:
But that doesn't give me any React types. I could define the React related types myself based on DefinitivelyTyped types, but then I'd have to maintain all these types myself for the duration of the code base. Another avenue would be to load the React code from modules instead of with separate script tags. However, if you try
then the browser simply can't find the modules, because this syntax assumes there is a node_modules directory on the client. This doesn't work either:
The compiler now complains that I should leave off the .d.ts extensions. But if I do that: With high hopes I also tried
This would have been perfect, because due to the comment // the browser would have ignored this. But the compiler told me:
To be honest, this doesn't really make sense to me. Essentially I'm looking at client side oriented modules (React and ReactDOM) but the way the types are delivered and compiled assume those modules are uses with Node, a server side technology. I appreciate my original suggestion may not have hit the mark. But the problem remains - how to use DefinitivelyTyped types and the TypeScript compiler to write strongly typed code that can be loaded as modules into the browser. |
In 2.9, you'll have a solution for this without changing the DefinitivelyTyped types to support your use case.
See #14844 |
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed. |
Search Terms
typescript exclude modules from compiled code
typescript exclude type modules
Suggestion
In my .tsx files, in order to import the React types I have to include the lines:
import * as React from "react";
import * as ReactDOM from "react-dom";
Problem is that these lines get copied to the transpiled .js files (along with the other import statements). As a result, when I load the .js files in Chrome as part of my pages, Chrome throws an exception, saying it can't resolve the modules. For more details, see:
https://stackoverflow.com/questions/50304192/how-to-import-type-information-in-module-meant-to-be-loaded-in-browsers
My suggestion is to either:
I appreciate I could use webpack to process and combine the generated .js files. I'm indeed doing this for production builds. However, for debug builds, I strongly prefer loading the generated modules individually to make it easier to debug the code. Not using webpack during development also saves time and complexity.
Use Cases
Debug builds, where you want
Alternative approaches:
Examples
Developer would write type imports as per normal, and the TypeScript compiler would recognize those imports that only import types and remove those imports from the generated .js code.
Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: