我正在开发一个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
类型上收到了打字稿错误"Cannot find namespace 'Jimp'."
我注意到 tsc
正在从文件中删除 import { MIME_PNG } from 'jimp';
行,如果我添加该文件,它编译就好了 .
我的 tsconfig.json
文件如下所示:
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"sourceMap": true,
"outDir": "./dist",
"rootDir": "./src",
"strict": true,
"moduleResolution": "node"
}
}
1 回答
我和你有同样的问题 . 我能够通过在 .ts 文件顶部的 .d.ts jimp文件中添加引用路径来解决它,我从Jimp导入了这些东西 .
所以
import { MIME_PNG } from 'jimp';
之前你应该添加
/// <reference path="../../node_modules/jimp/jimp.d.ts" />
这样,Typescript似乎能够找到Jimp命名空间 .