Whenever possible, I try to use import
over require
but in some circumstances this breaks type checking. What is the correct way to handle this issue? Is it possible to cast an import? Are not all require
's replaceable with import
's?
Left: Property 'get' does not exist...
Right: import * as convict from "convict";
instead of require("convict");
Left:
"use strict";
import * as Busboy from "busboy";
import * as convcit from "convict";
import * as config from "./config";
import * as express from "express";
import * as helmet from "helmet";
import * as logger from "morgan";
import * as path from "path";
import * as requestDebug from "request-debug";
import * as requestJs from "request";
// [ts] Property 'get' does not exist on type 'typeof "PrizmDoc-Node.js-Sample/config"'.
if (config.get("env") !== "production") {
requestDebug(requestJs);
}
...
Right:
//let convict = require("convict");
import * as convict from "convict";
const config = convict({
"env": {
"doc": "The application environment.",
"format": ["production", "development", "test"],
"default": "development",
"env": "NODE_ENV"
},
...
module.exports = config;
Unfortunately this is an issue with the typings of one of your dependencies, convict
. Looking at the @types/convict
source:
https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/convict/index.d.ts#L122
They export the definitions using export = ...
, which according to the Typescript handbook:
https://www.typescriptlang.org/docs/handbook/modules.html (see the "export =
and import = require()
" heading)
breaks import * from ...
imports. So, you're kinda stuck on this one.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments