---
title: "선언과 그 변환 결과"
description: "\"use typeshade\" 파일에서 상수와 변수, enum, 타입 별칭, 인터페이스, 네임스페이스가 각각 무엇이 되고 무엇이 생성되는지 싣습니다."
url: https://typeshade.dev/ko/guide/language/from-typescript/declarations/
language: ko
---

# 선언

선언은 상수나 변수나 레이아웃에 이름을 붙입니다. 셋 중 무엇이냐에 따라 생성되는 코드가 있는지 없는지가 갈립니다.

## 상수와 변수

- ### 스칼라 `const`
  모듈 상수입니다. 스칼라는 선언 자리에서 값 하나로 접힙니다. [module-const](https://github.com/typeshade/typeshade/blob/af54ddad5/examples/gallery/module-const/module-const.shade.ts)
  TypeScript
  ```ts
  const GAIN: f32 = 1.5 * 2.
  ```
  생성된 WGSL
  ```wgsl
  const GAIN: f32 = 3.0;
  ```
  생성된 GLSL ES 3.00
  ```glsl
  const float GAIN = 3.0;
  ```
- ### 벡터 `const`
  벡터나 배열 상수는 값을 식으로 지니고, 백엔드가 그 식을 평가합니다. [palette-const](https://github.com/typeshade/typeshade/blob/af54ddad5/examples/gallery/palette-const/palette-const.shade.ts)
  TypeScript
  ```ts
  const TINT: vec3 = vec3(0.2, 0.4, 0.9)
  ```
  생성된 WGSL
  ```wgsl
  const TINT: vec3<f32> = vec3<f32>(0.2, 0.4, 0.9);
  ```
  생성된 GLSL ES 3.00
  ```glsl
  const vec3 TINT = vec3(0.2, 0.4, 0.9);
  ```
- ### 지역 `let`
  변경 가능한 지역 변수입니다. 타입은 표기가 정하고, 모든 타깃이 영에서 시작합니다. [bitfield-bands](https://github.com/typeshade/typeshade/blob/af54ddad5/examples/gallery/bitfield-bands/bitfield-bands.shade.ts)
  TypeScript
  ```ts
  export function band(x: f32): f32 {
    let w: f32
    w = x * 2.
    return w
  }
  ```
  생성된 WGSL
  ```wgsl
  fn band(x: f32) -> f32 {
    let w = (x * 2.0);
    return w;
  }
  ```
  생성된 GLSL ES 3.00
  ```glsl
  float band(float x) {
    float w = (x * 2.0);
    return w;
  }
  ```
- ### 대입 전 읽기
  거절
  대입하지 않은 경로에서 지역 변수를 읽으면 [`TS8075`](https://typeshade.dev/ko/reference/errors/ts8075/)로 거부합니다. TypeScript가 TS2454로 보고하는 규칙과 같습니다. 편집기도 TS2454 대신 [`TS8075`](https://typeshade.dev/ko/reference/errors/ts8075/)를 보여 줍니다.
  TypeScript
  ```ts
  export function band(x: f32): f32 {
    let w: f32
    if (x > 0.) {
      w = x * 2.
    }
    return w
  }
  ```
  컴파일러 진단
  **[TS8075](https://typeshade.dev/ko/reference/errors/ts8075/)** "w" is read here before it is assigned on every path. Assign it before this read, or declare it with a value (Rule 7.6).
- ### 모듈 `let`
  모듈 변수입니다. 호출마다 하나씩 생깁니다. [private-state](https://github.com/typeshade/typeshade/blob/af54ddad5/examples/gallery/private-state/private-state.shade.ts)
  TypeScript
  ```ts
  let seed: u32 = 7
  ```
  생성된 WGSL
  ```wgsl
  var<private> seed: u32 = 7u;
  ```
  생성된 GLSL ES 3.00
  ```glsl
  uint seed = 7u;
  ```
- ### `var`
  거절
  호출별 변수는 `let`, 모듈 상수는 `const`입니다.
  TypeScript
  ```ts
  var k = 1.
  ```
  컴파일러 진단
  **[TS8014](https://typeshade.dev/ko/reference/errors/ts8014/)** Top-level var is not allowed. Use \`let\` for a per-invocation variable or \`const\` for a module constant.

## 타입과 네임스페이스

- ### `enum`
  멤버마다 모듈 상수가 하나씩 생깁니다. 이름은 `Enum_Member`, 타입은 `i32`입니다.
  TypeScript
  ```ts
  enum Mode {
    Flat,
    Shaded,
    Wire,
  }
  ```
  생성된 WGSL
  ```wgsl
  const Mode_Flat: i32 = 0;

  const Mode_Shaded: i32 = 1;

  const Mode_Wire: i32 = 2;
  ```
  생성된 GLSL ES 3.00
  ```glsl
  const int Mode_Flat = 0;

  const int Mode_Shaded = 1;

  const int Mode_Wire = 2;
  ```
- ### `const enum`
  같은 상수가 생깁니다. 여기서 `const enum`은 다르지 않습니다.
  TypeScript
  ```ts
  const enum Flag {
    None = 0,
    Both = 3,
  }
  ```
  생성된 WGSL
  ```wgsl
  const Flag_None: i32 = 0;

  const Flag_Both: i32 = 3;
  ```
  생성된 GLSL ES 3.00
  ```glsl
  const int Flag_None = 0;

  const int Flag_Both = 3;
  ```
- ### `type` 별칭
  대상을 가리키는 다른 이름입니다. 타입이 올 수 있는 자리마다 풀립니다.
  TypeScript
  ```ts
  type Meters = f32
  ```
  생성된 WGSL
  ```wgsl
  fn deeper(d: f32) -> f32 {
    return (d * 2.0);
  }
  ```
  생성된 GLSL ES 3.00
  ```glsl
  float deeper(float d) {
    return (d * 2.0);
  }
  ```
- ### `interface`
  구조체입니다. 같은 필드를 쓴 class와 결과가 같습니다.
  TypeScript
  ```ts
  interface Camera {
    view: mat4
    pos: vec3
  }
  ```
  생성된 WGSL
  ```wgsl
  struct Camera {
    view: mat4x4<f32>,
    pos: vec3<f32>,
  }
  ```
  생성된 GLSL ES 3.00
  ```glsl
  struct Camera {
    mat4 view;
    vec3 pos;
  };
  ```
- ### 구조체가 되는 클래스
  구조체입니다. 필드가 호스트의 메모리 레이아웃을 정합니다. [ray-class](https://github.com/typeshade/typeshade/blob/af54ddad5/examples/gallery/ray-class/ray-class.shade.ts)
  TypeScript
  ```ts
  class Ray {
    origin: vec3
    dir: vec3
  }
  ```
  생성된 WGSL
  ```wgsl
  struct Ray {
    origin: vec3<f32>,
    dir: vec3<f32>,
  }
  ```
  생성된 GLSL ES 3.00
  ```glsl
  struct Ray {
    vec3 origin;
    vec3 dir;
  };
  ```
- ### `namespace`
  멤버는 `Ns_member`로 펼쳐지고, namespace 자체는 아무것도 생성하지 않습니다.
  TypeScript
  ```ts
  namespace Palette {
    export const WARM: vec3 = vec3(0.9, 0.6, 0.3)
    export function tint(x: f32): vec3 {
      return WARM * x
    }
  }
  ```
  생성된 WGSL
  ```wgsl
  const Palette_WARM: vec3<f32> = vec3<f32>(0.9, 0.6, 0.3);

  fn Palette_tint(x: f32) -> vec3<f32> {
    return (Palette_WARM * x);
  }
  ```
  생성된 GLSL ES 3.00
  ```glsl
  const vec3 Palette_WARM = vec3(0.9, 0.6, 0.3);

  vec3 Palette_tint(float x) {
    return (Palette_WARM * x);
  }
  ```
