Skip to content

Commit

Permalink
docs: add template literal types
Browse files Browse the repository at this point in the history
  • Loading branch information
Debbl committed Jan 17, 2025
1 parent ec9c7c5 commit 8937e76
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/app/posts/(TIL)/template-literal-types/page.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
title: 模板字面量类型
date: 2025-01-17T15:27:56.070Z
duration: 1min
---

## 模板字面量类型(Template Literal Types)

模板字面量类型是 TypeScript 中的一种类型,它允许你创建可以包含变量的字符串类型。

```ts
type World = "world";
type Greeting = `hello ${World}`; // "hello world"
```

```ts
type EmailLocaleIDs = "welcome_email" | "email_heading";
type FooterLocaleIDs = "footer_title" | "footer_sendoff";

type AllLocaleIDs = `${EmailLocaleIDs | FooterLocaleIDs}_id`;
// "welcome_email_id" | "email_heading_id" | "footer_title_id" | "footer_sendoff_id"
```

## 相关文章

- https://www.typescriptlang.org/docs/handbook/2/template-literal-types.html

0 comments on commit 8937e76

Please sign in to comment.