Create React App TypeScript 绝对路径失效问题求助
解决Create React App TypeScript模板绝对路径导入类型报错的问题
1. 确保types目录存在入口导出文件
直接使用import type { Maybe, Nullish } from 'types'要求src/types目录下有index.ts文件,且该文件导出目标类型。示例:
// src/types/index.ts export type Maybe<T> = T | null; export type Nullish = null | undefined; // 若类型定义在其他子文件,可通过如下方式导出 // export type { Maybe, Nullish } from './your-type-file';
2. 校验tsconfig.json配置的正确性
确认修改的是项目根目录下的tsconfig.json(而非tsconfig.node.json),且baseUrl配置在compilerOptions内部:
{ "compilerOptions": { "baseUrl": "src", // 其他原有配置... }, "include": ["src"] }
3. 重启编辑器的TypeScript服务
编辑器(如VS Code)可能缓存旧配置,按下Ctrl+Shift+P(Windows/Linux)或Cmd+Shift+P(Mac),输入TypeScript: Restart TS Server并执行,刷新配置生效。
4. 额外映射配置(若上述步骤无效)
如果问题仍存在,可在compilerOptions中添加paths字段明确模块映射:
{ "compilerOptions": { "baseUrl": "src", "paths": { "types": ["types/index"] }, // 其他原有配置... } }
内容的提问来源于stack exchange,提问作者user2141352
相关产品推荐
相关产品推荐

