【发布时间】: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