简体   繁体   English

打字稿错误:app.ts(18,10):错误TS2503:找不到命名空间'server'

[英]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.

相关问题 Ionic2 + Firebase应用程序编译错误:TypeScript错误:错误TS2503:找不到名称空间“ firebase” - Ionic2+Firebase app compile error: TypeScript error:Error TS2503: Cannot find namespace 'firebase' 错误TS2503:找不到我的名称空间 - error TS2503: Cannot find my namespace 错误 TS2503:找不到命名空间“WebMidi” - error TS2503: Cannot find namespace 'WebMidi' dt~ react-router error TS2503:找不到命名空间'HistoryModule' - dt~react-router error TS2503: Cannot find namespace 'HistoryModule' Angular:无法使用 ExcelJS 导出 excel - 错误 TS2307:找不到模块“流” - 错误 TS2503:找不到命名空间“NodeJS” - Angular : Can't export excel using ExcelJS - error TS2307: Cannot find module 'stream' - error TS2503: Cannot find namespace 'NodeJS' 在模块内部使用模块进行类型声明无法编译(TS2503无法找到名称空间) - using module inside a module for type declaration doesn't compile (TS2503 cannot find namespace) axios 错误命名空间“axios”未找到 ts(2503) - axios error Namespace 'axios' not found ts(2503) 尝试使用导入的字符串常量来约束打字稿类型文字时,获取TS2503 - Getting s TS2503 when trying to use an imported string constant to constrain a typescript type literal typescript 找不到名称 'provide'.ts(2304) 错误 app.module.ts - typescript cannot find name 'provide'.ts(2304) error app.module.ts 打字稿错误:TS2304:找不到名字'$' - typescript error: TS2304: Cannot find name '$'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM