xxxx库可能需要更新其package.json或typings.ts

spa*_*ark 5 typescript reactjs vite

我有一个公共组件库调用rd-component,当我在新项目中使用此组件时"rd-component": "^0.1.47",Visual Studio代码显示如下错误:

Could not find a declaration file for module 'rd-component'. '/Users/John/source/reddwarf/frontend/ppt-web/node_modules/.pnpm/rd-component@0.1.47_@types+node@20.1.1_react-dom@18.2.0_react-redux@8.0.5_react@18.2.0_vite@4.3.2/node_modules/rd-component/dist/rd-component.es.js' implicitly has an 'any' type.
  There are types at '/Users/John/source/reddwarf/frontend/ppt-web/node_modules/rd-component/dist/index.d.ts', but this result could not be resolved when respecting package.json "exports". The 'rd-component' library may need to update its package.json or typings.ts(7016)
Run Code Online (Sandbox Code Playgroud)

我应该怎么做才能解决这个问题?这是公共 rd 组件的package.json

{
    "name": "rd-component",
    "version": "0.1.47",
    "type": "module",
    "description": "Reddwarf public component lib",
    "files": [
        "dist"
    ],
    "main": "./dist/rd-component.umd.js",
    "module": "./dist/rd-component.es.js",
    "types": "./dist/index.d.ts",
    "exports": {
        ".": {
            "import": "./dist/rd-component.es.js",
            "require": "./dist/rd-component.umd.js"
        },
        "./dist/style.css": {
            "import": "./dist/style.css",
            "require": "./dist/style.css"
        }
    },
    "scripts": {
        "dev": "vite",
        "build": "tsc && vite build",
        "watch": "vite build --watch",
        "lint": "eslint src --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
        "preview": "vite preview"
    },
    "devDependencies": {
        "@types/node": "^20.1.0",
        "@types/react": "^18.2.0",
        "@types/redux-logger": "^3.0.9",
        "axios": "^1.3.4",
        "js-wheel": "https://github.com/jiangxiaoqiang/js-wheel.git",
        "react": "^18.2.0",
        "react-dom": "^18.2.0",
        "react-redux": "^8.0.5",
        "redux": "^4.2.1",
        "redux-logger": "^3.0.6",
        "uuid": "^9.0.0",
        "vite": "^4.3.5"
    },
    "dependencies": {
        "@reduxjs/toolkit": "^1.9.5",
        "@vitejs/plugin-react": "^4.0.0",
        "antd": "^5.4.6",
        "vite-plugin-dts": "^2.3.0"
    }
}
Run Code Online (Sandbox Code Playgroud)

我已经阅读了一些告诉添加打字的答案,但似乎是遗留的打字稿配置。

ghy*_*ybs 31

如果有字段,则不再使用types中的字段: https: //www.typescriptlang.org/docs/handbook/release-notes/typescript-4-7.html#packagejson-exports-imports-and-self-referencingpackage.jsonexports

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

因此,您应该只需"types": "./dist/index.d.ts"在声明中复制您的指示即可exports."."

{
  "exports": {
    ".": {
      // Specify types first
      "types": "./dist/index.d.ts",
      "import": "./dist/rd-component.es.js",
      "require": "./dist/rd-component.umd.js"
    },
    "./dist/style.css": {
      "import": "./dist/style.css",
      "require": "./dist/style.css"
    }
  },
}
Run Code Online (Sandbox Code Playgroud)

  • 谢谢你!谢谢你!谢谢你!谢谢你!谢谢你!谢谢你!谢谢你!谢谢你!谢谢你!谢谢你!谢谢你!谢谢你!谢谢你!谢谢你!谢谢你!谢谢你!谢谢你!谢谢你!谢谢你!谢谢你!谢谢你!谢谢你!谢谢你!谢谢你! (5认同)
  • 你节省了我的时间。 (2认同)