📦类型不匹配(TypeScript)
Type error: Type 'string' is not assignable to type 'number'.
这是 TypeScript 在提醒你:“这里应该放这种类型的值,但放进来的是别的。”它能在运行之前帮你抓住错误,是个很贴心的报错。
🤔 为什么会出现?
- 值的类型不对,比如在该放数字的地方放了文字
- 改了数据结构(类型定义),但用到它的地方没跟着改
- 用了不存在的属性名
🔧 试试这样做
- 根据报错里的文件和行号,看看放进去的是什么值
- 用 as any 盖过去只会把问题藏起来,尽量避免
🤖 这样问 AI
构建时出现了下面的 TypeScript 报错。请不要用 any 糊弄过去,先解释类型为什么不匹配,再正确地修好。 [在这里粘贴完整的报错]
把 [ ] 部分换成你的实际情况,报错前后的几行也尽量一起复制粘贴。
相关术语
同一分类的其他报错
- 找不到模块
Module not found: Can't resolve - 找不到命令
command not found - 包版本冲突(ERESOLVE)
npm ERR! ERESOLVE unable to resolve dependency tree - 端口已被占用
EADDRINUSE: address already in use - 没有权限,安装不了(EACCES)
EACCES: permission denied - Node 版本不匹配
The engine "node" is incompatible with this module - 内存不足(heap out of memory)
JavaScript heap out of memory - 不能使用 import 语句(模块设置)
SyntaxError: Cannot use import statement outside a module - 代码检查(ESLint)没通过,构建失败
Failed to compile — ESLint errors - 找不到文件或文件夹(ENOENT)
ENOENT: no such file or directory