【发布时间】:2020-05-08 22:11:07
【问题描述】:
我不明白为什么 TS 在我的项目中找不到该模块,但在另一个项目中却可以正常工作。 我的项目:https://github.com/JesusTheHun/react-typescript-boilerplate-big 我从中提取信息的项目是 https://github.com/piotrwitek/react-redux-typescript-realworld-app 。
在我的项目中ProjectTypes 在store/types.d.ts 中声明并在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';
【问题讨论】:
标签: typescript