# gt-node: General Translation Node.js SDK: declareVar URL: https://generaltranslation.com/zh/docs/node/reference/functions/declare-var.mdx --- title: declareVar description: 在 General Translation 的 derive 调用中将某个值标记为不可派生。declareVar 的 API 参考。 --- 将某个值标记为不可派生——即无法被 General Translation 编译器 静态分析的内容。在 [`derive`](/docs/node/reference/functions/derive) 调用中使用 `declareVar`,以便隔离表达式中必须视为 Runtime 变量的部分。 ## 概览 [#overview] 调用 `declareVar` 并传入一个值后,会返回一个经过编码的 ICU `select` 字符串,并将该值放在其 `other` 分支中。这样,编译器就能保持周围内容可推导,同时把包裹的值当作变量处理。 ```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` 可在编码后的输出中为该变量添加标签。 * **恢复。** 可使用 [`decodeVars`](/docs/node/reference/functions/decode-vars) 将包含 `declareVar` 输出的 string 解码回插值形式。 ## 参数 [#parameters] | 参数 | 描述 | Type | 可选 | 默认值 | | ----------------------- | ----------- | -------------------------------------------------- | -- | --- | | [`variable`](#variable) | 要标记为不可派生的值。 | `string \| number \| boolean \| null \| undefined` | 否 | — | | [`options`](#options) | 声明选项。 | `{ $name?: string }` | 是 | — | ### `variable` [#variable] **Type** `string | number | boolean | null | undefined` · **必填** 要标记为不可派生的值。它会被强制转换为字符串,并进行清理。 ### `options` [#options] **Type** `{ $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] * 仅可在 [`derive`](/docs/node/reference/functions/derive) 调用内部使用 `declareVar`。 * 使用 [`decodeVars`](/docs/node/reference/functions/decode-vars) 将已编码的变量还原为文本。