tsc 生成的 .d.ts 个文件给出错误 "Cannot find namespace 'Jimp'"
tsc generated .d.ts files giving error "Cannot find namespace 'Jimp'"
我正在开发一个 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 中查看此文件时,以及当 publishing/importing 进入另一个项目时,我在 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"
}
}
我遇到了和你一样的问题。我能够通过在 . 文件顶部的 .d.ts jimp 文件中添加参考路径来解决它,其中我从 Jimp 进口东西。
所以在import { MIME_PNG } from 'jimp';
之前
你应该添加
/// <reference path="../../node_modules/jimp/jimp.d.ts" />
通过这种方式,Typescript 似乎能够找到 Jimp 命名空间。
我正在开发一个 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 中查看此文件时,以及当 publishing/importing 进入另一个项目时,我在 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"
}
}
我遇到了和你一样的问题。我能够通过在 . 文件顶部的 .d.ts jimp 文件中添加参考路径来解决它,其中我从 Jimp 进口东西。
所以在import { MIME_PNG } from 'jimp';
你应该添加
/// <reference path="../../node_modules/jimp/jimp.d.ts" />
通过这种方式,Typescript 似乎能够找到 Jimp 命名空间。