gpt4 book ai didi

javascript - 在 Typescript 中创建/修复 node-json2html 的声明文件

转载 作者:太空宇宙 更新时间:2023-11-04 01:25:18 24 4
gpt4 key购买 nike

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

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

typescript

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)

最佳答案

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

关于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;
}

关于javascript - 在 Typescript 中创建/修复 node-json2html 的声明文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57753018/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com