百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
T

typed-html

> 前端框架
开源

使用 TypeScript 的 TypeSafe HTML 模板。无需学习模板库。

385 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

使用 TypeScript 的 TypeSafe HTML 模板。无需学习模板库。

Typed HTML

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:

typescript
// example.tsx
const item = 'item';
const icon = 'icon-add';
const ul = 

    
{item}

;

typeof ul; // string

const button = 
    
;

typeof button; // string

console.log(ul);
console.log(button);

Prints:

xml

    
item

    

Getting started

Install:

bash
npm install --save typed-html

Configure your TypeScript compiler for JSX:

json
{
    "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:

typescript
// example.tsx
import * as elements from 'typed-html';

const w = 'world';
const helloWorld = 
Hello {w}
;

typeof helloWorld; // => Just a string of course

However, the following piece of code will NOT compile:

typescript
; // => Error: Property 'foo' does not exist on type 'JSX.IntrinsicElements'.
; // => Error:  Property 'foo' does not exist on type 'HtmlAnchorTag'

Supported environments

Typed HTML supports both NodeJS and (since 2.0) the browser.

For use in the browser, either load typed-html as a module, or use a bundler like webpack or rollup to bundle the package for you.

typescript
// Direct ES import:
import * as elements from './node_modules/typed-html/dist/elements.js';
// OR, when using a bundler like rollup or webpack
import * as elements from 'typed-html';

Supported scenarios

All template scenarios are supported with plain TypeScript.

Control flow

Conditional template with ?

typescript

Repeat a template with Array.map

typescript
const items = ['item', 'item2'];

    {items.map(i => 
{i}
)}

;

Helper templates

Want a helper template? Just call a function

typescript
function listItem(n: number) {
    return 
{n}
;
}

    {[1, 2].map(listItem)}

Using a helper template like an element

Want a helper component? Create a function that implements CustomElementHandler and you can call it like an HTML element.

typescript
import {Attributes, CustomElementHandler} from "typed-html"

function Button(attributes: Attributes | undefined, contents: string[]) {
    return 
;
}
// Or 
const Button: CustomElementHandler = (attributes, contents) => 
;
}
    
console.log(Button Text);

Prints:

xml

React-style children

It's possible to write React-style components as well. Consider the example below.

typescript
import {Attributes, CustomElementHandler} from "typed-html"

function Button({ children, ...attributes }: Attributes) {
    return 
;
}

console.log(Button Text);

Prints:

xml

Sanitization

Security is NOT a feature. This library does NOT sanitize.

typescript
const script = '
';
const body = {script};

Will result in:

xml

If you need sanitization, you can use something like sanitize-html.

Supported HTML

All HTML elements and attributes are supported, except for the svg.

  • Supported html elements: https://dev.w3.org/html5/html-author/#the-elements
  • Supported html events: http://htmlcss.wikia.com/wiki/HTML5_Event_Attributes

Missing an element or attribute? Please create an issue or a PR to add it. It's easy to add.

Void elements

Void elements (elements without closing tags) are supported, however you should close them in TypeScript.

typescript
const img = ; // => Error! JSX element 'img' has no corresponding closing tag.

In the example above, closing the image tag is required for valid TSX code:

typescript
const img = ; // => ''

See this code for a list of supported void elements.

Attribute types

All HTML attributes support a string value, however some attributes also support a number, Date or boolean(or absent value) type:

typescript
; 
// => 

;
;
;
;

const date = new Date('1914-12-20T08:00');
; 
// => 
updated;
old;

// =>   
 
    

Custom elements

You can add custom elements by adding them to the intrinsic elements yourself:

typescript
// MyCustomElements.d.ts

declare namespace JSX {
    interface CustomElement {
        customAttribute?: string;
    }
    interface IntrinsicElements {
        myCustomElement: CustomElement;
    }
}

Now you can use it:

typescript
// UseCustomElement.ts
import * as elements from 'typed-html';

const myElement = 
console.log(myElement);

This prints:

xml

Custom attributes

Custom attribute names are already supported out-of-the-box for attributes with a dash (-) in the name. For example:

typescript

Transformation

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 is not allowed in TypeScript. Therefore `typed-html` will transform to ``.

This transformation also works for custom attributes you define on a custom element yourself. For example:

typescript

Becomes

xml

How this all works

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:

typescript

{[1, 2].map(i => 
{i}
)}

Compiles to:

javascript
elements.createElement("ol", { start: 2 }, [1, 2].map(function (li) { 
    return elements.createElement("li", null, li); 
}));

Which translates to:

xml

    
1

    
2

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

TypeScripthtmltemplate-enginetypescript

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类前端框架
定价开源

> 相关工具

R
React
用于构建用户界面的 JavaScript 库
V
Vue.js
渐进式 JavaScript 框架
N
Next.js
基于 React 的全栈 Web 框架