Security News
pnpm 10.0.0 Blocks Lifecycle Scripts by Default
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
typed-html
Advanced tools
TypeSafe HTML templates using TypeScript. No need to learn a template library.
HTML templates have never been this easy. Type safe using plain TypeScript with a minimal runtime footprint. No need to learn a template language, if you know TypeScript, you're set.
This:
// example.tsx
const item = 'item';
const icon = 'icon-add';
const ul = <ul>
<li>{item}</li>
</ul>;
typeof ul; // string
const button = <button onclick="handleClick">
<i class={icon}></i>
</button>;
typeof button; // string
console.log(ul);
console.log(button);
Prints:
<ul>
<li>item</li>
</ul>
<button onclick="handleClick">
<i class="icon-add"></i>
</button>
Install:
npm install --save typed-html
Configure your TypeScript compiler for JSX:
{
"compilerOptions": {
"jsx": "react",
"jsxFactory": "elements.createElement"
}
}
Although we're configuring the compiler to use React, this is not what is being used.
Instead, we redirect all jsx element to typed-html's elements.createElement
.
Now create a *.tsx file. For example: example.tsx
with the following content:
// example.tsx
import * as elements from 'typed-html';
const w = 'world';
const helloWorld = <p>Hello <strong>{w}</strong></p>;
typeof helloWorld; // => Just a string of course
However, the following piece of code will NOT compile:
<foo></foo>; // => Error: Property 'foo' does not exist on type 'JSX.IntrinsicElements'.
<a foo="bar"></a>; // => Error: Property 'foo' does not exist on type 'HtmlAnchorTag'
All template scenarios are supported with plain TypeScript.
Conditional template with ?
<div>Random > 0.5: {Math.random() > .5 ? <strong>yes</strong> : 'no'}</div>
Repeat a template with Array.map
const items = ['item', 'item2'];
<ul>
{items.map(i => <li>{i}</li>)}
</ul>;
Want a helper template? Just call a function
function listItem(n: number) {
return <li>{n}</li>;
}
<ul>
{[1, 2].map(listItem)}
</ul>
All HTML elements and attributes are supported, except for the svg.
Missing an element or attribute? Please create an issue or a PR to add it. It's easy to add.
Void elements (elements without closing tags) are supported, however you should close them in TypeScript.
const img = <img href="/foo/bar.png">; // => Error! JSX element 'img' has no corresponding closing tag.
In the example above, closing the image tag is required for valid TSX code:
const img = <img href="/foo/bar.png"></img>; // => '<img href="/foo/bar.png">'
See this code for a list of supported void elements.
All HTML attributes support a string value, however some attributes also support a number
, Date
or boolean
(or absent value) type:
<meter value={1} min={0} max={5} low={1} high={4} optimum={3}></meter>;
// => <meter value="1" min="0" max="5" low="1" high="4" optimum="3"></meter>
<ol start={3}></ol>;
<progress value={3} max={4}></progress>;
<td colspan={3} rowspan={3}></td>;
<th colspan={3} rowspan={3}></th>;
const date = new Date('1914-12-20T08:00');
<time datetime={date}></time>;
// => <time datetime="1914-12-20T08:00:00.000Z"></time>
<ins datetime={date}>updated</ins>;
<del datetime={date}>old</del>;
// => <form> <input type="checkbox" checked> </form>
<form novalidate={false}>
<input type="checkbox" checked disabled={false}></input>
</form>
You can add custom elements by adding them to the intrinsic elements yourself:
// MyCustomElements.d.ts
declare namespace JSX {
interface CustomElement {
customAttribute?: string;
}
interface IntrinsicElements {
myCustomElement: CustomElement;
}
}
Now you can use it:
// UseCustomElement.ts
import * as elements from 'typed-html';
const myElement = <myCustomElement customAttribute="customValue"></myCustomElement>
console.log(myElement);
This prints:
<my-custom-element custom-attribute="customValue"></my-custom-element>
Custom attribute names are already supported out-of-the-box for attributes with a dash (-
) in the name. For example:
<button data-menu-item="3"></button>
As a browser is case insensitive when it comes to element and attribute names, it is common practice to use kebab case for this. However <custom-element></custom-element>
is not allowed in TypeScript. Therefore typed-html
will transform <customElement></customElement>
to <custom-element></custom-element>
.
This transformation also works for custom attributes you define on a custom element yourself. For example:
<customElement aCustomAttr="value"></customElement>
Becomes
<custom-element a-custom-attr="value"></custom-element>
The way this works is by using TypeScript's jsx support, but not for jsx/react interoperability. Instead, it defines the normal html tags as IntrinsicElements
in the JSX namespace.
At runtime, the elements.createElement
function is called for every html tag. It simply converts the given element to a string with minimal overhead.
This:
<ol start={2}>{[1, 2].map(i => <li>{i}</li>)}</ol>
Compiles to:
elements.createElement("ol", { start: 2 }, [1, 2].map(function (li) {
return elements.createElement("li", null, li);
}));
Which translates to:
<ol start="2">
<li>1</li>
<li>2</li>
</ol>
FAQs
TypeSafe HTML templates using TypeScript. No need to learn a template library.
We found that typed-html demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
Product
Socket now supports uv.lock files to ensure consistent, secure dependency resolution for Python projects and enhance supply chain security.
Research
Security News
Socket researchers have discovered multiple malicious npm packages targeting Solana private keys, abusing Gmail to exfiltrate the data and drain Solana wallets.