Skip to content
🔷

TypeScript 基础错误

50 个错误

TypeScript 常见错误排查手册:类型不兼容、隐式 any、泛型约束等编译错误的解决思路。

50 / 50

  1. 1

    类型 '字符串' 是不 assignable 到类型 '数字'

    你 tried 到 assign 值的 one 类型到 变量 or 参数的 不同, incompatible 类型. TypeScript caught mismatch at 编译时间.

  2. 2

    属性 'X' 做不 exist on 类型 'Y'

    你 tried 到 access 属性那个 TypeScript doesn't know 关于 on given 类型. 属性是 不 declared in 接口 or 类型.

  3. 3

    Cannot 查找 name 'X'

    你 referenced name (变量, 函数, or 类型) 那个 TypeScript cannot 查找 in current 作用域 or 导入.

  4. 4

    Expected 0 实参, 但获取 1

    你 called 函数使用更多 or fewer 实参 than 它的 signature declares. TypeScript 检查实参数量 at 编译时间.

  5. 5

    对象是 可能地 '空' or 'undefined'

    使用 strictNullChecks 启用, TypeScript prevents 你从 accessing 属性 on 值那个能 是空 or undefined 不使用检查首先.

  6. 6

    属性 'X' 是 missing in 类型 'Y'

    你 assigned 对象到 输入变量, 但对象是 missing one or 更多必需属性 declared in 接口.

  7. 7

    实参的 类型 'X' 是不 assignable 到参数的 类型 'Y'

    你通过值 到函数, 但值's 类型 doesn't match 函数's 参数类型.

  8. 8

    'X' 仅 refers 到类型, 但是 是 used as 值这里

    你 used 类型 or 接口 name in 值位置 (e.g., 使用 `新` or as 变量), 但类型是 erased at 编译时间 and 有否 运行时 existence.

  9. 9

    否 overload matches 这个 call

    你 called 函数使用实参那个 don't match 任何的 它的 declared overload signatures. TypeScript 检查每个 overload in 顺序.

  10. 10

    变量 'X' implicitly 有 '任何' 类型

    使用 noImplicitAny 启用, TypeScript 错误当 变量 or 参数有 否类型 annotation and 它的类型 cannot 是 inferred, defaulting 到任何.

  11. 11

    Cannot assign 到 'X' 因为它 是读取-仅属性

    你 tried 到 assign 新值 到属性 marked as readonly, or 到 Readonly<T> 对象. TypeScript prevents mutations 的 readonly 属性 at 编译时间.

  12. 12

    Generic 类型需要 1 类型实参(s)

    你 used generic 类型 or 函数不使用 providing 必需类型参数(s). TypeScript 需要 explicit 类型实参当 它们 cannot 是 inferred.

  13. 13

    类型 'X' 是不 comparable 到类型 'Y'

    你 compared two 值谁的类型有 否 overlap — comparison 将总是是 假 or 真. TypeScript flags 这个 as likely logic 错误.

  14. 14

    Duplicate identifier 'X'

    你 declared two 变量, 函数, 类型, or 接口使用相同 name in 相同作用域. TypeScript 做不 允许 duplicate identifiers (except for 接口合并).

  15. 15

    '等待' 是仅 允许 in 异步函数

    你 used `等待` keyword 之外的 异步函数 or at 顶部 level 不使用顶部-level 等待 support. 等待需要异步上下文.

  16. 16

    模块有 否 exported 成员 'X'

    你 tried 到导入 name 从模块, 但模块 doesn't 导出任何事使用那个 name. 导入 doesn't match 任何导出.

  17. 17

    类型 'X' 有否 属性 in 常见使用类型 'Y'

    你 assigned or compared two 对象类型那个有 否属性 in 常见. 这个是 通常 sign 的 using 错误类型.

  18. 18

    Unable 到 resolve signature 的目标 constructor

    TypeScript couldn't determine 哪个 overload or signature 到 use for 函数 or constructor call, 经常 due 到复杂 generic 类型.

  19. 19

    Excessive stack 深度 comparing 类型

    TypeScript's 类型 checker exceeded 它的 recursion 限制虽然 comparing two 类型. 这个通常 happens 使用 deeply recursive or self-referential 类型.

  20. 20

    '这个' implicitly 有类型 '任何'

    使用 noImplicitThis 启用, TypeScript 错误当 `这个` 是 used in 函数不使用 determined 类型. 这个通常 happens in standalone 函数.

  21. 21

    TS2783: 'X' 是 specified 更多 than 一次

    你有 属性 in 两者都 spread 对象 and explicit assignment. explicit 值 wins, 哪个可能不 是 intended.

  22. 22

    TS4114: 这个成员必须有 'override' modifier

    你是 overriding 基础类 方法但 forgot 'override' keyword. 这个是 必需当 noImplicitOverride 是启用.

  23. 23

    TS4116: 类型是 不包含 in union

    你的切换情况值 doesn't match 任何 union 成员's discriminant 属性值.

  24. 24

    TS4124: 这个成员 cannot 有 'override' modifier

    你 marked 方法使用 'override' 但基础类 doesn't 有方法使用那个 name.

  25. 25

    TS4125: 'override' modifier cannot 是 used 使用 'declare'

    你 cannot use 两者都 'override' and 'declare' on 相同类 成员.

  26. 26

    TS4138: REST 元素必须是 最后 in tuple 类型

    In tuple 类型, REST 元素 (...) 能仅 appear as 最后元素.

  27. 27

    TS4144: Indexed 类型必须有 类型 annotation

    那里's issue 使用 computed 属性 or mapped 类型键. 键类型需要到 是 properly constrained.

  28. 28

    TS4222: 否 overload expects N 实参

    你're calling 函数使用数字的 实参那个 doesn't match 任何 overload signature.

  29. 29

    TS4232: 这个表达式是 不 callable

    你're trying 到 invoke 某事 as 函数, 但 TypeScript sees 它 as 非-函数类型.

  30. 30

    TS4242: Enum 成员必须有 initializer

    enum 成员需要值 但 doesn't 有 one, and TypeScript 能't infer 它.

  31. 31

    错误的类型断言隐藏运行时错误

    你用 'as Type' 覆盖了推断类型 —— TypeScript 停止检查,错误类型导致运行时错误。

  32. 32

    枚举反向映射的意外行为

    你迭代数字枚举时同时得到了键和值 —— TypeScript 为数字枚举创建反向映射。

  33. 33

    泛型类型推断产生意外类型

    你的泛型函数的类型参数被推断为比预期更宽的类型 —— 结果类型过于宽松。

  34. 34

    装饰器元数据未发射

    你使用依赖元数据反射的装饰器,但编译器选项未启用 —— 运行时元数据缺失。

  35. 35

    Symbol 唯一类型冲突

    你比较了两个有相同描述的 Symbol 但它们是不同的唯一 symbol —— Symbol() 始终创建唯一身份。

  36. 36

    Mixin 类型在运行时冲突

    两个 mixin 定义同名方法 —— 最后应用的 mixin 胜出,先前的方法静默丢失。

  37. 37

    条件类型分布产生意外结果

    你的条件类型对联合类型是分布式的 —— 它对每个成员分别求值,产生结果的联合。

  38. 38

    strictNullChecks:值可能为 null

    你的变量类型为 T | null 但你将其当作 T 使用 —— TypeScript 在严格空值检查下阻止此操作。

  39. 39

    noImplicitAny:参数隐式为 any

    你的函数参数缺少类型注解 —— TypeScript 推断 'any' 并在启用 noImplicitAny 时报告错误。

  40. 40

    strictFunctionTypes:函数签名不兼容

    你将函数赋值给期望不同参数变型的类型 —— strictFunctionTypes 捕获不匹配。

  41. 41

    动态属性访问缺少索引签名

    你用了 obj[dynamicKey] 但 obj 的类型没有索引签名 —— TypeScript 无法保证键存在。

  42. 42

    可辨识联合穷尽性检查失败

    你的 switch 语句缺少某个联合成员的情况 —— 通过 'default: assertNever' 的穷尽性检查捕获它。

  43. 43

    泛型约束违反:类型不满足 extends

    你传递了不满足泛型参数 'extends' 约束的类型 —— TypeScript 拒绝它。

  44. 44

    结构类型允许意外赋值

    带额外属性的对象被赋值给属性更少的类型 —— 结构类型允许它,但可能隐藏 bug。

  45. 45

    额外属性检查仅对对象字面量

    你将带额外属性的变量赋值给类型 —— TypeScript 不检查变量的额外属性,只检查字面量。

  46. 46

    赋值后类型收窄丢失

    你在 if 块中收窄了类型,但赋值给变量后收窄丢失 —— 变量具有宽类型。

  47. 47

    const 断言与 let:可变与只读

    你用 'let x = { a: 1 } as const' 期望不可变性,但 'let' 允许重新赋值 —— 只有 'const' 能防止它。

  48. 48

    Promise 类型解包混淆

    你的异步函数返回 Promise<T> 但 TypeScript 推断 Promise<Promise<T>> —— async 已将返回包装在 Promise 中。

  49. 49

    只读违反:修改只读属性

    你尝试向只读数组 push 或给只读属性赋值 —— TypeScript 阻止修改只读类型。

  50. 50

    品牌类型混淆:与幻影属性的交集

    你使用了品牌类型(string & { __brand: 'X' })但未正确构造品牌 —— 幻影属性在运行时不存在。

← 返回错误