[英]Typescript error: app.ts(18,10): error TS2503: Cannot find namespace 'server'
Here is very simple code: 这是非常简单的代码:
"use strict";
import * as express from "express";
class Server {
public app: express.Application;
public static run(): Server {
return new Server();
}
constructor() {
this.app = express();
}
}
var server = Server.run();
export = server.app;
When I try to compile it tsc app.ts
I get an error: 当我尝试编译它时
tsc app.ts
我收到一个错误:
app.ts(18,10): error TS2503: Cannot find namespace 'server'.
app.ts(18,10):错误TS2503:找不到命名空间'server'。
In spite of the error TSC creates output file (app.js) 尽管错误TSC创建输出文件(app.js)
Why do I get the error? 为什么我会收到错误? What do I do wrong?
我做错了什么?
I changed your last line to export default server.app
and it compiles fine. 我更改了你的最后一行
export default server.app
并且它编译得很好。
You have a commonjs style export statement, not an ES6 export statement that typescript expects. 您有一个commonjs样式导出语句,而不是typescript期望的ES6导出语句。
See the typescript specification for export syntax at https://www.typescriptlang.org/docs/handbook/modules.html 有关导出语法的typescript规范,请参阅https://www.typescriptlang.org/docs/handbook/modules.html
Typescript does support some export =
syntax, however it is limited to class, interface, namespace, function, or enum. Typescript确实支持一些
export =
语法,但它仅限于类,接口,命名空间,函数或枚举。
The documentation states: 文件说明:
The export = syntax specifies a single object that is exported from the module.
export =语法指定从模块导出的单个对象。 This can be a class, interface, namespace, function, or enum."
这可以是类,接口,命名空间,函数或枚举。“
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.