我正在try 创建一个使用Storybook,ViteJS,Reaction和Print的设计系统.我已经创建了整个系统并构建了它,但是当我try 将包链接到一个项目时,我收到了以下错误消息:

TS7016: Could not find a declaration file for module 'mypackage'. 'C:/filepath/mypackage.es.js' implicitly has an 'any' type.   There are types at 'C:/filepath/node_modules/mypackage/dist/index.d.ts', but this result could not be resolved when respecting package.json "exports". The 'mypackage' library may need to update its package.json or typings.

我的Package.json文件如下所示:

{
    "name": "mypackage",
    "private": true,
    "version": "0.8.0",
    "engines": {
        "node": "20"
    },
    "main": "dist/mypackage.cjs",
    "type": "module",
    "types": "dist/index.d.ts",
    "scripts": {
        "build": "tsc && vite build",
        "lint": "eslint src --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
        "format": "prettier \"src/**/*.+(ts|tsx|js|jsx)\" --write",
        "storybook": "storybook dev -p 6006",
        "build-storybook": "storybook build",
        "test": "vitest"
    },
    "peerDependencies": {
        "react": "^18.2.0",
        "react-dom": "^18.2.0"
    },
    "devDependencies": {
        "@storybook/addon-essentials": "^7.0.23",
        "@storybook/addon-interactions": "^7.0.23",
        "@storybook/addon-links": "^7.0.23",
        "@storybook/blocks": "^7.0.23",
        "@storybook/react": "^7.0.23",
        "@storybook/react-vite": "^7.0.23",
        "@storybook/testing-library": "^0.2.0",
        "@testing-library/jest-dom": "^5.16.5",
        "@testing-library/react": "^14.0.0",
        "@types/bytes": "^3.1.1",
        "@types/react": "^18.2.13",
        "@types/react-dom": "^18.2.6",
        "@types/react-helmet": "^6.1.6",
        "@typescript-eslint/eslint-plugin": "^5.60.0",
        "@typescript-eslint/parser": "^5.60.0",
        "@vitejs/plugin-react": "^4.0.1",
        "axios": "^1.4.0",
        "eslint": "^8.43.0",
        "eslint-config-prettier": "^8.8.0",
        "eslint-plugin-prettier": "^4.2.1",
        "eslint-plugin-react": "^7.32.2",
        "eslint-plugin-react-hooks": "^4.6.0",
        "eslint-plugin-react-refresh": "^0.4.1",
        "eslint-plugin-storybook": "^0.6.12",
        "i18next": "^23.2.3",
        "jsdom": "^22.1.0",
        "prettier": "^2.8.8",
        "prop-types": "^15.8.1",
        "redux-saga": "^1.2.3",
        "sass": "^1.63.6",
        "storybook": "^7.0.23",
        "typescript": "^5.0.2",
        "vite": "^4.3.9",
        "vitest": "^0.32.2"
    },
    "exports": {
        ".": {
            "require": "./dist/mypackage.cjs",
            "import": "./dist/mypackage.es.js"
        }
    },
    "files": [
        "dist"
    ],
    "dependencies": {
        "@emotion/react": "^11.11.1",
        "@emotion/styled": "^11.11.0",
        "@mui/lab": "^5.0.0-alpha.134",
        "@mui/material": "^5.13.5",
        "@mui/x-date-pickers": "^6.9.0",
        "@storybook/addon-knobs": "^7.0.2",
        "bytes": "^3.1.2",
        "history": "^5.3.0",
        "i18next-http-backend": "^2.2.1",
        "moment": "^2.29.4",
        "object-path-immutable": "^4.1.2",
        "path": "^0.12.7",
        "react-helmet": "^6.1.0",
        "react-idle-timer": "^5.7.2",
        "typed-redux-saga": "^1.5.0",
        "vite-plugin-dts": "^2.3.0",
        "vite-plugin-svgr": "^3.2.0",
        "vite-tsconfig-paths": "^4.2.0",
        "yup": "^1.2.0"
    }
}

我的VITE配置文件如下所示:

import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import tsconfigPaths from 'vite-tsconfig-paths';
import dts from 'vite-plugin-dts';
import { resolve } from 'path';
import * as packageJson from './package.json';
import svgr from 'vite-plugin-svgr';

export default defineConfig(() => ({
  plugins: [
      react(),
      tsconfigPaths(),
      dts({
        include: ['src'],
      }),
      svgr(),
  ],
    build: {
      lib: {
          entry: resolve('src', 'index.ts'),
          name: 'ds-can',
          formats: ['es', 'cjs'],
          fileName: (format) => `mypackage.${format === 'cjs' ? 'cjs' : 'es.js'}`,
      },
        optimizeDeps: {
          exclude: Object.keys(packageJson.peerDependencies),
        },
        esbuild: {
          minify: true,
        },
        rollupOptions: {
          external: [...Object.keys(packageJson.peerDependencies)],
        }
    },
    test: {
      environment: 'jsdom',
        globals: true,
        setupFiles: './setupTests.ts'
    }
}));

最后,我的tsfig.json如下所示:

{
    "compilerOptions": {
        "target": "ESNext",
        "useDefineForClassFields": true,
        "lib": ["DOM", "DOM.Iterable", "ESNext"],
        "allowJs": false,
        "allowSyntheticDefaultImports": true,
        "strict": true,
        "forceConsistentCasingInFileNames": true,
        "module": "ESNext",
        "moduleResolution": "Node",
        "resolveJsonModule": true,
        "isolatedModules": true,
        "noEmit": true,
        "jsx": "react-jsx",
        "declaration": true,
        "skipLibCheck": true,
        "esModuleInterop": true,
        "declarationMap": true,
        "baseUrl": ".",
        "paths": {
            "mypackage": ["src/index.ts"],
        },
        "typeRoots": ["node_modules/@types", "src/index.d.ts"]
    },
    "include": ["src"],
    "references": [{ "path": "./tsconfig.node.json" }]
}

我做了一些搜索,但找不到遇到同样问题的人.

推荐答案

我终于解决了这个问题.如果存在exports字段,则不会使用package.json中的types字段:https://www.typescriptlang.org/docs/handbook/esm-node.html#packagejson-exports-imports-and-self-referencing

如果需要为类型声明指向不同的位置,则需要添加"types"导入条件.

因此,为了解决这个问题,我需要在exports."."声明中添加"types": "./dist/index.d.ts"个指示:

{
  "exports": {
    ".": {
      "types": "./dist/index.d.ts",
      "require": "./dist/mypackage.cjs",
      "import": "./dist/mypackage.es.js"
    },
  },
}

Typescript相关问答推荐

了解TS类型参数列表中的分配

使用React处理Websocket数据

泛型和数组:为什么我最终使用了`泛型T []`而不是`泛型T []`?<><>

当我点击外部按钮时,如何打开Html Select 选项菜单?

如何键入函数以只接受映射到其他一些特定类型的参数类型?

对数组或REST参数中的多个函数的S参数进行类型判断

编剧错误:正在等待Expect(Locator).toBeVisible()

如何在LucideAngular 添加自定义图标以及如何使用它们

Material UI / MUI系统:我如何告诉TypeScript主题是由提供程序传递的?

React Typescript项目问题有Redux-Toolkit userSlice角色问题

在列表的指令中使用intersectionObservable隐藏按钮

如何合并两个泛型对象并获得完整的类型安全结果?

找不到财产的标准方式?

为什么TypeScrip不能解析对象析构中的赋值?

Typescript -返回接口中函数的类型

自动通用回调

抽象类对派生类强制不同的构造函数签名

必需的输入()参数仍然发出&没有初始值设定项&q;错误

有没有一种更好的方法来存储内存中的数据,而不是在类型脚本中使用数组和基于索引的函数?

使用 fp-ts 时如何使用 TypeScript 序列化任务执行?