为何别名导入会导致TypeScript错误报告失效?
TypeScript别名导入后失去错误检测能力
问题现象
- 使用别名导入可复用类型时,TypeScript完全停止错误报告:
import { PolymorphicComponentProps } from "@interfaces/polymorphic"; - 切换为相对路径导入后,TypeScript恢复正常错误检查:
import { PolymorphicComponentProps } from "../../interfaces/polymorphic";
核心问题
相对路径导入能正常触发TypeScript的错误检测,别名导入则完全失效,无法发现代码中的类型错误。
已尝试的排查步骤
- 查阅大量TypeScript别名相关资料
- 对比create-react-app的默认tsconfig配置,调整自身项目的配置参数
当前项目tsconfig.json配置
{ "compilerOptions": { "allowJs": true, "allowSyntheticDefaultImports": true, "baseUrl": "src", "declaration": true, "declarationDir": "dist", "esModuleInterop": true, "forceConsistentCasingInFileNames": true, "isolatedModules": true, "jsx": "react-jsx", "lib": ["dom", "dom.iterable", "esnext"], "noFallthroughCasesInSwitch": true, "module": "esnext", "moduleResolution": "node", "noEmit": false, "outDir": "dist", "resolveJsonModule": true, "sourceMap": true, "skipLibCheck": true, "strict": true, "target": "es5", "paths": { "@components/*": ["components/*"], "@core/*": ["core/*"], "@constants/*": ["constants/*"], "@hooks/*": ["hooks/*"], "@interfaces/*": ["interfaces/*"], "@utils/*": ["utils/*"], "@styles/*": ["styles/*"] } }, "include": ["example", "src", "__tests__"], "exclude": ["dist", "node_modules", "src/stories", "webpack", ".vscode"] }
内容的提问来源于stack exchange,提问作者Aaron Balthaser
相关产品推荐
相关产品推荐

