【问题标题】:tsc generated .d.ts files giving error "Cannot find namespace 'Jimp'"tsc 生成的 .d.ts 文件给出错误“找不到命名空间 'Jimp'”
【发布时间】:2018-03-05 06:55:11
【问题描述】:

我正在开发一个 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 仅使用命令 tsc 就可以很好地编译,但是当我发布包时,我使用命令 tsc -d -p ./ --outdir dist/ 进行编译以生成 .d.ts 文件。

输出的文件如下所示:

/// <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>;
}

现在,当在 VSCode 中查看此文件时,以及在发布/导入到其他项目时,我在 Jimp.Jimp 类型上收到打字稿错误,提示“找不到命名空间 'Jimp'。”

我注意到tsc 正在从文件中删除import { MIME_PNG } from 'jimp'; 行,如果我将该文件添加回来,它编译得很好。

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

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

【问题讨论】:

    标签: node.js typescript namespaces tsc


    【解决方案1】:

    我和你有同样的问题。我可以通过在 .ts 文件顶部添加一个 .d.ts jimp 文件的引用路径来解决这个问题,我从 Jimp 导入这些文件。

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

    你应该添加 /// &lt;reference path="../../node_modules/jimp/jimp.d.ts" /&gt;

    这样看来,Typescript 似乎能够找到 Jimp 命名空间。

    【讨论】:

      猜你喜欢
      • 2021-09-06
      • 2021-04-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-28
      • 2017-02-15
      相关资源
      最近更新 更多