跳转到内容
本站为社区维护的非官方中文翻译,与 Cloudflare, Inc. 无关联。本站不提供账户登录,请勿在任何非官方站点输入您的 Cloudflare 账户信息。官方文档:developers.cloudflare.com

TypeScript 示例

最后更新 查看 MarkdownAgent 设置

The TypeScriptExample component is used 1414 times on 345 pages.

See all examples of pages that use TypeScriptExample

Used 1414 times.

Pages

Partials

TypeScript 示例

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

组件

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> 属性

filename

类型:string

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

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

playground

类型:boolean

设为 true 时,JavaScript 标签页上会显示一个 Run Worker in Playground 按钮。

code

类型:object

要传递给 Astro Code 组件 ↗的属性。

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

这篇文档对您有帮助吗?