简体   繁体   English

TypeScript编译器找不到我先前声明的变量

[英]The TypeScript compiler can't find my previosly declared variable

I'm trying to create a Node.js application which will connect to a MongoDb database. 我正在尝试创建一个将连接到MongoDb数据库的Node.js应用程序。 I'm using TypeScript. 我正在使用TypeScript。 But the following lines of code are troublesome. 但是以下几行代码很麻烦。

/// <reference path="mongodb.d.ts" />
var mongo = require('mongodb');
class DefaultModel<T> {
    private db : mongo.Db;
}

TSC reports that: TS2095: Could not find symbol 'mongo'. TSC报告: TS2095: Could not find symbol 'mongo'. . I don't understand why it cannot find it as it was declared just outside the class. 我不明白为什么它找不到,因为它只是在类外声明的。 Can you help me figure out why? 你能帮我弄清楚为什么吗?

The module is declared in mongodb.d.ts as declare module "mongodb" { /* Omitted */ } 该模块在mongodb.d.ts中declare module "mongodb" { /* Omitted */ }declare module "mongodb" { /* Omitted */ }

TS2095: Could not find symbol 'mongo'. TS2095:找不到符号“ mongo”。

You are trying to use it in the type delaration space : :mongo.Db; 您正在尝试在类型定义空间中使用它:mongo.Db; . And you only have it declared in the variable declaration space : var mongo 而且只有在变量声明空间中声明它: var mongo

Fix: use import not var : 修复:使用import not var

/// <reference path="mongodb.d.ts" />
import mongo = require('mongodb');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM