TypeScript的常见问题,并且更喜欢导入超过require [英] Frequent issue with TypeScript and preferring import over require

查看:237
本文介绍了TypeScript的常见问题,并且更喜欢导入超过require的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在可能的情况下,我尝试在 require 上使用 import ,但在某些情况下会中断类型检查。处理此问题的正确方法是什么?是否有可能进行导入?是不是要求可以用 import 替换?

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?

左:财产获取不存在...

正确:从*定罪进口*作为囚犯; 而不是要求(定罪);

左:

"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);
}

...

右:

//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;


推荐答案

不幸的是,这是您的某个依赖项的输入问题,定罪。查看 @ types / convict 来源:

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

他们使用 export = ... 导出定义,根据Typescript手册:

They export the definitions using export = ..., which according to the Typescript handbook:

ht tps://www.typescriptlang.org/docs/handbook/modules.html (请参阅 export = import = require ()标题

从...... 进口中断 import *。所以,你有点坚持这个。

breaks import * from ... imports. So, you're kinda stuck on this one.

这篇关于TypeScript的常见问题,并且更喜欢导入超过require的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆