Created
March 21, 2025 02:45
-
-
Save 1inguini/3b290f6e0b6f7de9acbc8af8c8ccc60e to your computer and use it in GitHub Desktop.
TypeScript in single HTML file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html> | |
| <html lang="en"> | |
| <head> | |
| <meta charset="UTF-8" /> | |
| <meta name="viewport" content="width=device-width, initial-scale=1.0" /> | |
| <title>single-html typescript</title> | |
| <script async type="module"> | |
| import { | |
| transpile, | |
| ScriptTarget, | |
| } from "https://esm.sh/typescript@5.6.3?target=esnext"; | |
| for (const { src, type, textContent } of document.getElementsByTagName( | |
| "script" | |
| )) { | |
| if (type !== "text/typescript") { | |
| continue; | |
| } | |
| const source = src ? fetch(src).then((r) => r.text()) : textContent; | |
| const js = transpile(await source, { | |
| target: ScriptTarget.ESNext, | |
| }); | |
| const script = new Blob([js], { type: "text/javascript" }); | |
| import(URL.createObjectURL(script)); | |
| } | |
| </script> | |
| </head> | |
| <body> | |
| <script type="text/typescript"> | |
| interface Test { | |
| a: string; | |
| b: number; | |
| c: boolean; | |
| } | |
| const test: Test = { | |
| a: "foo", | |
| b: 111, | |
| c: true | |
| }; | |
| document.getElementById<HTMLSpanElement>("status").textContent = "Complete!"; | |
| </script> | |
| <p>[Status] <span id="status">Processing...</span></p> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment