Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/content/docs/ko/guides/environment-variables.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ PUBLIC_ANYBODY=there

`.env.[mode]` 파일에서 더 많은 사용자 정의 환경 변수를 정의할 수 있지만, `PUBLIC_` 접두사가 붙은 사용자 정의 환경 변수에 대해 TypeScript IntelliSense를 얻고 싶을 수 있습니다.

이를 위해 `src/`에 `env.d.ts`를 생성하고 다음과 같이 `ImportMetaEnv`를 구성할 수 있습니다.
이를 위해 `src/`에 `env.d.ts` 파일을 생성하여 [전역 타입을 확장](/ko/guides/typescript/#전역-타입-확장하기)하고 `ImportMetaEnv`를 다음과 같이 구성할 수 있습니다.

```ts title="src/env.d.ts"
interface ImportMetaEnv {
Expand Down
14 changes: 3 additions & 11 deletions src/content/docs/ko/guides/imports.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ title: 가져오기 참조
description: Astro 프로젝트에 다양한 파일 타입을 가져오는 방법 알아보기
i18nReady: true
---

import RecipeLinks from '~/components/RecipeLinks.astro';
import ReadMore from '~/components/ReadMore.astro';

Expand Down Expand Up @@ -174,22 +173,17 @@ import logoUrl from '../../assets/logo.png?url';

`tsconfig.json` 파일에서 별칭 가져오기를 추가할 수 있습니다.

```json title="tsconfig.json" ins={5-6}
```json title="tsconfig.json" ins={4-5}
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@components/*": ["src/components/*"],
"@assets/*": ["src/assets/*"]
"@components/*": ["./src/components/*"],
"@assets/*": ["./src/assets/*"]
}
}
}
```

:::note
별칭 경로를 확인할 수 있도록 `compilerOptions.baseUrl`이 설정되어 있는지 확인하세요.
:::

이 구성이 변경되면 개발 서버가 자동으로 다시 시작됩니다. 이제 프로젝트의 어느 곳에서나 별칭을 사용하여 가져올 수 있습니다.

```astro title="src/pages/about/company.astro" ins="@components" ins="@assets"
Expand All @@ -199,8 +193,6 @@ import logoUrl from '@assets/logo.png?url';
---
```

이러한 별칭은 [VS Code](https://code.visualstudio.com/docs/languages/jsconfig) 및 기타 편집기에 자동으로 통합됩니다.

## `import.meta.glob()`

[Vite의 `import.meta.glob()`](https://ko.vite.dev/guide/features.html#glob-import)는 glob 패턴을 사용하여 일치하는 파일 경로를 찾아 한 번에 여러 파일을 가져오는 방법입니다.
Expand Down
3 changes: 1 addition & 2 deletions src/content/docs/ko/guides/integrations-guide/alpinejs.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ githubIntegrationURL: 'https://github.com/withastro/astro/tree/main/packages/int
category: renderer
i18nReady: true
---

import PackageManagerTabs from '~/components/tabs/PackageManagerTabs.astro'

이 **[Astro 통합][astro-integration]은** 프로젝트에 [Alpine.js](https://alpinejs.dev/)를 추가하므로 페이지 어디에서나 Alpine.js를 사용할 수 있습니다.
Expand Down Expand Up @@ -148,7 +147,7 @@ export default (Alpine: Alpine) => {

## TypeScript용 Intellisense

`@astrojs/alpine` 통합은 전역 window 객체에 `Alpine`을 추가합니다. IDE 자동 완성을 위해 `src/env.d.ts` 파일에 다음을 추가하세요:
`@astrojs/alpine` 통합은 [전역 window 객체](/ko/guides/typescript/#window-및-globalthis-확장)에 `Alpine`을 추가합니다. IDE 자동 완성을 위해 `src/env.d.ts` 파일에 다음을 추가하세요.

```ts title="src/env.d.ts"
interface Window {
Expand Down
3 changes: 1 addition & 2 deletions src/content/docs/ko/guides/integrations-guide/cloudflare.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ githubIntegrationURL: 'https://github.com/withastro/astro/tree/main/packages/int
category: adapter
i18nReady: true
---

import PackageManagerTabs from '~/components/tabs/PackageManagerTabs.astro'
import ReadMore from '~/components/ReadMore.astro';
import Since from '~/components/Since.astro';
Expand Down Expand Up @@ -426,7 +425,7 @@ Cloudflare 환경 변수와 비밀은 [`astro:env` API](/ko/guides/environment-v
```
:::

`Runtime`을 사용하여 `runtime` 객체의 타입을 설정할 수 있습니다.
`Runtime`을 사용한 [전역 타입 확장](/ko/guides/typescript/#전역-타입-확장하기)을 통해 `runtime` 객체의 타입을 지정할 수 있습니다.

```ts title="src/env.d.ts"
type Runtime = import('@astrojs/cloudflare').Runtime<Env>;
Expand Down
2 changes: 1 addition & 1 deletion src/content/docs/ko/guides/integrations-guide/netlify.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ const {
<h1>Hello there, friendly visitor from {city}!</h1>
```

TypeScript를 사용하는 경우 `NetlifyLocals`를 사용하도록 `src/env.d.ts`를 업데이트하여 적절한 타입을 얻을 수 있습니다.
TypeScript를 사용하는 경우 `NetlifyLocals`를 사용하도록 `src/env.d.ts`를 업데이트하여 [적절한 타입](/ko/guides/typescript/#전역-타입-확장하기)을 얻을 수 있습니다.

```ts title="src/env.d.ts"
type NetlifyLocals = import('@astrojs/netlify').NetlifyLocals;
Expand Down
3 changes: 1 addition & 2 deletions src/content/docs/ko/guides/integrations-guide/vercel.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -388,8 +388,7 @@ export default defineConfig({
});
```

에지 미들웨어는 `ctx.locals.vercel.edge`로 Vercel의 [`RequestContext`](https://vercel.com/docs/functions/edge-middleware/middleware-api#requestcontext)에 액세스할 수 있습니다. TypeScript를 사용하는 경우 `EdgeLocals`를 사용하기 위해 `src/env.d.ts` 파일을 업데이트하여 적절한 타입을 얻을 수 있습니다.

에지 미들웨어는 `ctx.locals.vercel.edge`로 Vercel의 [`RequestContext`](https://vercel.com/docs/functions/edge-middleware/middleware-api#requestcontext)에 액세스할 수 있습니다. TypeScript를 사용하는 경우 `EdgeLocals`를 사용하기 위해 `src/env.d.ts` 파일을 업데이트하여 [적절한 타입](/ko/guides/typescript/#전역-타입-확장하기)을 얻을 수 있습니다.

```ts
type EdgeLocals = import('@astrojs/vercel').EdgeLocals
Expand Down
2 changes: 1 addition & 1 deletion src/content/docs/ko/guides/middleware.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ export const onRequest = (context, next) => {
};
```

`.astro` 파일과 미들웨어 코드에서 자동 완성 기능을 제공하는 `Astro.locals`의 데이터에 타입 정보를 제공하려면 `env.d.ts` 파일에 전역 네임스페이스를 선언하세요.
`.astro` 파일과 미들웨어 코드에서 자동 완성 기능을 제공하는 `Astro.locals`의 데이터에 타입 정보를 제공하려면 `env.d.ts` 파일에 전역 네임스페이스를 선언하여 [전역 타입을 확장](/ko/guides/typescript/#전역-타입-확장하기)하세요.

```ts title="src/env.d.ts"
type User = {
Expand Down
3 changes: 1 addition & 2 deletions src/content/docs/ko/guides/sessions.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ title: 세션
description: 요청 시 렌더링되는 페이지에 대한 요청 간에 데이터를 공유합니다.
i18nReady: true
---

import Since from '~/components/Since.astro';
import ReadMore from '~/components/ReadMore.astro';

Expand Down Expand Up @@ -126,7 +125,7 @@ export const onRequest = defineMiddleware(async (context, next) => {

기본적으로 세션 데이터는 타입이 지정되지 않으며, 어떤 키든 임의의 데이터를 저장할 수 있습니다. 값은 콘텐츠 컬렉션 및 액션에서 사용되는 것과 동일한 라이브러리인 [devalue](https://github.com/Rich-Harris/devalue)를 사용하여 직렬화 및 역직렬화됩니다. 이는 지원되는 타입이 동일하며, 문자열, 숫자, `Date`, `Map`, `Set`, `URL`, 배열 및 일반 객체를 포함한다는 의미입니다.

선택적으로 `src/env.d.ts` 파일을 만들고 `App.SessionData` 타입 선언을 추가하여 세션 데이터의 TypeScript 타입을 정의할 수 있습니다.
선택적으로 `src/env.d.ts` 파일을 만들고 `App.SessionData` 타입 선언을 추가하여 세션 데이터의 [TypeScript 타입을 정의](/ko/guides/typescript/#전역-타입-확장하기)할 수 있습니다.

```ts title="src/env.d.ts"
declare namespace App {
Expand Down
106 changes: 64 additions & 42 deletions src/content/docs/ko/guides/typescript.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,6 @@ Astro에는 확장 가능한 세 가지 `tsconfig.json` 템플릿인 `base`, `st
}
```

`src/env.d.ts` 파일은 사용자 정의 타입 선언을 추가하기 위한 관례로서, 또는 `tsconfig.json`이 없는 경우 Astro 타입을 활용하기 위해 생성할 수 있습니다.

사용자 정의 타입 선언을 추가하거나 `tsconfig.json`이 없는 경우 Astro 타입을 활용하기 위해 `src/env.d.ts`를 규칙으로 생성할 수 있습니다.

```ts title="src/env.d.ts"
// 사용자 정의 타입을 선언합니다.
declare var myString: string;

// Astro 타입입니다. `tsconfig.json`이 이미 있는 경우에는 필요하지 않습니다.
/// <reference path="../.astro/types.d.ts" />
```

### TypeScript 편집기 플러그인

[공식 Astro VS Code 확장 프로그램](https://marketplace.visualstudio.com/items?itemName=astro-build.astro-vscode)을 사용하지 않을 때 [Astro TypeScript 플러그인](https://www.npmjs.com/package/@astrojs/ts-plugin)을 별도로 설치할 수 있습니다. 이 플러그인은 VSCode 확장 프로그램에 의해 자동으로 설치 및 구성되므로 둘 다 설치할 필요는 없습니다.
Expand Down Expand Up @@ -120,7 +108,7 @@ import type { SomeType } from "./script";

## 별칭 가져오기

Astro는 `tsconfig.json` `paths` 구성에서 정의하는 [별칭 가져오기](/ko/guides/imports/#별칭)를 지원합니다. 자세한 내용은 [가이드](/ko/guides/imports/#별칭)를 참조하세요.
Astro는 `tsconfig.json` `paths` 구성에서 정의하는 별칭 가져오기를 지원합니다. 자세한 내용은 [가져오기 가이드](/ko/guides/imports/#별칭)를 참조하세요.

```astro title="src/pages/about/nate.astro" "@components" "@layouts"
---
Expand All @@ -140,7 +128,19 @@ import Layout from "@layouts/Layout.astro";
}
```

## `window` 및 `globalThis` 확장
## 전역 타입 확장하기

`src/env.d.ts`를 생성하여 사용자 정의 타입 선언을 추가하거나, `tsconfig.json` 없이 Astro의 타입을 활용할 수 있습니다.

```ts title="src/env.d.ts"
// 사용자 정의 타입을 선언합니다.
declare var myString: string;

// Astro의 타입입니다. `tsconfig.json`이 이미 있는 경우에는 필요하지 않습니다.
/// <reference path="../.astro/types.d.ts" />
```

### `window` 및 `globalThis` 확장

전역 객체에 속성을 추가하려는 경우가 있을 수 있습니다. `env.d.ts` 파일에 `declare` 키워드를 사용하여 최상위 선언을 추가하면 됩니다.

Expand All @@ -155,14 +155,62 @@ declare function myFunction(): boolean;

`window` 객체의 속성에만 타입을 추가하려면 `Window` 인터페이스를 사용하세요.

`window` 객체의 속성에만 타입을 지정하려면 대신 `Window` 인터페이스를 제공하세요.

```ts title="src/env.d.ts"
interface Window {
myFunction(): boolean;
}
```

### 비표준 속성 추가하기

사용자 정의 속성이나 CSS 속성에 대한 타입을 정의하고 싶을 수도 있습니다. `.d.ts` 파일에서 `astroHTML.JSX` 네임스페이스를 재선언하여 기본 JSX 정의를 확장하면 비표준 속성을 추가할 수 있습니다.

```ts title="src/env.d.ts"
declare namespace astroHTML.JSX {
interface HTMLAttributes {
"data-count"?: number;
"data-label"?: string;
}

// 스타일 객체에 사용자 정의 CSS 속성을 추가합니다.
interface CSSProperties {
"--theme-color"?: "black" | "white";
}
}
```

:::note
`astroHTML`은 `.astro` 컴포넌트에 전역적으로 주입됩니다. TypeScript 파일에서 사용하려면 [삼중 슬래시 지시어](https://www.typescriptlang.org/docs/handbook/triple-slash-directives.html)를 사용하세요.

```ts
/// <reference types="astro/astro-jsx" />

type MyAttributes = astroHTML.JSX.ImgHTMLAttributes;
```
:::

### 가져오기 사용하기

[동적 가져오기](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import)를 사용하면 프로젝트 또는 외부 라이브러리에서 선언된 타입을 재사용하여 전역 타입을 확장할 수 있습니다.

```ts title="src/env.d.ts"
type Product = {
id: string;
name: string;
price: number;
};

declare namespace App {
interface Locals {
orders: Map<string, Product[]>
session: import("./lib/server/session").Session | null;
user: import("my-external-library").User;
}
}
```

`.d.ts`는 [앰비언트 모듈](https://www.typescriptlang.org/docs/handbook/modules/reference.html#ambient-modules) 선언 파일입니다. 구문은 ES 모듈과 유사하지만, 최상위 수준에서 가져오기/내보내기를 허용하지 않습니다. Typescript가 이를 감지하면 해당 파일을 [모듈 확장 파일](https://www.typescriptlang.org/docs/handbook/declaration-merging.html#module-augmentation)로 간주하여 전역 타입 정의가 깨질 수 있습니다.

## 컴포넌트 Props

Astro는 TypeScript를 통해 컴포넌트 props의 타입을 지원합니다. 활성화하려면 컴포넌트 프런트매터에 TypeScript `Props` 인터페이스를 추가하세요. `export` 구문을 사용할 수 있지만 필수는 아닙니다. [Astro VSCode 확장 프로그램](/ko/editor-setup/)은 `Props` 인터페이스를 자동으로 찾아서 다른 템플릿에서 해당 컴포넌트를 사용할 때 적절한 TS 지원을 제공합니다.
Expand Down Expand Up @@ -214,32 +262,6 @@ const { href, ...attrs } = Astro.props;
</a>
```

`.d.ts` 파일에서 `astroHTML.JSX` 네임스페이스를 다시 선언하여 기본 JSX 정의를 확장할 수 있습니다. 이를 통해 비표준 속성을 추가할 수도 있습니다.

```ts title="src/custom-attributes.d.ts"
declare namespace astroHTML.JSX {
interface HTMLAttributes {
"data-count"?: number;
"data-label"?: string;
}

// 스타일 객체에 CSS 사용자 정의 속성을 추가합니다.
interface CSSProperties {
"--theme-color"?: "black" | "white";
}
}
```

:::note
`astroHTML`은 `.astro` 컴포넌트에 전역적으로 주입됩니다. TypeScript 파일에서 사용하려면 [삼중 슬래시 지시어](https://www.typescriptlang.org/docs/handbook/triple-slash-directives.html)를 사용하세요.

```ts
/// <reference types="astro/astro-jsx" />

type MyAttributes = astroHTML.JSX.ImgHTMLAttributes;
```
:::

### `ComponentProps` 타입

<p><Since v="4.3.0" /></p>
Expand Down