什么是 constructor signature in interface
接口中的 constructor signature 不能在類中實(shí)現(xiàn); 它們僅用于定義定義 newable 的現(xiàn)有 JS API. 下面是一個(gè)例子:
interface ComesFromString {name: string; }意思是這個(gè)接口代表一個(gè)可以使用 `new` 操作符操作的對(duì)象。返回的類型是 ComesFromStringinterface StringConstructable {new(n: string): ComesFromString; }class MadeFromString implements ComesFromString {constructor (public name: string) {console.log('ctor invoked');} }// 下面函數(shù)定義了一個(gè)工廠方法。工廠方法的輸入就是之前定義的 constructor signaturefunction makeObj(n: StringConstructable) {return new n('hello!'); }console.log(makeObj(MadeFromString).name);執(zhí)行結(jié)果:
以上例子實(shí)際上為 makeObj 的函數(shù)調(diào)用創(chuàng)建了一個(gè) constraint,傳入的輸入?yún)?shù)必須可以被 new 操作施加,并且構(gòu)造函數(shù)僅包含一個(gè)輸入?yún)?shù),類型為 string.
下列代碼會(huì)引起編譯錯(cuò)誤:
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)
添加問(wèn)號(hào)將其設(shè)置為 optional 參數(shù)后,問(wèn)題消失:
具有構(gòu)造簽名的接口并不意味著由任何類實(shí)現(xiàn)(乍一看,這對(duì)于一些具有 C#/Java 背景的開(kāi)發(fā)人員來(lái)說(shuō)可能看起來(lái)很奇怪,但這確實(shí)是另一種不同的設(shè)計(jì)思路)。
暫時(shí)把它想象成一個(gè)帶有調(diào)用簽名的接口(就像Java世界中的@FunctionalInterface)。 它的目的是描述一種函數(shù)類型。所描述的簽名應(yīng)該由函數(shù)對(duì)象滿足。但不僅僅是任何高級(jí)函數(shù)或方法。 它應(yīng)該是一個(gè)知道如何構(gòu)造對(duì)象的函數(shù),一個(gè)在使用 new 關(guān)鍵字時(shí)被調(diào)用的函數(shù)。
因此,帶有構(gòu)造簽名的接口定義了構(gòu)造函數(shù)的簽名。如上圖我舉過(guò)的例子,makeObj 函數(shù)只接受構(gòu)造函數(shù)僅僅包含唯一一個(gè)輸入?yún)?shù)且數(shù)據(jù)類型為 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');輸出:
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的什么是 constructor signature in interface的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: “华为坤灵”子品牌发布:面向分销市场,将
- 下一篇: SAP UI5 应用开发教程之五十七 -