TypeScript:使用调用签名和构造函数签名实现接口 [英] TypeScript: Implement interface with both call signature and constructor signature

查看:72
本文介绍了TypeScript:使用调用签名和构造函数签名实现接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以创建一个实现以下接口的对象:

Is it possible create an object that implements the following interface:

interface I {
    (): string;
    new(): any;
}

我看到可以实现具有调用签名和来自这个问题的一些字段的接口:使用裸函数签名和其他字段实现 TypeScript 接口

I saw that it is possible to implement an interface having a call signature and some fields from this question: Implementing TypeScript interface with bare function signature plus other fields

推荐答案

环顾四周,很明显一个对象可以同时实现具有构造函数签名和调用签名的接口:

After looking around, it is clearly possible for an object to implement the an interface with both a constructor signature and a call signature:

https://github.com/Microsoft/TypeScript/blob/master/lib/lib.d.ts#L142 就是一个例子.

底层函数似乎是相同的,尽管以两种不同的方式调用:

The underlying function seems to be the same, albeit called in two different ways:

new Object(3);

Object(3);

这篇关于TypeScript:使用调用签名和构造函数签名实现接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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