代码之家  ›  专栏  ›  技术社区  ›  Blü

模块导入忽略package.json中的主字段

  •  0
  • Blü  · 技术社区  · 7 年前

    我有一个导入模块的文件(在本例中 react-intercom ),此模块中有一个package.json文件,其中一个主字段指向 lib/index.js

    但是,导入时使用

    import { IntercomAPI } from 'react-intercom'

    typescript将忽略主字段并查找 react-intercom/index 相反

    本例中的应用程序是一个带有嵌入式react实例的角度应用程序

    这是我们的tsconfig文件

    {
      "compileOnSave": false,
      "compilerOptions": {
        "outDir": "./dist/out-tsc",
        "sourceMap": true,
        "declaration": false,
        "moduleResolution": "node",
        "baseUrl": "./",
        "paths": {
          "@/*": ["src/app/*"],
          "#/*": ["src/*"]
        },
        "module": "commonjs",
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "target": "es6",
        "typeRoots": ["node_modules/@types", "./src/typings.d.ts"],
        "lib": ["es2018", "dom"],
        "allowJs": true,
      },
      "exclude": ["test.ts", "**/*.spec.ts", "node_modules", "build", "dist"],
      "include": ["./src/*.ts", "./src/**/*.ts"],
      "files": ["node_modules/ngx-i18next/index.ts"]
    }
    

    以及我们的tsconfig.app文件

    {
      "extends": "../tsconfig.json",
      "compilerOptions": {
        "outDir": "../out-tsc/<appname>",
        "baseUrl": "./",
        "paths": {
          "@/*": ["app/*"],
          "#/*": ["./*"]
        },
        "module": "es2015",
        "types": [],
        "typeRoots": ["./typings.d.ts", "node_modules/@types"],
        "experimentalDecorators": true,
        "jsx": "react"
      },
      "exclude": [
        "test.ts",
        "**/*.spec.ts",
        "../node_modules",
        "../build",
        "../dist"
      ],
      "include": ["./*.ts", "./**/*.ts"],
      "files": ["../node_modules/ngx-i18next/index.ts"]
    }
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   Blü    7 年前

    最终找出问题所在,是模块而不是我们的配置

    结果发现 one of the last commit added an index.d.ts file at the base of the module ,并且看起来typescript在查看 main package.json中的条目

    所以它从寻找react interco/lib/index.js变成了react interco/index.d.ts,这是一个文件 missing the typings for IntercomAPI

    推荐文章