简体   繁体   中英

Converting JavaScript `require` to TypeScript

I am reading a tutorial on Express and Node.js and trying to convert the JavaScript to TypeScript.

The code includes

var handlebars = require("express-handlebars")
    .create({ defaultLayout: 'main' });

which I have translated to TypeScript as is.

Is there a more idiomatic TypeScript using import ... as (from ECMAScript 6) or import handlebars = ... that accomplishes the same thing (including the call to create )?

I have tried the following:

import handlebars = require("express-handlebars")
handlebars.create({ defaultLayout: 'main' })

but I get a runtime error from nodejs on the later line

app.engine('handlebars', handlebars.engine)

saying Error: callback function required .

I assume that the error comes because the object has not been properly created.

I do not know this package but looking at the exports there is no engine exported.

My guess is

import * as handlebars from 'express-handlebars'; 
const engine = handlebars.create(...).engine; 
app.engine('handlebars', engine);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM