In node.js I'm using jsreport-core
and they do their import like var jsreport = require('jsreport-core')();
with the trailing ()
. I'm curious what is the best way to replicate this import technique is in TypeScript?
In the above example, the TypeScript compiler will give an error if we use variables before declaring them using let, whereas it won't give an error when using variables before declaring them using var.
Import types. You can also import declarations from other files using import types. This syntax is TypeScript-specific and differs from the JSDoc standard: import types can be used to get the type of a value from a module if you don’t know the type, or if it has a large type that is annoying to type:
You can use the “@type” tag and reference a type name (either primitive, defined in a TypeScript declaration, or in a JSDoc “@typedef” tag). You can use most JSDoc types and any TypeScript type, from the most basic like string to the most advanced, like conditional types.
So when you type const x = require ('x'), TypeScript is going to complain that it doesn't know what require is. You need to install @types/node package to install type definitions for the CommonJS module system in order to work with it from the TypeScript. Let's imagine you have a.ts and x.js as source files.
I'm curious what the best way to replicate this import technique is in TypeScript
You need to split the import
and the function call:
import jsreportCreator = require('jsreport-core');
const jsreport = jsreportCreator();
I am pulling in the "@types/jsreport-core": "^1.5.1"
in package.json
's devDependencies
and am using an import
, for example:
import JsReport from 'jsreport-core';
const jsReport = JsReport({
loadConfig: true
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With