Toggle navigation
IT人
IT人
什麼是 constructor signature in interface
i042416
發表於
2022-03-25
介面中的 `constructor signature` 不能在類中實現; 它們僅用於定義定義 `newable` 的現有 JS API. 下面是一個例子: ```typescript interface ComesFromString { name: string; } 意思是這個介面代表一個可以使用 `new` 操作符操作的物件。返回的型別是 ComesFromString interface StringConstructable { new(n: string): ComesFromString; } class MadeFromString implements ComesFromString { constructor (public name: string) { console.log('ctor invoked'); } } // 下面函式定義了一個工廠方法。工廠方法的輸入就是之前定義的 constructor signature function makeObj(n: StringConstructable) { return new n('hello!'); } console.log(makeObj(MadeFromString).name); ``` 執行結果:  以上例子實際上為 `makeObj` 的函式呼叫建立了一個 `constraint`,傳入的輸入引數必須可以被 new 操作施加,並且建構函式僅包含一個輸入引數,型別為 `string`. 下列程式碼會引起編譯錯誤: ```typescript 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`. 例子: ```typescript interface ClassicInterface { // old school interface like in C#/Java method1():string; methodN():string; } interface Factory { //knows how to construct an object // NOTE: pay attention to the return type new (myNumberParam: number, myStringParam: string): ClassicInterface } class MyImplementation implements ClassicInterface { // The constructor looks like the signature described in Factory constructor(num: number, s: string) { console.log('in myImplementation:', num, s); } // obviously returns an instance of ClassicInterface method1() { return '1'; } methodN() { return '2'; } } class MyOtherImplementation implements ClassicInterface { // The constructor looks like the signature described in Factory constructor(n: number, s: string) { console.log('in myOtherImplementation:', n, s); } // obviously returns an instance of ClassicInterface method1() { 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'); ``` 輸出: 
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/24475491/viewspace-2884140/,如需轉載,請註明出處,否則將追究法律責任。
最新文章
Go 為什麼不支援字首自增運算子?
騰訊全面上雲背後:程式設計師的技術焦慮和技術理想
抓包整理外篇fiddler————瞭解工具欄[一]
簡單ELK配置實現生產級別的日誌採集和查詢實踐
2021年Q1-2022年Q1小米集團資產及負債率(附原資料表)
2021年12月-2022年5月中國國產品牌手機出貨量佔比(附原資料表)
2020年Q3-2022年Q1京東主要營業支出型別及規模(附原資料表)
Counterpoint:2022年全球伺服器市場達到1117億美元 同比增長17%
告別單調,Django後臺主頁改造 - 使用AdminLTE元件
ShardingSphere-proxy-5.0.0建立mysql讀寫分離的連線(六)
面試官:哥們Go語言的互斥鎖瞭解到什麼程度?
如何解決 Iterative 半監督訓練 在 ASR 訓練中難以落地的問題丨RTC Dev Meetup