我试图为node-json2html创建一个声明文件,但是使用import而不是require导入包会产生错误。
我相信我创建的减速文件是错误的。

可以在以下位置找到该软件包:https://www.npmjs.com/package/node-json2html

打字稿

javascript

我试图多次更改声明文件。

减速文件:

declare module "node-json2html" {
    class json2html {
        static version: string;
        static transform(data: string, transform: J2Htransform, _options?: J2Hoptions): string | J2Htransform;
        static toText(html: string): string;
    }
    interface J2Hoptions { [x: string]: any; events: boolean; }
    interface J2Htransform {
        '<>'?: string,
        text?: string,
        html?: J2Htransform[] | string,
        [x: string]: string | Function | J2Htransform[] | undefined
    }


    function _isArray(obj: any): boolean;
    function _transform(obj: any, transform: object, options: J2Hoptions): J2Htransform;
    function _extend(options: J2Hoptions, _options: J2Hoptions): J2Hoptions;
    function _apply(obj: object, transform: J2Htransform, index: number, options: J2Hoptions): J2Htransform;

}


导入命令:

import {json2html} from 'node-json2html';


使用包的函数:

private async movieListToJson(movies: Array<Movie>): Promise<string> {
        const data: string = JSON.stringify(movies);
        const transform = { "<>": "div", "html": "<ul><li>id: ${id}</li><li>name: ${name}</li><li>image adress: ${imageAdress}</li><li>score: ${score}</li><li>page url: ${pageUrl}</li></ul>" };
        return '<h1>Movie list:</br></h1>' + json2html.transform(data, transform);
    }


问题:“无法读取未定义的属性'transform'”

完整的错误消息:

UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'transform' of undefined
    at MovieManager.<anonymous> (C:\Users\trfac\Documents\Projects\JS\movies\dist\movieManager.js:196:96)
    at step (C:\Users\trfac\Documents\Projects\JS\movies\dist\movieManager.js:39:23)
    at Object.next (C:\Users\trfac\Documents\Projects\JS\movies\dist\movieManager.js:20:53)
    at C:\Users\trfac\Documents\Projects\JS\movies\dist\movieManager.js:14:71
    at new Promise (<anonymous>)
    at __awaiter (C:\Users\trfac\Documents\Projects\JS\movies\dist\movieManager.js:10:12)
    at MovieManager.movieListToJson (C:\Users\trfac\Documents\Projects\JS\movies\dist\movieManager.js:191:16)
    at MovieManager.<anonymous> (C:\Users\trfac\Documents\Projects\JS\movies\dist\movieManager.js:80:51)
    at step (C:\Users\trfac\Documents\Projects\JS\movies\dist\movieManager.js:39:23)
    at Object.next (C:\Users\trfac\Documents\Projects\JS\movies\dist\movieManager.js:20:53)
(node:6644) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 6)

最佳答案

错误“无法读取未定义的属性'transform'”是运行时错误。因此,首先,我们必须在查看类型之前对其进行修复。

在包的usage instructions上,我们看到:

var json2html = require('node-json2html');


这意味着整个模块实际上是json2html。它没有命名的导出json2html

因此,您必须将导入更改为:

import * as json2html from 'node-json2html';


或启用esModuleInterop的此:

import json2html from 'node-json2html';


现在,运行时错误应该消失了。当然,我们必须相应地调整类型。为此,将export =语句添加到模块声明中:

declare module "node-json2html" {
    class json2html {
        static version: string;
        static transform(data: string, transform: J2Htransform, _options?: J2Hoptions): string | J2Htransform;
        static toText(html: string): string;
    }
    // ...
    export = json2html;
}

07-24 13:52