【问题标题】:Local project module not found未找到本地项目模块
【发布时间】:2020-05-08 22:11:07
【问题描述】:

我不明白为什么 TS 在我的项目中找不到该模块,但在另一个项目中却可以正常工作。 我的项目:https://github.com/JesusTheHun/react-typescript-boilerplate-big 我从中提取信息的项目是 https://github.com/piotrwitek/react-redux-typescript-realworld-app

在我的项目中ProjectTypesstore/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


    【解决方案1】:

    https://stackoverflow.com/a/52449433/1331578 中所写。当您将单个导出移动到单个文件时,一切都应该按预期工作。 如果您想使用更复杂的类型,您必须更紧密地匹配原始存储库。 所以你在 /services 文件 types.d.ts 中丢失了 如果你把它们放在里面

    declare module 'ProjectTypes' {
    export type Services = typeof import('./index').default;
    }
    

    服务将从 ProjectTypes 开始工作。其他类型也可以这样做。

    【讨论】:

    • 如果是这样,为什么在我提到的项目中工作?
    • 我更新了我的答案。所以基本上你需要像在原始项目中一样将 types.d.ts 添加到 /services 中,以使其像原始项目一样工作。这使它可以在我的你的回购克隆中工作。
    • 你知道原因吗?
    • 我猜 typeof import('./actions').default 导入正确,但是 ActionType 没有正确导入。只有奇怪的东西没有被标记为无效,所以它可能有效但不可用。
    • 这看起来像是 typescript 和 javascript 的奇怪组合。如果它在下一次正确构建一次它将起作用。所以唯一的问题是第一次正确构建它。所以在类型中首先要写'RootAction:RootAction;'那么它可以被替换为 ActionType<... .>
    猜你喜欢
    • 2019-01-20
    • 1970-01-01
    • 2022-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-28
    • 1970-01-01
    相关资源
    最近更新 更多