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

TypeScript中前缀问号类型标注的官方名称是什么?

关于?string类型标注的官方名称说明
  • 这个语法不是标准TypeScript/JavaScript的特性,它是Flow(Facebook推出的静态类型检查工具)里的「可选空类型简写(Nullable Type Shorthand)」,用来快速表示string | null类型。
  • 你遇到的Babel不识别、ts-loader能识别的差异,是因为ts-loader可能配置了兼容Flow语法的处理逻辑,而你的Babel没有添加对应处理Flow语法的预设(比如@babel/preset-flow)。
  • 标准TypeScript里没有这种简写语法,要表达string | null必须完整写出联合类型;如果是要表示参数可选(对应string | undefined),则是在参数名后加?,比如function test(a?: string)。

代码对比示例:

// Flow语法
function test(a: ?string) {
  return undefined
}

// 等价的TypeScript写法(开启strictNullChecks时)
function test(a: string | null) {
  return undefined
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.21 16:18:19