ค้นหาเว็บไซต์

typescript - index.d.ts ข้อผิดพลาด TS2307: ไม่สามารถหามอดูลของตัวเองได้


ฉันกำลังเผชิญปัญหาที่ฉันไม่สามารถแก้ไขได้ ฉันต้องการแชร์อินเทอร์เฟซและชนิดบางอย่างระหว่างไฟล์คลาสของฉัน

รีโพของฉันประมาณนี้:

test
  -> dist/
  -> src/
    -> index.ts
  -> .babelrc
  -> .eslintrc.js
  -> index.d.ts
  -> package.json
  -> tsconfig.json

ใน index.d.ts:

declare module test { // I've try with 'declare namespace test' but the result is the same
  export interface LabeledValue {
    label: string;
    size: number;
  }
}

ถ้าฉันนำเข้า LabeledValue ใน index.ts ฉันจะได้รับข้อผิดพลาดและฉันไม่เข้าใจสาเหตุ: import { LabeledValue } from 'test';//TS2307: ไม่พบโมดูล 'test'

src/index.ts

import {LabeledValue} from 'test';

function printLabel(labeledObj: LabeledValue) {
  console.log(labeledObj.label);
}

let myObj = {size: 10, label: "Size 10 Object"};
printLabel(myObj);

package.json:

{
  "name": "test",
  "types": "index.d.ts",
  "version": "1.0.0",
  "description": "test",
  "author": "oyabi",
  "license": "MIT",
  "private": true,
  "scripts": {
    "type-check": "tsc --noEmit",
    "type-check:watch": "yarn type-check --watch",
    "build": "npm run build:types && npm run build:js",
    "build:types": "tsc --emitDeclarationOnly",
    "build:js": "babel src --out-dir dist --extensions \".ts,.tsx\" --source-maps inline"
  },
  "dependencies": {
    "axios": "^0.19.0",
    "body-parser": "^1.18.3",
    "express": "^4.16.4",
    "helmet": "^3.21.2",
    "morgan": "^1.9.1",
    "winston": "^3.1.0"
  },
  "devDependencies": {
    "@babel/cli": "^7.7.0",
    "@babel/core": "^7.7.2",
    "@babel/plugin-syntax-class-properties": "^7.0.0",
    "@babel/polyfill": "^7.7.0",
    "@babel/preset-env": "^7.7.1",
    "@babel/preset-typescript": "^7.7.2",
    "@babel/runtime": "^7.7.2",
    "@types/express": "^4.17.2",
    "@types/helmet": "^0.0.45",
    "@types/morgan": "^1.7.36",
    "@types/qrcode": "^1.3.4",
    "@typescript-eslint/eslint-plugin": "^2.7.0",
    "@typescript-eslint/parser": "^2.7.0",
    "apidoc": "^0.18.0",
    "babel-core": "^7.0.0-bridge",
    "babel-eslint": "^10.0.1",
    "babel-jest": "^24.8.0",
    "babel-plugin-transform-class-properties": "^6.24.1",
    "babel-preset-airbnb": "^4.2.0",
    "babel-preset-env": "^1.7.0",
    "eslint": "^6.6.0",
    "eslint-config-airbnb-base": "^14.0.0",
    "eslint-config-prettier": "^6.5.0",
    "eslint-friendly-formatter": "^4.0.1",
    "eslint-plugin-import": "^2.18.2",
    "eslint-plugin-jest": "^23.0.4",
    "eslint-plugin-prettier": "^3.1.0",
    "jest": "^24.8.0",
    "moxios": "^0.4.0",
    "nodemon": "^1.19.4",
    "prettier": "^1.19.1",
    "retire": "^2.0.1",
    "supertest": "^4.0.2",
    "typescript": "^3.7.2",
    "uuid": "^3.3.2"
  }
}

.babelrc

{
  "presets": ["@babel/typescript",
    ["airbnb", {
      "useBuiltIns": "usage"
    }]
  ],
  "plugins": [
    "syntax-class-properties",
    "transform-class-properties"
  ]
}

.eslintrc

module.exports = {
  root: true,
  parser: "@typescript-eslint/parser",
  parserOptions: {
    sourceType: "module",
    project: "./tsconfig.json"
  },
  env: {
    node: true,
    jest: true
  },
  extends: [
    "airbnb-base",
    "plugin:@typescript-eslint/recommended",
    "plugin:jest/recommended",
    "plugin:prettier/recommended",
    "prettier/@typescript-eslint",
    "plugin:import/errors",
    "plugin:import/warnings",
    "plugin:import/typescript"
  ],
  plugins: ["@typescript-eslint", "jest", "import"],
  rules: {
    "import/no-commonjs": "error",
    "import/no-amd": "error",
    // allow debugger during development
    "no-debugger": process.env.NODE_ENV === "production" ? 2 : 0,
    "no-underscore-dangle": 0,
    "prettier/prettier": [
      2,
      {
        printWidth: 80,
        singleQuote: true,
        trailingComma: "all"
      }
    ]
  },
  settings: {
    "import/resolver": {
      node: {
        extensions: [".js", ".jsx", ".ts", ".tsx"]
      }
    }
  }
};

tsconfig.json

{
  "compilerOptions": {
    "target": "esnext",
    "module": "commonjs",
    "declaration": true,
    "outDir": "./dist",
    "strict": true,
    "noImplicitAny": true,
    "strictNullChecks": true,
    "noImplicitThis": true,
    "esModuleInterop": true,
    "resolveJsonModule": true
  },
  "include": ["index.d.ts"]
}

โค้ดของฉันจริงๆ ซับซ้อนกว่านี้ แต่คุณมีความเข้าใจปัญหาได้ดี ข้อผิดพลาดเกิดขึ้นใน Visual Studio Code และกับ yarn build (กับโค้ดจริง) ด้วยโค้ดนี้คุณสามารถใช้ yarn tsc src/index.ts เพื่อแสดงข้อผิดพลาดได้

คุณพอจะมีความคิดไหมว่าฉันจะแก้ปัญหานี้ได้อย่างไร โปรดช่วยบอกหน่อย?

คำตอบ #1

ฉันแก้ปัญหาของฉันเกี่ยวกับการติดตั้ง eslint-import-resolver-typescript และการตั้งค่าเริ่มต้นแล้ว

คำตอบ #2

ตรวจสอบให้แน่ใจว่าไฟล์ของคุณลงท้ายด้วย .ts

บทความที่เกี่ยวข้อง