Skip to content

Instantly share code, notes, and snippets.

@1inguini
Created March 21, 2025 02:45
Show Gist options
  • Select an option

  • Save 1inguini/3b290f6e0b6f7de9acbc8af8c8ccc60e to your computer and use it in GitHub Desktop.

Select an option

Save 1inguini/3b290f6e0b6f7de9acbc8af8c8ccc60e to your computer and use it in GitHub Desktop.
TypeScript in single HTML file
<!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