接口中的 constructor signature 不能在类中实现; 它们仅用于定义定义 newable 的现有 JS API. 下面是一个例子:

interface ComesFromString {name: string;
}意思是这个接口代表一个可以使用 `new` 操作符操作的对象。返回的类型是 ComesFromStringinterface StringConstructable {new(n: string): ComesFromString;
}class MadeFromString implements ComesFromString {constructor (public name: string) {console.log('ctor invoked');}
}// 下面函数定义了一个工厂方法。工厂方法的输入就是之前定义的 constructor signaturefunction makeObj(n: StringConstructable) {return new n('hello!');
}console.log(makeObj(MadeFromString).name);

执行结果:

以上例子实际上为 makeObj 的函数调用创建了一个 constraint,传入的输入参数必须可以被 new 操作施加,并且构造函数仅包含一个输入参数,类型为 string.

下列代码会引起编译错误:

class Other implements ComesFromString {constructor (public name: string, count: number) {}
}makeObj(Other);

Argument of type ‘typeof Other’ is not assignable to parameter of type ‘StringConstructable’.(2345)

添加问号将其设置为 optional 参数后,问题消失:

具有构造签名的接口并不意味着由任何类实现(乍一看,这对于一些具有 C#/Java 背景的开发人员来说可能看起来很奇怪,但这确实是另一种不同的设计思路)。

暂时把它想象成一个带有调用签名的接口(就像Java世界中的@FunctionalInterface)。 它的目的是描述一种函数类型。所描述的签名应该由函数对象满足。但不仅仅是任何高级函数或方法。 它应该是一个知道如何构造对象的函数,一个在使用 new 关键字时被调用的函数。

因此,带有构造签名的接口定义了构造函数的签名。如上图我举过的例子,makeObj 函数只接受构造函数仅仅包含唯一一个输入参数且数据类型为 string.

例子:

interface ClassicInterface { // old school interface like in C#/Javamethod1():string;methodN():string;
}interface Factory { //knows how to construct an object// NOTE: pay attention to the return typenew (myNumberParam: number, myStringParam: string): ClassicInterface
}class MyImplementation implements ClassicInterface {// The constructor looks like the signature described in Factoryconstructor(num: number, s: string) { console.log('in myImplementation:', num, s);} // obviously returns an instance of ClassicInterfacemethod1() {return '1';}methodN() {return '2';}
}class MyOtherImplementation implements ClassicInterface {// The constructor looks like the signature described in Factoryconstructor(n: number, s: string) { console.log('in myOtherImplementation:', n, s);} // obviously returns an instance of ClassicInterfacemethod1() {return '3';}methodN() {return '4';}
}// And here is the polymorphism of construction
function instantiateClassicInterface(ctor: Factory, myNumberParam: number, myStringParam: string): ClassicInterface {return new ctor(myNumberParam, myStringParam);
}// And this is how we do it
let iWantTheFirstImpl = instantiateClassicInterface(MyImplementation, 3.14, "smile");
let iWantTheSecondImpl = instantiateClassicInterface(MyOtherImplementation, 42, "vafli");
console.log('done');

输出:

什么是 constructor signature in interface相关推荐

  1. TypeScript constructor signature 类型的变量赋值方式

    看这段代码: interface ArrayContaining {new (sample: string): any; } 试图给一个类型为 ArrayContaining 的变量赋值,下列这样赋值 ...

  2. 什么是 TypeScript 里的 Constructor signature

    TypeScript 官方文档里关于 Constructor signature 只有这短短的一段话: JavaScript functions can also be invoked with th ...

  3. No primary or default constructor found for interface java.util.List

    https://blog.csdn.net/qq_39723363/article/details/84379685 springboot No primary or default construc ...

  4. No primary or default constructor found for interface javax.servlet.http.HttpServletResponse

    在我们使用spring-boot-starter-web依赖的时候,我们可以在controller的接口类的方法参数中使用HttpServletResponse或HttpServletRequest进 ...

  5. SpringBoot 提示:java.lang.IllegalStateException: No primary or default constructor found for interface

    SpringBoot集成MyBatis-Plus 实现HTPP POST提交实体对象提示如下错误片段: c.c.c.c.a.BaseControllerExceptionHandler : 运行时异常 ...

  6. java.lang.IllegalStateException: No primary or default constructor found for interface java.util

    java.lang.IllegalStateException: No primary or default constructor found for interface java.util.Lis ...

  7. 关于 TypeScript 内 constructor signature 的一些失败尝试

    interface ArrayContaining {//new (sample: any[]): any;jasmineToString(): string;}class Jerry{jasmine ...

  8. No primary or single public constructor found for interface java.util.List - and no default construc

    @RequestMapping("/batchDeleteProduct")public TmyResult batchDeleteProduct( List<Integer ...

  9. 40.lombok在IntelliJ IDEA下的使用

    转自:https://www.cnblogs.com/yjmyzz/p/lombok-with-intellij-idea.html lombok是一款可以精减java代码.提升开发人员生产效率的辅助 ...

最新文章

  1. linux学习笔记之--vim 程序编辑器
  2. easyui中combobox 验证输入的值必须为选项框中的数据
  3. opencv环境搭建
  4. [Web]Restful风格的适用场景
  5. 【CCCC】L3-021 神坛 (30分)计算几何+求三角形面积(极角排序)
  6. linux exec操作文件描述符
  7. 生态系统服务——水源涵养水源涵养分布数据
  8. c语言编程数字炸弹,C语言实现数字炸弹小游戏
  9. python车辆型号识别_基于Tensorflow的车辆检测和车型识别
  10. 北理计算机2017,←2017 计算机学院 2019→
  11. 实用是计算机知识,实用电脑知识
  12. 《重学 Java 设计模式》PDF 出炉了 - 小傅哥,肝了50天写出18万字271页的实战编程资料
  13. 图扑软件 | 数字孪生智慧选煤厂
  14. 做SEO优化第十二步:网站优化推广方案
  15. 初学Linux时用到的一点复制,粘贴,删除,创建指令
  16. How to Rerun Failed Tests in JUnit?
  17. SecureCRT 注册机使用方法
  18. 岑村小学辅导班老师体会有感
  19. Docker拉取Solace pubsub+镜像timeout的问题
  20. python-字符串连接

热门文章

  1. 工作单元php,PHP面向对象中的工作单元是什么
  2. 面部识别公司深网视界泄露数百万人信息
  3. SECD machine
  4. wkhtmltopd 下载安装以及php环境下的使用
  5. JavaScript强化教程 —— JavaScript 总结
  6. 在项目中让Ajax面向对象 (二)
  7. idea中隐藏.idea文件夹和.iml文件
  8. golang中创建logger时候踩过的坑
  9. 【Task5(2天)】模型调参
  10. 解决 【xshell 5 不能使用退格键和Delete建】的问题