简体   繁体   English

tsc 生成的 .d.ts 文件给出错误“找不到命名空间‘Jimp’”

[英]tsc generated .d.ts files giving error "Cannot find namespace 'Jimp'"

I'm working on an NPM package, and one of my classes is simply this:我正在开发一个 NPM 包,我的一个课程就是这样:

import { MIME_PNG } from 'jimp';
import { IDimensions } from './spritesheet';

/**
 * A class for a single sprite. This contains the image
 * and supporting data and methods
 */
export class Sprite {
    image: Jimp.Jimp;
    dimensions: IDimensions;

    /**
     * 
     * @param img a jimp image of the sprite
     */
    constructor (img: Jimp.Jimp) {
        this.image = img;

        this.dimensions = {
            width: this.image.bitmap.width,
            height: this.image.bitmap.height
        }
    }

    /**
     * Get the buffer for the sprite. Returns a promise.
     */
    getBuffer (): Promise<Buffer> {
        return new Promise((resolve, reject) => {
            return this.image.getBuffer(MIME_PNG, (err, buff) => {
                if (err) return reject(err);

                resolve(buff);
            });
        });
    }
}

Typescript compiles this fine with just the command tsc , but when I publish the package I'm compiling with the command tsc -d -p ./ --outdir dist/ to generate .d.ts files. Typescript 仅使用命令tsc就可以很好地编译它,但是当我发布包时,我正在使用命令tsc -d -p ./ --outdir dist/进行编译以生成.d.ts文件。

The outputted file looks like this:输出的文件如下所示:

/// <reference types="node" />
import { IDimensions } from './spritesheet';
/**
 * A class for a single sprite. This contains the image
 * and supporting data and methods
 */
export declare class Sprite {
    image: Jimp.Jimp;
    dimensions: IDimensions;
    /**
     *
     * @param img a jimp image of the sprite
     */
    constructor(img: Jimp.Jimp);
    /**
     * Get the buffer for the sprite. Returns a promise.
     */
    getBuffer(): Promise<Buffer>;
}

Now, when viewing this file in VSCode, and when publishing/importing into a different project, I get typescript errors on the Jimp.Jimp types saying "Cannot find namespace 'Jimp'."现在,在 VSCode 中查看此文件以及发布/导入到不同项目时,我在Jimp.Jimp类型上收到打字稿错误, Jimp.Jimp “找不到命名空间 'Jimp'”。

I noticed tsc is dropping the import { MIME_PNG } from 'jimp';我注意到tsc正在删除import { MIME_PNG } from 'jimp'; line from the file and, if I add that file back, it compiles just fine.文件中的行,如果我将该文件添加回来,它编译得很好。

My tsconfig.json file looks like this:我的tsconfig.json文件如下所示:

{
  "compilerOptions": {
    "target": "es6",
    "module": "commonjs",
    "sourceMap": true,
    "outDir": "./dist",
    "rootDir": "./src",
    "strict": true,
    "moduleResolution": "node"
  }
}

I had the same issue as you.我和你有同样的问题。 I was able to solve it by adding a reference path to the .d.ts jimp file at the top of the .ts file where I import the stuff from Jimp.我能够通过在.ts文件的顶部添加一个指向.d.ts jimp 文件的参考路径来解决它,我在其中从 Jimp 导入了东西。

So before import { MIME_PNG } from 'jimp';所以在import { MIME_PNG } from 'jimp';之前import { MIME_PNG } from 'jimp';

you should add /// <reference path="../../node_modules/jimp/jimp.d.ts" />你应该添加/// <reference path="../../node_modules/jimp/jimp.d.ts" />

This way it seems that Typescript is able to find the Jimp namespace.通过这种方式,Typescript 似乎能够找到 Jimp 命名空间。

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

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