-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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 embedded in HTML files. #12874
Comments
I'm confused. Isn't this a suggestion for browser vendors (and not TypeScript itself)? Or are you suggesting that The new file extension is probably unnecessary, as the |
TypeScript is a build tool, the need is to take an HTML file with TypeScript code embedded in and output another HTML file with JavaScript compiled code embedded in. TypeScript need to support that natively as WebComponents interest is rising very fast. |
@fgirardey Well, TypeScript is technically a programming language (see first sentence in the README) 🙂 . The I agree that it could be useful for |
I agree with that, we don't need to add a file extension and yes, I don't see any good reason to not... Someone has some? |
@tony19 That's one of the options I've been looking into - use a third party to find In addition - at the moment most editors don't support I suggested the filename extension because I find it's extremely useful to have different file names for input and output. If there isn't a file extension difference, what should It's not a requirement, just a nice to have. |
I second this. TypeScript is already included in HTML files; there's just not a parser that operates on those files/sections of them, that I know of. |
It will be very handy for quick projects or hand-made admin panels actually |
In vue files, you can type: |
Why is this out of scope? |
First sorry for closing without adding an explanation. The request here is achievable today using the compiler API. one example of this already available is the vetur VSCode extension (see vuejs/vetur#94 for more details on how this works). A vue file, similar to other templates, can be preprocessed by a third-party tool that "extracts" the parts that needs to be "checked" and use the TS compiler API to check them and report errors. There is a similar discusssion in #19891 |
It would be very useful for TypeScript to be embedded in HTML files.
Problem
Newer browsers can use HTML imports to reduce the number of requests when loading components. This is used especially in custom elements and libraries that extend them (for instance Polymer).
For instance, suppose I have a custom element with CSS, HTML and script components. I add it to my page with a single statement:
Then
my-component.html
looks something like:This can be made to work with with TypeScript with
<script src="typescript-output.js">
but that results in another server round trip and loses the bundling of CSS, HTML and script that makes the HTML import pattern so useful.The output of the TS transpile should be included in the component HTML when presented to the user.
Solution
One way to do this would be to support a
.tshtml
file format (or possibly.tml
or.ts.html
).This would behave in editors like HTML, but any
<script>
tags in the file would be TypeScript.The Javascript emitter would produce a
.html
file with the contents of any<script>
tags converted to the target JS version. Any content outside of<script>
tags would be included unchanged.This would be consistent pattern for users, anyone familiar with
.tsx
transpiling to.jsx
or.cshtml
in Razor should recognise the behaviour.A file format like
.ts.html
would work with current HTML editors without any changes, but would also add risk for any projects that already had files with that naming format. Perhaps it could be combined with<script language="TypeScript">
tags to avoid that.Risks
If TypeScript doesn't support being embedded in HTML one of two things are likely to happen for all projects that want to use the HTML import browser feature:
.ts
output into.html
files will emerge (they probably already have) or...At the moment these are limited to a few fairly bleeding edge libraries, mostly those using custom elements, shadow DOM and the like. However I'd argue that these are exactly the sort of projects that we want to use TypeScript.
The text was updated successfully, but these errors were encountered: