TS2339:类型上不存在属性 [英] TS2339: Property does not exist on type

查看:200
本文介绍了TS2339:类型上不存在属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm converting a js file to ts in WebStorm 2016.2.2.

我有以下片段:

///<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 产生了错误:

base_dir, abs_path and include produced the errors:

TS2339:全局"类型上不存在属性base_dir"

TS2339: Property 'base_dir' does not exist on type 'Global'

TS2339:全局"类型上不存在属性abs_path"

TS2339: Property 'abs_path' does not exist on type 'Global'

TS2339:全局"类型上不存在属性包含"

TS2339: Property 'include' does not exist on type 'Global'

所以我将它们添加到全局"界面如下:

So I added them to 'Global' interface as following:

///<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));
};

它确实消除了这些错误.

It did eliminate those errors.

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

Then, I continued converting the rest of the file, I had to import both Request and Response from express, so I added the following:

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

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

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

So now the whole snippet is like that:

///<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 错误,所以我再次坚持:

Unfortunately, adding the import statement has returned the TS2339 error, so I stuck again with:

TS2339:全局"类型上不存在属性base_dir"

TS2339: Property 'base_dir' does not exist on type 'Global'

TS2339:全局"类型上不存在属性abs_path"

TS2339: Property 'abs_path' does not exist on type 'Global'

TS2339:全局"类型上不存在属性包含"

TS2339: Property 'include' does not exist on type 'Global'

顺便说一句,Express 与此错误无关.我尝试从其他模块导入,但产生了相同的错误.它发生在我至少有一个 import 语句

BTW Express has nothing to to with this error specifically. I've tried to import from other modules and it produced the same error. It occurs when I have at least one import statement

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

Does someone know how can I fix it?

任何帮助将不胜感激!

推荐答案

问题是任何具有顶级导入或导出的 typescript 文件都会变成一个模块.请参阅 https://github.com/Microsoft/TypeScript/issues/1574

The problem is that any typescript file with top-level import or export becomes a module. See https://github.com/Microsoft/TypeScript/issues/1574

这篇关于TS2339:类型上不存在属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆