只要有可能,我就会尝试在require上使用import,但在某些情况下,这会破坏类型检查。处理这个问题的正确方法是什么?是否可以强制转换导入?不是所有的require都可以用import替代吗?

左图:Property 'get' does not exist...
右图:用import * as convict from "convict";代替require("convict");
左图:
"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;发布于 2018-01-22 02:53:05
不幸的是,这是您的一个依赖项convict的类型问题。查看@types/convict源代码:
https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/convict/index.d.ts#L122
他们使用export = ...导出定义,根据Typescript手册:
https://www.typescriptlang.org/docs/handbook/modules.html (参见“export =和import = require()”标题)
中断import * from ...导入。所以,你有点被困在这个问题上了。
https://stackoverflow.com/questions/48368325
复制相似问题