只要有可能,我都会尝试使用import
over require
但在某些情况下这会破坏类型检查。处理这个问题的正确方法是什么?是否可以导入?不是全部require
可以替换为import
's?
Left: Property 'get' does not exist...
Right: import * as convict from "convict";
代替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;
不幸的是,这是您的依赖项之一的类型问题,convict
。看着@types/convict
source:
https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/convict/index.d.ts#L122 https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/convict/index.d.ts#L122
他们使用导出定义export = ...
,根据 Typescript 手册:
https://www.typescriptlang.org/docs/handbook/modules.html https://www.typescriptlang.org/docs/handbook/modules.html(参见“export =
and import = require()
“ 标题)
breaks import * from ...
进口。所以,你有点卡在这一点上了。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)