我不明白为什么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';发布于 2020-01-23 03:30:06
如用https://stackoverflow.com/a/52449433/1331578编写的。当您将单个导出移动到单个文件时,所有内容都应按预期工作。如果你想使用更复杂的类型,你必须更紧密地匹配原始的repo。所以在/services文件types.d.ts中,如果你把它们放在里面,你就会丢失
declare module 'ProjectTypes' {
export type Services = typeof import('./index').default;
}服务将从ProjectTypes开始工作。对于其他类型也可以这样做。
https://stackoverflow.com/questions/59864992
复制相似问题