【问题标题】:typescript 0.9 interface export. is it possible?typescript 0.9 接口导出。是否可以?
【发布时间】:2013-06-26 02:02:47
【问题描述】:

正如我现在从 typescript 中看到的,您可以像这样导出类:

// client.ts 
    class Client { 
        constructor(public name: string, public description: string) { } 
    } 
    export = Client; 

// app.ts 
import MyClient = require('./client'); 
var myClient = new MyClient("Joe Smith", "My #1 client");

但是,有没有办法导出接口?

现在我收到一条错误消息:

错误 TS1003:需要标识符。

当我尝试做这样的事情时:

// INotifier.ts
    interface INotifier {
        // code
    }
    export = INotifier;

【问题讨论】:

    标签: interface typescript


    【解决方案1】:

    我已经在 Visual Studio 中尝试过,这对我有用,使用 import 语法(答案已更新以反映 TypeScript 语言的变化):

    file1.ts

    interface IPoint {
        getDist(): number;
    }
    
    export = IPoint;
    

    app.ts

    // Obsolete syntax
    //import example = module('file1');
    
    // Newer syntax
    import example = require('file1');
    
    class Point implements example {
        getDist() {
            return 1;
        }
    }
    

    附加说明:在这种情况下,您将无法使用 ECMAScript 6 样式导入 - 因为它们仅适用于类和模块。

    //Won't work because it resolves to a "non-module entity"
    import * as example from 'file1';
    

    【讨论】:

    • 但是,导出界面目前在 Visual Studio 中不提供 Intellisense。您必须这样做:var r: IPoint; export = r; 才能获得 Intellisense。
    猜你喜欢
    • 2015-08-23
    • 1970-01-01
    • 2019-01-27
    • 2011-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-23
    • 2017-06-07
    相关资源
    最近更新 更多