javascript - 在 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)
解决方案
错误无法读取未定义的属性“转换”是运行时错误。所以首先我们必须在查看类型之前解决这个问题。
在包的使用说明中,我们看到:
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;
}
推荐阅读
- excel - 根据单元格值隐藏多行的代码
- ansible - 使用 kolla-ansible 安装 ceph 进行多合一设置
- haskell - 如何在 Haskell 的函数中使用 do 块?
- python - 如果最后一个 impala 语句是 select 语句,如何检查 pyodbc 包?
- c# - DevExpress:如何创建点击事件处理程序?
- function - 将相同的参数添加到 Elm 中的函数列表
- android - 如何使用 react-native 预览应用程序组件内的文件
- amazon-web-services - 上传到 S3 存储桶时出现 403 禁止错误
- c++ - 如何添加宏 Visual Studio 2019
- javascript - 创建没有标识符的多维数组