'X' 仅 refers 到类型, 但是 是 used as 值这里
错误信息
仅 refers 到类型, 但是 是 used as 值这里
快速摘要
你 used 类型 or 接口 name in 值位置 (e.g., 使用 `新` or as 变量), 但类型是 erased at 编译时间 and 有否 运行时 existence.
为什么发生
Using
新使用接口 or 类型 aliasUsing 类型 name in 运行时表达式
Confusing 类型使用类
Trying 到 use
instanceof使用接口
最小示例
✗ 错误代码
interface Person { name: string; }
const p = new Person(); // Person is a type, not a constructor✓ 修复代码
class Person { constructor(public name: string) {} }
const p = new Person("Alice");Use 类相反的 接口当 你需要 constructor.
如何诊断
检查如果 name 是 declared as
接口or类型(不类)Determine 如果你 需要运行时值 or 只是类型
Look for
新,instanceof, or other 值-位置 usages
如何修复
如果你 需要 constructor, use
类相反的接口If you need an object, use an object literal:
const x: MyType = { ... }如果你 需要 instanceof, use 类 or 自定义类型 guard
如果你 需要运行时检查, 编写类型 guard 函数
如何预防
理解那个类型是 编译-时间仅 (类型 erasure)
Use 类当 你需要两者都类型 and 运行时 behavior