微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

TypeScript混合类型实现

http://www.typescriptlang.org/Handbook#interfaces网站上解释了混合类型.因为这些对于创建JS的输入非常有用 – 我想知道是否可以定义一个实现例如JS的类.这样的界面:

interface Counter {
    (start: number): string;
    interval: number;
    reset(): void;
}

如果没有,那么可能只创建该类型的对象?怎么样?

解决方法

I wonder if it’s possible to define a class implementing

没有

If not then maybe it’s possible to create just an object of that type?

是.将使用类型断言(或任何):

interface Counter {
    (start: number): string;
    interval: number;
    reset(): void;
}

function getCounter():Counter{
    var counter = <Counter>function(start:number){};
    counter.interval = 123;
    counter.reset = function(){};
    return counter;
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐