# gt-node: General Translation Node.js SDK: declareVar
URL: https://generaltranslation.com/ja/docs/node/reference/functions/declare-var.mdx
Docs index: https://generaltranslation.com/llms.txt
Description: General Translation の derive 呼び出し内で、値を非導出としてマークします。declareVar の API リファレンス。

非導出の値とは、General Translation コンパイラが静的に解析できない値を指します。`declareVar` は [`derive`](/docs/node/reference/functions/derive) 呼び出し内で使用し、式のうち Runtime 変数として扱う必要がある部分を切り分けます。

## 概要 [#overview]

`declareVar` に値を渡して呼び出すと、その値を `other` 分岐に持つ、エンコード済みの ICU `select` 文字列が返されます。これにより、ラップした値は変数として扱いつつ、周囲のコンテンツはコンパイラが導出可能なまま保てます。

```ts
import { declareVar, derive, getGT } from 'gt-node';

const gt = await getGT();
const message = gt(`My name is ${derive(declareVar(getName()))}`);
```

シグネチャ:

```ts
declareVar(
  variable: string | number | boolean | null | undefined,
  options?: { $name?: string }
): string
```

## 仕組み [#how-it-works]

* **非導出マーカー。** `declareVar` は引数を ICU の `select` 式 (`{_gt_, select, other {...}}`) でラップするため、コンパイラはそれを静的な値ではなく変数として扱います。
* **名前は任意です。** エンコードされた出力内で変数にラベルを付けるには、`$name` を渡します。
* **復元。** `declareVar` の出力を含む文字列は、[`decodeVars`](/docs/node/reference/functions/decode-vars) を使って補間形式にデコードし直せます。

## パラメータ [#parameters]

| パラメータ                   | 説明         | 型                                               | 任意  | デフォルト |
| ----------------------- | ---------- | -------------------------------------------------- | --- | ----- |
| [`variable`](#variable) | 非導出として扱う値。 | `string \| number \| boolean \| null \| undefined` | いいえ | —     |
| [`options`](#options)   | 宣言用のオプション。 | `{ $name?: string }`                               | はい  | —     |

### `variable` [#variable]

**型** `string | number | boolean | null | undefined` · **必須**

非導出としてマークする値です。文字列に変換されたうえで、サニタイズされます。

### `options` [#options]

**型** `{ $name?: string }` · **任意**

* `$name?: string` — 変数の任意の名前です。エンコードされた出力に含まれます。

## 戻り値 [#returns]

**型** `string`

値 (および任意の名前) を保持する、エンコード済みの ICU `select` 文字列。

## 例 [#examples]

```ts
import { declareVar, derive, getGT } from 'gt-node';

function derivableName() {
  if (someCondition) {
    return declareVar(Math.random().toString());
  }
  return 'John Doe';
}

const gt = await getGT();
const greeting = gt(`My name is ${derive(derivableName())}`);
```

## メモ [#notes]

* `declareVar` は [`derive`](/docs/node/reference/functions/derive) の呼び出し内でのみ使用してください。
* エンコードされた変数は [`decodeVars`](/docs/node/reference/functions/decode-vars) を使ってテキストにデコードしてください。

## Sitemap

See the full [sitemap](https://generaltranslation.com/sitemap.md) for all pages.
