TypeScript example·续5

  • <a href="https://developers.cloudflare.com/workers/testing/vitest-integration/mock-outbound-requests/">/workers/testing/vitest-integration/mock-outbound-requests/</a>-<a href="https://github.com/cloudflare/cloudflare-docs/blob/production/src/content/docs/workers/testing/vitest-integration/mock-outbound-requests.mdx">Source</a>
  • <a href="https://developers.cloudflare.com/workers/testing/vitest-integration/write-your-first-test/">/workers/testing/vitest-integration/write-your-first-test/</a>-<a href="https://github.com/cloudflare/cloudflare-docs/blob/production/src/content/docs/workers/testing/vitest-integration/write-your-first-test.mdx">Source</a>
  • <a href="https://developers.cloudflare.com/workers/tutorials/connect-to-mongodb-atlas/">/workers/tutorials/connect-to-mongodb-atlas/</a>-<a href="https://github.com/cloudflare/cloudflare-docs/blob/production/src/content/docs/workers/tutorials/connect-to-mongodb-atlas.mdx">Source</a>
  • <a href="https://developers.cloudflare.com/workers/versions-and-deployments/gradual-deployments/version-affinity/">/workers/versions-and-deployments/gradual-deployments/version-affinity/</a>-<a href="https://github.com/cloudflare/cloudflare-docs/blob/production/src/content/docs/workers/versions-and-deployments/gradual-deployments/version-affinity.mdx">Source</a>
  • <a href="https://developers.cloudflare.com/workers/versions-and-deployments/version-overrides/">/workers/versions-and-deployments/version-overrides/</a>-<a href="https://github.com/cloudflare/cloudflare-docs/blob/production/src/content/docs/workers/versions-and-deployments/version-overrides.mdx">Source</a>
  • <a href="https://developers.cloudflare.com/workers/vite-plugin/reference/api/">/workers/vite-plugin/reference/api/</a>-<a href="https://github.com/cloudflare/cloudflare-docs/blob/production/src/content/docs/workers/vite-plugin/reference/api.mdx">Source</a>
  • <a href="https://developers.cloudflare.com/workers/wrangler/api/">/workers/wrangler/api/</a>-<a href="https://github.com/cloudflare/cloudflare-docs/blob/production/src/content/docs/workers/wrangler/api.mdx">Source</a>
  • <a href="https://developers.cloudflare.com/workflows/build/call-workflows-from-pages/">/workflows/build/call-workflows-from-pages/</a>-<a href="https://github.com/cloudflare/cloudflare-docs/blob/production/src/content/docs/workflows/build/call-workflows-from-pages.mdx">Source</a>
  • <a href="https://developers.cloudflare.com/workflows/build/events-and-parameters/">/workflows/build/events-and-parameters/</a>-<a href="https://github.com/cloudflare/cloudflare-docs/blob/production/src/content/docs/workflows/build/events-and-parameters.mdx">Source</a>
  • <a href="https://developers.cloudflare.com/workflows/build/rules-of-workflows/">/workflows/build/rules-of-workflows/</a>-<a href="https://github.com/cloudflare/cloudflare-docs/blob/production/src/content/docs/workflows/build/rules-of-workflows.mdx">Source</a>
  • <a href="https://developers.cloudflare.com/workflows/build/sleeping-and-retrying/">/workflows/build/sleeping-and-retrying/</a>-<a href="https://github.com/cloudflare/cloudflare-docs/blob/production/src/content/docs/workflows/build/sleeping-and-retrying.mdx">Source</a>
  • <a href="https://developers.cloudflare.com/workflows/build/subscribe-to-instance-events/">/workflows/build/subscribe-to-instance-events/</a>-<a href="https://github.com/cloudflare/cloudflare-docs/blob/production/src/content/docs/workflows/build/subscribe-to-instance-events.mdx">Source</a>
  • <a href="https://developers.cloudflare.com/workflows/build/trigger-workflows/">/workflows/build/trigger-workflows/</a>-<a href="https://github.com/cloudflare/cloudflare-docs/blob/production/src/content/docs/workflows/build/trigger-workflows.mdx">Source</a>
  • <a href="https://developers.cloudflare.com/workflows/build/workers-api/">/workflows/build/workers-api/</a>-<a href="https://github.com/cloudflare/cloudflare-docs/blob/production/src/content/docs/workflows/build/workers-api.mdx">Source</a>
  • <a href="https://developers.cloudflare.com/workflows/examples/wait-for-event/">/workflows/examples/wait-for-event/</a>-<a href="https://github.com/cloudflare/cloudflare-docs/blob/production/src/content/docs/workflows/examples/wait-for-event.mdx">Source</a>

Partials

  • <a href="https://github.com/cloudflare/cloudflare-docs/blob/production/src/content/partials/agents/common-integration-patterns.mdx">src/content/partials/agents/common-integration-patterns.mdx</a>
  • <a href="https://github.com/cloudflare/cloudflare-docs/blob/production/src/content/partials/agents/troubleshooting.mdx">src/content/partials/agents/troubleshooting.mdx</a>
  • <a href="https://github.com/cloudflare/cloudflare-docs/blob/production/src/content/partials/browser-run/example-quick-action-binding.mdx">src/content/partials/browser-run/example-quick-action-binding.mdx</a>
  • <a href="https://github.com/cloudflare/cloudflare-docs/blob/production/src/content/partials/workers/cloudflare-config.mdx">src/content/partials/workers/cloudflare-config.mdx</a>
  • <a href="https://github.com/cloudflare/cloudflare-docs/blob/production/src/content/partials/workers/navigation_requests.mdx">src/content/partials/workers/navigation_requests.mdx</a>
  • <a href="https://github.com/cloudflare/cloudflare-docs/blob/production/src/content/partials/workers/request-signal-example.mdx">src/content/partials/workers/request-signal-example.mdx</a>
  • <a href="https://github.com/cloudflare/cloudflare-docs/blob/production/src/content/partials/workers/service-binding-rpc-example.mdx">src/content/partials/workers/service-binding-rpc-example.mdx</a>
  • <a href="https://github.com/cloudflare/cloudflare-docs/blob/production/src/content/partials/workers/service-binding-rpc-functions-example.mdx">src/content/partials/workers/service-binding-rpc-functions-example.mdx</a>

</details>

TypeScript examples

The TypeScriptExample component uses ts-blank-space ↗︎ to remove TypeScript-specific syntax from your example and provide a JavaScript tab. This reduces maintenance burden by only having a single example to maintain.

Note

Some TypeScript syntax influences runtime behaviour, and cannot be stripped.

Please refer to the Unsupported Syntax ↗︎ section of the project's README.

Caution

Code blocks are typically configured with options on the opening fence, like so:

```ts collapse={1-2}
// ...
```

These cannot be extracted by TypeScriptExample so they must be moved to the code prop.

<TypeScriptExample code={{
  collapse: "1-2"
}}>
```ts
// ...
```
</TypeScriptExample>

Component

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&lt;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&lt;Environment>
```
</TypeScriptExample>

<TypeScriptExample> Props

filename

type: string

An optional filename, ending in .ts.

.ts will be replaced by .js for the JavaScript tab.

playground

type: boolean

If set to true, a Run Worker in Playground button will appear on the JavaScript tab.

code

type: object

Props to pass to the Astro Code component ↗︎.

These props will apply to both code blocks and so options like collapse may not work as expected, as lines may be removed from the TypeScript code.

Was this helpful?

YesNo

On this page

TypeScript example·续5 - 图1Docs

{"@context":"https://schema.org","@type":"TechArticle","@id":"https://developers.cloudflare.com/style-guide/build-the-page/components/typescript-example/#page","headline":"TypeScript example","description":"Show TypeScript and auto-transpiled JavaScript tabs.","url":"https://developers.cloudflare.com/style-guide/build-the-page/components/typescript-example/","inLanguage":"en","image":"https://developers.cloudflare.com/style-guide/build-the-page/components/typescript-example/og.png?v=90f6b67264c3fdfd","dateModified":"2026-08-20","publisher":{"@type":"Organization","name":"Cloudflare","description":"One platform for your apps, agents, and workforce. Build, secure, and scale without managing infrastructure","url":"https://www.cloudflare.com/","sameAs":["https://github.com/cloudflare","https://www.linkedin.com/company/cloudflare","https://x.com/cloudflare"],"logo":{"@type":"ImageObject","url":"https://developers.cloudflare.com/logo.svg"},"address":{"@type":"PostalAddress","streetAddress":"101 Townsend St","addressLocality":"San Francisco","addressRegion":"CA","postalCode":"94107","addressCountry":"US"},"contactPoint":[{"@type":"ContactPoint","contactType":"Customer Support","url":"https://support.cloudflare.com/","availableLanguage":["English"]},{"@type":"ContactPoint","contactType":"Sales","url":"https://www.cloudflare.com/contact/","availableLanguage":["English"]}]},"isPartOf":{"@type":"WebSite","@id":"https://developers.cloudflare.com/#website","name":"Cloudflare Docs","url":"https://developers.cloudflare.com/"}}


description: Guidelines for using components in documentation. title: Usage image: https://developers.cloudflare.com/style-guide/build-the-page/components/usage/og.png?v=f43f02caed168b2a


Skip to content

Documentation Index
Fetch the complete documentation index at: https://developers.cloudflare.com/style-guide/llms.txt
Use this file to discover all available pages before exploring further.