使用Map.groupBy遇TypeScript报错:MapConstructor无groupBy属性
解决TypeScript中
Map.groupBy不存在的类型错误 问题原因
Map.groupBy是ES2024新增的静态方法,你的TypeScript 5.4.5和Node.js 22.0.0版本本身已支持该特性,但当前tsconfig.json未明确指定包含ES2024标准库定义,导致TypeScript无法识别该方法的类型。
解决方案
修改tsconfig.json的compilerOptions,添加lib配置项引入ES2024标准库:
{ "compilerOptions": { "target": "ESNext", "lib": ["ES2024", "DOM"], // 前端项目保留DOM,后端项目可移除DOM "useDefineForClassFields": true, "module": "ESNext", "skipLibCheck": true, /* Bundler mode */ "moduleResolution": "bundler", "allowImportingTsExtensions": true, "resolveJsonModule": true, "isolatedModules": true, "noEmit": true, "jsx": "react-jsx", "jsxImportSource": "@emotion/react", /* Linting */ "strict": true, "noUnusedLocals": true, "noUnusedParameters": true, "noFallthroughCasesInSwitch": true, "types": ["vite-plugin-arraybuffer/types"] }, "include": ["src"], "references": [{ "path": "./tsconfig.node.json" }] }
补充说明
- 若为Node.js后端项目,
lib可仅设置为["ES2024"] - 配置中的
skipLibCheck不影响本次修复效果 - 保存配置后重启编辑器或TypeScript语言服务,类型错误即可消失
内容的提问来源于stack exchange,提问作者KVM
相关产品推荐
相关产品推荐

