跳转到内容
搜索文档

TypeScript 示例

最后更新 查看 MarkdownAgent 设置

The TypeScriptExample component is used 1360 times on 271 pages.

See all examples of pages that use TypeScriptExample

Used 1360 times.

Pages

Partials

TypeScript 示例

TypeScriptExample 组件使用 ts-blank-space 从您的示例中移除 TypeScript 特定的语法并提供一个 JavaScript 标签页。这通过只需维护单个示例来减少维护负担。

lang 设置为 ts 时,此组件会自动在 GitHubCode 组件中使用。

组件

import { TypeScriptExample } from "~/components";

<TypeScriptExample code={{
  collapse: "1-2"
}}>
```ts
// comment to demonstrate
// collapsible sections
interface Environment {
  KV: KVNamespace;
}

export default {
  async fetch(req, env, ctx): Promise<Response> {
    if (req !== "POST") {
        return new Response("Method Not Allowed", {
            status: 405,
            headers: {
                "Allow": "POST"
            }
        });
    }

    await env.KV.put("foo", "bar");

    return new Response();
  }
} satisfies ExportedHandler<Environment>
```
</TypeScriptExample>

<TypeScriptExample> Props

filename

类型string

一个可选的文件名,以 .ts 结尾。

.ts 将在 JavaScript 标签页中被替换为 .js

playground

类型boolean

如果设置为 true,将在 JavaScript 标签页上显示 Run Worker in Playground 按钮。

code

类型object

传递给 Expressive Code 组件的 Props。

这些 props 将应用于这两个代码块,因此像 collapse 这样的选项可能无法按预期工作,因为行可能会从 TypeScript 代码中移除。

这篇文档对您有帮助吗?