You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

TypeScript中as const数组经操作后能否保留精确值类型推断?

实现精确类型推断的数字转字符串数组函数

先看问题里的示例数组:

const myArray = ["foo", 3.14, true] as const;
// TypeScript 自动推断类型:readonly ["foo", 3.14, true]

完全可以实现满足要求的泛型函数,直接上代码:

// 定义类型转换逻辑:遍历数组元素,把数字转为对应字符串字面量类型
type ConvertNumbersToStrings<T> = T extends readonly any[]
  ? { [K in keyof T]: T[K] extends number ? `${T[K]}` : T[K] }
  : never;

// 实现转换函数
function convertNumbersToStrings<T extends readonly any[]>(arr: T): ConvertNumbersToStrings<T> {
  return arr.map(item => 
    typeof item === 'number' ? item.toString() : item
  ) as ConvertNumbersToStrings<T>;
}

测试一下使用效果:

const result = convertNumbersToStrings(myArray);
// TypeScript 精确推断类型:readonly ["foo", "3.14", true]
console.log(result); // 实际输出:["foo", "3.14", true]

关键逻辑说明

  1. 类型层面的转换:
    使用条件类型ConvertNumbersToStrings<T>,通过映射类型遍历原数组的每个索引位。如果当前元素是number类型,就用TypeScript的模板字符串类型${T[K]}把它转成对应的字符串字面量(比如3.14变成"3.14"),非数字元素则保留原类型。

  2. 函数实现与类型断言:
    函数内部用map遍历数组,判断元素类型后做值转换。由于Array.prototype.map的默认返回类型是元素类型的联合数组,无法自动对齐我们定义的精确元组类型,所以需要用as ConvertNumbersToStrings<T>做类型断言,让TypeScript识别出精确的返回类型。

这个方案对各种readonly元组都生效,比如传入[42, "hello", null] as const,会自动推断返回类型为readonly ["42", "hello", null]。

内容的提问来源于stack exchange,提问作者Nick Manning

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 06:10:04