typescript - 未找到本地项目模块
问题描述
我不明白为什么 TS 在我的项目中找不到该模块,但在另一个项目中却可以正常工作。我的项目:https ://github.com/JesusTheHun/react-typescript-boilerplate-big 我从中提取信息的项目是https://github.com/piotrwitek/react-redux-typescript-realworld-app。
在我的项目ProjectTypes
中声明store/types.d.ts
并导入 in store/index.ts
,但 TS 没有找到它。在另一个项目中,它以相同的方式声明store/types.d.ts
并导入store/index.ts
. 它适用于一个人而不是我的。
我对两者都使用了相同版本的 TypeScript,并且在检查了几次之后,每个配置都是相同的。我一定错过了什么,我无法弄清楚它是什么。
我试过了--traceResolution
,它说它使用“缓存”来解析另一个项目中的模块,并且只在 node_modules 中搜索我的项目。
以下是一些示例:
// tsconfig.json
{
"include": ["src", "typings"],
"exclude": ["src/**/*.spec.*"],
"extends": "./node_modules/react-redux-typescript-scripts/tsconfig.json",
"compilerOptions": {
}
}
扩展的 tsconfig.json
// ./node_modules/react-redux-typescript-scripts/tsconfig.json
{
"compilerOptions": {
"baseUrl": "./", // relative paths base
// "paths": {
// "@src/*": ["src/*"], // will enable import aliases -> import { ... } from '@src/components'
// //WARNING: Require to add this to your webpack config -> resolve: { alias: { '@src': PATH_TO_SRC } }
// "redux": ["typings/redux"], // override library types with your alternative type-definitions in typings folder
// "redux-thunk": ["typings/redux-thunk"] // override library types with your alternative type-definitions in typings folder
// },
"outDir": "dist/", // target for compiled files
"allowSyntheticDefaultImports": true, // no errors with commonjs modules interop
"esModuleInterop": true, // enable to do "import React ..." instead of "import * as React ..."
"allowJs": true, // include js files
"checkJs": false, // typecheck js files
"declaration": false, // don't emit declarations
"emitDecoratorMetadata": true, // include only if using decorators
"experimentalDecorators": true, // include only if using decorators
"forceConsistentCasingInFileNames": true,
"importHelpers": true, // importing transpilation helpers from tslib
"noEmitHelpers": true, // disable inline transpilation helpers in each file
"jsx": "preserve", // preserving JSX
"lib": ["dom", "es2017"], // you will need to include polyfills for es2017 manually
"skipLibCheck": true,
"types": ["jest"], // which global types to use
"target": "es5", // "es2015" for ES6+ engines
"module": "esnext", // "es2015" for tree-shaking
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"noEmitOnError": false,
"noFallthroughCasesInSwitch": true,
"noUnusedLocals": true,
"strict": true,
"pretty": true,
"removeComments": true,
"sourceMap": true
}
}
模块声明
import { StateType, ActionType } from 'typesafe-actions';
declare module 'ProjectTypes' {
export type Store = StateType<typeof import('./index').default>;
export type RootAction = ActionType<typeof import('./actions').default>;
export type RootState = StateType<ReturnType<typeof import('./reducers').default>>;
}
declare module 'typesafe-actions' {
interface Types {
RootAction: ActionType<typeof import('./root-action').default>;
}
}
导入不起作用
import { RootAction, RootState, Services } from 'ProjectTypes';
解决方案
如https://stackoverflow.com/a/52449433/1331578中所述。当您将单个导出移动到单个文件时,一切都应该按预期工作。如果您想使用更复杂的类型,您必须更紧密地匹配原始存储库。所以如果你把它们放在 /services 文件 types.d.ts 中,你会错过
declare module 'ProjectTypes' {
export type Services = typeof import('./index').default;
}
服务将从 ProjectTypes 开始工作。其他类型也可以这样做。
推荐阅读
- sqlite - SQLite 请求特定日期之间的日历事件
- xamarin - Xamarin Android DialogFragment 中的 OnTimedEvent() 如何 Dissmis()?
- hook - 在模式挂钩中向用户显示消息
- go - 构建约束排除所有 Go 文件 go build for amd64 系统
- python - 如何快速下载大量小文件?(不受带宽限制)
- javascript - 如何在打字稿中仅使用一种状态在 redux 存储中设置 preloadedState?
- objective-c - NSEventMaskCursorUpdate 与 addGlobalMonitorForEventsMatchingMask 未触发
- postgresql - 如何在 Supabase 中使用单个 API 调用插入多个表
- python - 如何在 Statsmodels 或 Linearmodels 中测试 2SLS 的系数相等性?
- scala - Docker 运行没有抛出这样的文件或目录:未知