简体   繁体   English

打字稿无法在node_modules文件夹的定义文件中找到类型

[英]Typescript cannot find type in definition file in node_modules folder

I have a project which has this structure: 我有一个具有以下结构的项目:

project/
├── package.config
├── node_modules/
│   ├── interactjs/
│   ├── ├── index.d.ts
├── src/
│   ├── browser/
│   |   ├── tsconfig.json
│   |   ├── index.ts

I have the following ./package.json : 我有以下./package.json

{
    ...
    "dependencies": {
        "interactjs": "1.3.4"
    },
    "devDependencies": {
        "typescript": "3.2.2"
    }
}

My ./src/browser/tsconfig.json is: 我的./src/browser/tsconfig.json是:

{
    "compilerOptions": {
        "target": "es5",
        "module": "none",
        "declaration": true,
        "strict": true,
        "strictNullChecks": false,
        "outDir": "./out"
    },
    "typeRoots": [
        "../../node_modules",
        "../../node_modules/@types",
        "../definitions"
    ],
    "include": [
        "./**/*"
    ]
}

As you can see I am including also folder definitions as there are some manual definitions I want to include in all Typescript files of my project. 如您所见,我还包括文件夹definitions因为我想在项目的所有Typescript文件中包括一些手动定义。

Problem 问题

The following fails compilation: 以下导致编译失败:

const p : interact.Position = { x: 1, y: 2 };

With error: 有错误:

index.ts:9:11 - error TS2503: Cannot find namespace 'interact'.

9 const s : interact.Position = { x: 1, y: 2 };
            ~~~~~~~~

interact is not found even though in node_modules/interactjs file index.d.ts is present with all the definitions. interact找不到即使在node_modules/interactjs文件index.d.ts存在的所有定义。

What is the problem? 问题是什么?

If you wish to keep module resolution to none adding the typing file into the "include" section should give you the output desired. 如果您希望保持模块分辨率不变,则将键入文件添加到"include"部分中,这将为您提供所需的输出。

tsconfig.json tsconfig.json

{
    "compilerOptions": {
        "target": "es5",
        "module": "none",
        "declaration": true,
        "strict": true,
        "strictNullChecks": false,
        "outDir": "./out",
        "noImplicitAny": false //<------ to ignore the errors in interactjs/index.d.ts
    },
    "typeRoots": [
        "../../node_modules",
        "../../node_modules/@types",
        "../definitions"
    ],
    "include": [
        "../../node_modules/interactjs/index.d.ts", //<----- include interact in the global scope
        "./**/*"
    ]
}

index.ts 索引

const p : interact.Position = { x: 1, y: 2 };
const s : interact.SnapPosition = { x: 1, y: 2, range: 0 };

const listener : interact.Listener = (e: interact.InteractEvent)=>{
    console.log(e);
};

interact.on("cancel", listener)

built index.js 内置index.js

"use strict";
var p = { x: 1, y: 2 };
var s = { x: 1, y: 2, range: 0 };
var listener = function (e) {
    console.log(e);
};
interact.on("cancel", listener);

It looks like you're missing the line "moduleResolution":"node", in your tsconfig.json. 看起来您在tsconfig.json中缺少行"moduleResolution":"node",

This is what one of my tsconfig.json files looks like. 这就是我的tsconfig.json文件之一。

{
  "compileOnSave": false,
  "compilerOptions": {
  "baseUrl": "./",
  "outDir": "./dist/out-tsc",
  "sourceMap": true,
  "declaration": false,
  "module": "es2015",
  "moduleResolution": "node",
  "emitDecoratorMetadata": true,
  "experimentalDecorators": true,
  "target": "es5",
  "typeRoots": [
    "node_modules/@types"
  ],
  "lib": [
    "es2017",
    "dom"
   ]
  }
}

When you import a package Typescript (and Node) determine which file/module to import from that package by looking for a main field in the package.json file included with the package. 导入包Typescript(和节点)时,通过在package.json附带的package.json文件中查找main字段,确定要从该包导入哪个文件/模块。 The package.json file in interactjs includes this line: interactjs中的package.json文件包含以下行:

"main": "dist/interact.js",

That means that the main module in the interactjs package is named interact.js , and it is in the dist/ directory. 这意味着interactjs包中的主模块名为interact.js ,位于dist/目录中。

If the package.json file of the package does not explicitly specify the location of a type definition file, Typescript will assume that the type definition file has the same base name and location as the package's main module. 如果package.json文件未明确指定类型定义文件的位置,则Typescript将假定该类型定义文件的基本名称和位置与包的主模块相同。 Given the location of the main module in interactjs, Typescript will look for type definitions in the file dist/interact.d.ts . 给定主模块在interactjs中的位置,Typescript将在dist/interact.d.ts文件中查找类型定义。 Try renaming the type definition file from index.d.ts to interact.d.ts , and make sure that it is in the dist/ directory. 尝试将类型定义文件从index.d.ts重命名为interact.d.ts ,并确保它在dist/目录中。

If you are authoring a package that includes Typescript definitions it is helpful to make the location of the definition file explicit by including a types field in your package.json field as is described in the publishing guide 如果您要编写包含Typescript定义的软件包,则可以通过在package.json字段中包含types字段来使定义文件的位置明确,如发布指南中所述。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM