首页 > 解决方案 > 在 Typescript 中为 node-json2html 创建/修复声明文件

问题描述

我尝试为 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);
    }

问题:“无法读取未定义的属性‘转换’”

完整的错误信息:

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)

标签: javascriptnode.jstypescripttypescript-typings

解决方案


错误无法读取未定义的属性“转换”是运行时错误。所以首先我们必须在查看类型之前解决这个问题。

在包的使用说明中,我们看到:

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

这意味着整个模块实际上是json2html. 它没有命名的 export 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;
}

推荐阅读