Skip to content

Commit db890ed

Browse files
i18n(zh-cn): Update typescript.mdx and related pages (#12400)
Co-authored-by: Yan <[email protected]>
1 parent de13fb3 commit db890ed

File tree

6 files changed

+108
-50
lines changed

6 files changed

+108
-50
lines changed

src/content/docs/zh-cn/guides/integrations-guide/alpinejs.mdx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ category: renderer
99
i18nReady: true
1010
---
1111

12-
import PackageManagerTabs from '~/components/tabs/PackageManagerTabs.astro'
12+
import PackageManagerTabs from '~/components/tabs/PackageManagerTabs.astro';
13+
import Since from '~/components/Since.astro';
1314

1415
这个 **[Astro 集成][astro-integration]**[Alpine.js](https://alpinejs.dev/) 添加到你的项目中,这样你就可以在页面的任何位置使用 Alpine.js。
1516

@@ -97,7 +98,13 @@ export default defineConfig({
9798

9899
### `entrypoint`
99100

100-
请查阅我们的 [Astro 集成文档][astro-integration] 了解更多关于集成的信息。
101+
<p>
102+
103+
**类型:** `string`<br />
104+
<Since v="0.4.0" pkg="@astrojs/alpinejs" />
105+
</p>
106+
107+
101108
你可以通过设置 `entrypoint` 选项为与根目录相关的的导入说明符(例如 `entrypoint: "/src/entrypoint"`)来扩展 Alpine。
102109

103110
这个文件的默认导出应该是一个函数,该函数接收一个在应用启动前的 Alpine 实例作为参数。这样做可以在高级用例中允许使用自定义指令、插件以及其他自定义功能。
@@ -149,7 +156,7 @@ export default (Alpine: Alpine) => {
149156

150157
## TypeScript 的智能提示
151158

152-
`@astrojs/alpine` 集成会将 `Alpine` 对象添加到全局的 `window` 对象中。为了在你的开发环境中获得 `Alpine` 的自动完成提示,你需要在 `src/env.d.ts` 文件中添加如下代码:
159+
`@astrojs/alpine` 集成会将 `Alpine` 对象添加到 [全局的 `window` 对象](/zh-cn/guides/typescript/#window-和-globalthis)。为了在你的开发环境中获得 `Alpine` 的自动完成提示,你需要在 `src/env.d.ts` 文件中添加如下代码:
153160

154161
```ts title="src/env.d.ts"
155162
interface Window {

src/content/docs/zh-cn/guides/integrations-guide/cloudflare.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ Cloudflare 的环境变量和私密环境变量与 [`astro:env` API](/zh-cn/guid
424424
```
425425
:::
426426

427-
你可以使用 `Runtime` 来为 `runtime` 对象添加类型:
427+
你可以使用 `Runtime` [扩展全局类型](/zh-cn/guides/typescript/#扩展全局类型) 来为 `runtime` 对象添加类型:
428428

429429
```ts title="src/env.d.ts"
430430
type Runtime = import('@astrojs/cloudflare').Runtime<Env>;

src/content/docs/zh-cn/guides/integrations-guide/vercel.mdx

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,12 @@ vercel deploy --prebuilt
9999
要配置这个适配器,将一个对象传递给 `astro.config.mjs` 中的 `vercel()` 函数调用:
100100

101101
### `webAnalytics`
102+
<p>
102103

103104
**类型:** `VercelWebAnalyticsConfig`<br/>
104105
**适用于:** Serverless, Static<br/>
105106
<Since v="3.8.0" pkg="@astrojs/vercel" />
107+
</p>
106108

107109
对于 `@vercel/[email protected]` 或更早的版本时,你可以在 Astro 配置中设置 `webAnalytics: { enabled: true }` 以将 Vercel 的跟踪脚本注入到你所有的页面中。
108110

@@ -124,9 +126,12 @@ export default defineConfig({
124126

125127
### `imagesConfig`
126128

129+
<p>
130+
127131
**类型:** `VercelImageConfig`<br/>
128132
**适用于:** Serverless, Static<br/>
129133
<Since v="3.3.0" pkg="@astrojs/vercel" />
134+
</p>
130135

131136
[Vercel 的图片优化 API](https://vercel.com/docs/concepts/image-optimization) 选项。查看 [Vercel 的图片优化配置文档](https://vercel.com/docs/build-output-api/v3/configuration#images)以获取支持的参数的完整列表。
132137

@@ -149,9 +154,12 @@ export default defineConfig({
149154

150155
### `imageService`
151156

157+
<p>
158+
152159
**类型:** `boolean`<br/>
153160
**适用于:** Serverless, Static<br/>
154161
<Since v="3.3.0" pkg="@astrojs/vercel" />
162+
</p>
155163

156164
启用后,在生产中会自动配置并使用由 Vercel Image Optimization API 提供支持的[图像服务](/zh-cn/reference/image-service-reference/) 。在开发中,将使用由 [`devImageService`](#devimageservice) 指定的图像服务。
157165

@@ -190,10 +198,13 @@ import astroLogo from '../assets/logo.png';
190198

191199
### `devImageService`
192200

201+
<p>
202+
193203
**类型:** `'sharp' | string`<br/>
194-
**适用于:** Serverless, Static
204+
**默认值:** `sharp`<br />
205+
**适用于:** Serverless, Static<br />
195206
<Since v="3.8.0" pkg="@astrojs/vercel" />
196-
**默认值:** `sharp`
207+
</p>
197208

198209
[imageService](#imageservice) 启用时,允许你在开发中配置使用哪个图像服务。如果你无法在开发机器上安装 Sharp 的依赖项,但使用另一个图像服务(如 Squoosh)可以让你在开发环境中预览图像,这可能很有用。但构建不会受其影响,将始终使用 Vercel 的图像优化。
199210

@@ -214,10 +225,13 @@ export default defineConfig({
214225

215226
### `isr`
216227

228+
<p>
229+
217230
**类型:** <code>boolean | VercelISRConfig</code><br/>
218-
**适用于:** Serverless
231+
**默认值:** `false`<br/>
232+
**适用于:** Serverless<br/>
219233
<Since v="7.2.0" pkg="@astrojs/vercel" />
220-
**默认值:** `false`
234+
</p>
221235

222236
允许你的项目作为一个 [ISR (增量式静态再生)](https://vercel.com/docs/incremental-static-regeneration) 功能部署,这个功能以首次请求后预渲染页面相同的方式缓存你按需渲染的页面。
223237

@@ -286,8 +300,11 @@ export default defineConfig({
286300

287301
### `includeFiles`
288302

303+
<p>
304+
289305
**类型:** `string[]`<br/>
290306
**适用于:** Serverless
307+
</p>
291308

292309
使用此属性来强制将文件与你的函数打包在一起。当你发现缺失文件时,这很有帮助。
293310

@@ -303,7 +320,10 @@ export default defineConfig({
303320
});
304321
```
305322

323+
<p>
324+
306325
### `excludeFiles`
326+
</p>
307327

308328
**类型:** `string[]`<br/>
309329
**适用于:** Serverless
@@ -324,8 +344,11 @@ export default defineConfig({
324344

325345
### `maxDuration`
326346

347+
<p>
348+
327349
**类型:** `number`<br/>
328350
**适用于:** Serverless
351+
</p>
329352

330353
使用这个属性来延长或限制无服务器函数运行的最长持续时间(以秒为单位),直到超时。请参阅 [Vercel 文档](https://vercel.com/docs/functions/serverless-functions/runtimes#maxduration) 获取你的账号计划的默认和最大时限。
331354

@@ -343,10 +366,12 @@ export default defineConfig({
343366

344367
### `skewProtection`
345368

369+
<p>
370+
346371
**类型:** `boolean`<br/>
347372
**适用于:** Serverless
348373
<Since pkg="@astrojs/vercel" v="7.6.0" />
349-
374+
</p>
350375
使用此属性启用 [Vercel Skew protection](https://vercel.com/docs/deployments/skew-protection)(适用于 Vercel Pro 和企业账户)。
351376

352377
```js title="astro.config.mjs" ins={7}
@@ -380,7 +405,7 @@ export default defineConfig({
380405
}),
381406
});
382407
```
383-
边缘中间件可以用 `ctx.locals.vercel.edge` 访问 Vercel 的 [`RequestContext`](https://vercel.com/docs/functions/edge-middleware/middleware-api#requestcontext) 。如果你正在使用 TypeScript,可以通过更新 `src/env.d.ts` 来使用 `EdgeLocals` 获得正确的类型定义:
408+
边缘中间件可以用 `ctx.locals.vercel.edge` 访问 Vercel 的 [`RequestContext`](https://vercel.com/docs/functions/edge-middleware/middleware-api#requestcontext) 。如果你正在使用 TypeScript,可以通过更新 `src/env.d.ts` 来使用 `EdgeLocals` [获得正确的类型定义](/zh-cn/guides/typescript/#扩展全局类型)
384409

385410
```ts
386411
type EdgeLocals = import('@astrojs/vercel').EdgeLocals

src/content/docs/zh-cn/guides/middleware.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ export const onRequest = (context, next) => {
139139
};
140140
```
141141

142-
要给 `Astro.locals` 内的信息定义类型,也就是在 `.astro` 文件和中间件代码中能提供自动补全, `env.d.ts` 文件中声明一个全局命名空间
142+
要给 `Astro.locals` 内的信息定义类型,也就是在 `.astro` 文件和中间件代码中能提供自动补全,通过在 `env.d.ts` 文件中声明一个全局命名空间来 [扩展全局类型](/zh-cn/guides/typescript/#扩展全局类型)
143143

144144
```ts title="src/env.d.ts"
145145
type User = {

src/content/docs/zh-cn/guides/sessions.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ export const onRequest = defineMiddleware(async (context, next) => {
123123

124124
默认情况下,会话数据的类型是未被定义的,你可以将任意数据存储在任何 key 中。使用 [devalue](https://github.com/Rich-Harris/devalue) 对值进行序列化和反序列化,该库与 content layer 和 action 所使用的库相同。这意味着支持的类型是相同的,包括字符串、数字、`Date``Map``Set``URL`、数组和简单对象。
125125

126-
你可以通过创建 `src/env.d.ts` 文件并为 `App.SessionData` 添加类型,来选择性地为你的会话数据定义 TypeScript 类型:
126+
你可以通过创建 `src/env.d.ts` 文件并为 `App.SessionData` 添加类型,来选择性地为你的会话数据 [定义 TypeScript 类型](/zh-cn/guides/typescript/#扩展全局类型)
127127

128128
```ts title="src/env.d.ts"
129129
declare namespace App {

src/content/docs/zh-cn/guides/typescript.mdx

Lines changed: 64 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,6 @@ Astro 中包含三个可扩展的 `tsconfig.json` 模板:`base`、`strict` 和
4040
}
4141
```
4242

43-
你可以创建 `src/env.d.ts` 作为添加自定义类型声明的约定,或者如果你没有 `tsconfig.json`,则可以从 Astro 类型中受益:
44-
45-
```ts title="src/env.d.ts"
46-
// 自定义类型声明
47-
declare var myString: string;
48-
49-
// Astro 类型,如果你已经有 `tsconfig.json`,则不需要
50-
/// <reference path="../.astro/types.d.ts" />
51-
```
52-
5343
### TypeScript 编辑器插件
5444

5545
当你不使用[官方 Astro VS Code 扩展](https://marketplace.visualstudio.com/items?itemName=astro-build.astro-vscode)时,可以单独安装 [Astro TypeScript 插件](https://www.npmjs.com/package/@astrojs/ts-plugin)。该插件由 VS Code 扩展自动安装和配置,你无需同时安装两者。
@@ -119,7 +109,7 @@ import type { SomeType } from "./script";
119109

120110
## 导入别名
121111

122-
Astro 支持你在 `tsconfig.json` 文件里的 `paths` 配置所定义的 [导入别名](/zh-cn/guides/imports/#别名)
112+
Astro 支持你在 `tsconfig.json` 文件里的 `paths` 配置所定义的导入别名。 [阅读导入指南](/zh-cn/guides/imports/#别名) 了解更多
123113

124114
```astro title="src/pages/about/nate.astro" "@components" "@layouts"
125115
---
@@ -139,7 +129,19 @@ import Layout from "@layouts/Layout.astro";
139129
}
140130
```
141131

142-
## 扩展 `window``globalThis`
132+
## 扩展全局类型
133+
134+
你可以创建 `src/env.d.ts` 作为约定来添加自定义类型声明,或者在没有 `tsconfig.json` 的情况下利用 Astro 类型的优势:
135+
136+
```ts title="src/env.d.ts"
137+
// 自定义类型声明
138+
declare var myString: string;
139+
140+
// Astro 类型,如果你已有 `tsconfig.json` 则无需添加。
141+
/// <reference path="../.astro/types.d.ts" />
142+
```
143+
144+
### `window``globalThis`
143145

144146
你可能需要在全局对象中添加一项属性。可以通过在 `env.d.ts` 文件中使用 `declare` 关键字添加顶级声明来实现这一点:
145147

@@ -160,6 +162,56 @@ interface Window {
160162
}
161163
```
162164

165+
### 添加非标准属性
166+
167+
你可能需要为自定义属性或 CSS 属性定义类型。你可以通过在 `.d.ts` 文件中重新声明 `astroHTML.JSX` 命名空间来扩展默认的 JSX 定义,以添加非标准属性。
168+
169+
```ts title="src/env.d.ts"
170+
declare namespace astroHTML.JSX {
171+
interface HTMLAttributes {
172+
"data-count"?: number;
173+
"data-label"?: string;
174+
}
175+
176+
// 向 style 对象添加一个 CSS 自定义属性
177+
interface CSSProperties {
178+
"--theme-color"?: "black" | "white";
179+
}
180+
}
181+
```
182+
183+
:::note
184+
`astroHTML` 会在 `.astro` 组件中全局注入。要在 TypeScript 文件中使用它,可以使用 [三斜杠指令](https://www.typescriptlang.org/docs/handbook/triple-slash-directives.html)
185+
186+
```ts
187+
/// <reference types="astro/astro-jsx" />
188+
189+
type MyAttributes = astroHTML.JSX.ImgHTMLAttributes;
190+
```
191+
:::
192+
193+
### 使用导入
194+
195+
你可能希望通过复用项目中其他地方或外部库中声明的类型来扩展全局类型。为此,请使用 [动态导入](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import)
196+
197+
```ts title="src/env.d.ts"
198+
type Product = {
199+
id: string;
200+
name: string;
201+
price: number;
202+
};
203+
204+
declare namespace App {
205+
interface Locals {
206+
orders: Map<string, Product[]>
207+
session: import("./lib/server/session").Session | null;
208+
user: import("my-external-library").User;
209+
}
210+
}
211+
```
212+
213+
`.d.ts` 文件是一种 [环境模块](https://www.typescriptlang.org/docs/handbook/modules/reference.html#ambient-modules) 声明。虽然它的语法与 ES 模块类似,但这些文件不允许顶层导入/导出。如果 TypeScript 遇到一个 `.d.ts` 文件,它将被视为 [模块增强](https://www.typescriptlang.org/docs/handbook/declaration-merging.html#module-augmentation),这将破坏你的全局类型。
214+
163215
## 组件参数
164216

165217
Astro 支持通过 TypeScript 来定义你的组件参数。为了启动它,你需要将一个名为 `Props` 的 TypeScript 接口添加到你的的组件。你可以(可选的)使用 `export` 语句将其导出,但这不是强制的。[Astro VS Code 扩展](/zh-cn/editor-setup/)会自动寻找 `Props` 接口,并且当你在其他模板内使用该组件时,给你提供适当的 TS 支持。
@@ -212,32 +264,6 @@ const { href, ...attrs } = Astro.props;
212264
</a>
213265
```
214266

215-
也可以通过在 `.d.ts` 文件中重新声明命名空间 `astroHTML.JSX`,来为默认的 JSX 定义扩展非标准属性。
216-
217-
```ts title="src/custom-attributes.d.ts"
218-
declare namespace astroHTML.JSX {
219-
interface HTMLAttributes {
220-
"data-count"?: number;
221-
"data-label"?: string;
222-
}
223-
224-
// 添加一个 CSS 自定义属性到 style 对象中
225-
interface CSSProperties {
226-
"--theme-color"?: "black" | "white";
227-
}
228-
}
229-
```
230-
231-
:::note
232-
`astroHTML` 被全局注入到 `.astro` 组件中。如果要在 TypeScript 文件中使用它,请使用一个[三斜杠指令(triple-slash directive)](https://www.typescriptlang.org/docs/handbook/triple-slash-directives.html)来进行引用:
233-
234-
```ts
235-
/// <reference types="astro/astro-jsx" />
236-
237-
type MyAttributes = astroHTML.JSX.ImgHTMLAttributes;
238-
```
239-
:::
240-
241267
### `ComponentProps` 类型
242268

243269
<p><Since v="4.3.0" /></p>

0 commit comments

Comments
 (0)