Frequent issue with TypeScript and preferring import over require

Brian Jenkins

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 instead of require

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;
Joe Quigley

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.

edited at
0

Comments

0 comments
Login to comment

Related

Import vs Require in Typescript

Typescript import/as vs import/require?

Preferring OSPF over Static Route

TypeScript: import = require vs import from

Material Design Lite with ReactJS (import/require Issue)

Sequelize import having an issue with TypeScript

Module vs Namespace - Import vs Require Typescript

Make typescript to output "require" instead of "import"

Typescript : require statement not part of an import statement

Webpack / typescript require works but import doesn't

Require, Import, Require Import

C programming preferring uint8 over char

Preferring shift over reduce in parser for language without statement terminators

Preferring Reader monad over directly passing the environment as parameters

Typescript Tests Issue With Absolute Import Linting

TypeScript how to solve - 'require' call may be converted to an import

How to convert a TypeScript import statement into a JS Require statement

Nodejs require()(require()) to import

Python return statement, frequent issue

JAXB & JPA annotations. Are there any strong arguments for preferring field access over property access?

Preferring non-generic version of method over generic version in c#

Why is adl preferring the 'boost::range_detail::operator|' over the local 'operator|'?

PL/SQL issue concerning Frequent Itemset

In Typescript, how can I make it understand the types when I import by using require()?

How to convert Electron files from 'require' to 'import' for use with TypeScript? Angular2+

Visual Studio ReSharper - Typescript automatic import class declared in external module - Use from instead of require

ESLint: Require statement not part of import statement.(@typescript-eslint/no-var-requires)

How can I import a Typescript 1.0 function like a node.js require with arguments?

How to import node module in typescript project. ERR_REQUIRE_ESM