繁体   English   中英

TS2339:属性在类型上不存在

[英]TS2339: Property does not exist on type

我正在WebStorm 2016.2.2中将js文件转换为ts。

我有以下代码段:

///<reference path="./typings/globals/node/index.d.ts" />

global.base_dir = __dirname;

global.abs_path = function(path) {
    return global.base_dir + path;
};
global.include = function(file) {
    return require(global.abs_path('/' + file));
};

base_dirabs_pathinclude产生的错误:

TS2339:类型“ Global”上不存在属性“ base_dir”

TS2339:类型“ Global”上不存在属性“ abs_path”

TS2339:属性“ include”在类型“ Global”上不存在

所以我将它们添加到“全局”界面,如下所示:

///<reference path="./typings/globals/node/index.d.ts" />

declare namespace NodeJS{
    interface Global {
        base_dir: string;
        abs_path: (path: string) => string;
        include: (file: string) => string;
    }
}

global.base_dir = __dirname;

global.abs_path = function(path) {
    return global.base_dir + path;
};
global.include = function(file) {
    return require(global.abs_path('/' + file));
};

它确实消除了这些错误。

然后,我继续转换文件的其余部分,必须从express导入RequestResponse ,因此添加了以下内容:

///<reference path="./typings/modules/express/index.d.ts" />

import {Request, Response} from "~express/lib/express";

所以现在整个代码段是这样的:

///<reference path="./typings/globals/node/index.d.ts" />
///<reference path="./typings/modules/express/index.d.ts" />

import {Request, Response} from "~express/lib/express";

declare namespace NodeJS{
    interface Global {
        base_dir: string;
        abs_path: (path: string) => string;
        include: (file: string) => string;
    }
}

global.base_dir = __dirname;

global.abs_path = function(path) {
    return global.base_dir + path;
};
global.include = function(file) {
    return require(global.abs_path('/' + file));
};

不幸的是,添加import语句返回了TS2339错误,因此我再次陷入:

TS2339:类型“ Global”上不存在属性“ base_dir”

TS2339:类型“ Global”上不存在属性“ abs_path”

TS2339:属性“ include”在类型“ Global”上不存在

BTW Express与该错误无关。 我试图从其他模块导入,它产生了相同的错误。 当我至少有一个import语句时发生

有人知道我该如何解决吗?

任何帮助将不胜感激!

问题是任何具有顶级导入或导出功能的打字稿文件都将成为一个模块。 参见https://github.com/Microsoft/TypeScript/issues/1574

暂无
暂无

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

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