无法从我的打字稿库导入打字稿界面

时间:2020-09-21 14:43:38

标签: reactjs typescript npm package components

我目前正在尝试将我的React Typescript子模块转换为可以使用npm导入到我们项目中的库。我已成功使用您的标准import {Component} from 'myLib'导入组件。这可以按预期工作。

很遗憾,我无法导入接口,而必须提供完整路径,否则会出现错误:Cannot use namespace 'IMyInterface' as a type

通过完整的路径,我的意思是这可行:import {Component} from 'myLib/build/path/to/my/interface'

我正在从src/index.ts导入和导出所有组件和接口,它们被指定为汇总配置文件中的输入。

我的配置基于其他几个教程/指南/文档。我不明白允许我仅使用软件包名称(即import {IMyInterface} from 'myLib')导入外部接口的原因。任何帮助将不胜感激!

作为一个旁注,我将要导入的项目也是TS项目,并且在我消耗Lib的项目的clarification.d.ts文件中,我在该文件中设置了declare module "myLib"

这是我的配置-

我正在使用汇总来编译项目。这是汇总配置:

import peerDepsExternal from "rollup-plugin-peer-deps-external";
import resolve from "@rollup/plugin-node-resolve";
import commonjs from "@rollup/plugin-commonjs";
import typescript from "rollup-plugin-typescript2";
import postcss from "rollup-plugin-postcss";


const packageJson = require("./package.json");

export default {
    input: "src/index.ts",
    output: [
        {
            file: packageJson.main,
            format: "cjs",
            sourcemap: true
        },
        {
            file: packageJson.module,
            format: "esm",
            sourcemap: true
        }
    ],
    plugins: [
        peerDepsExternal(),
        resolve(),
        commonjs(),
        typescript({ useTsconfigDeclarationDir: true }),
        postcss({
            modules: true,
        })
    ]
};

我的tsconfig文件(在Lib中):

{
    "compilerOptions": {
        "rootDir": "src",
        "declaration": true,
        "declarationDir": "build",
        "module": "esnext",
        "target": "es5",
        "lib": [
            "es6",
            "dom",
            "es2016",
            "es2017"
        ],
        "sourceMap": true,
        "jsx": "react",
        "moduleResolution": "node",
        "allowSyntheticDefaultImports": true,
        "esModuleInterop": true
    },
    "include": [
        "src/**/*"
    ],
    "exclude": [
        "node_modules",
        "build",
        "src/**/*.stories.tsx",
        "src/**/*.test.tsx"
    ]
}

最后,这是我的package.json文件(在我的Lib中)的重要内容:

  "main": "build/index.js",
  "module": "build/index.esm.js",
  "files": [
    "build"
  ],
  "types": "build/index.d.ts",

0 个答案:

没有答案