只要有可能,我都会尝试使用 import
而不是 require
但在某些情况下,这会破坏类型检查。处理这个问题的正确方法是什么?是否可以导入?不是所有的 require
都可以用 import
替换吗?
左:属性“get”不存在...
正确: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;
最佳答案
不幸的是,这是您的一个依赖项 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 ...
导入。所以,你有点卡在这个上面了。
关于javascript - TypeScript 经常出现问题并且更喜欢 import 而不是 require,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48368325/