TypeScript constructor signature 类型的变量赋值方式
生活随笔
收集整理的這篇文章主要介紹了
TypeScript constructor signature 类型的变量赋值方式
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
看這段代碼:
interface ArrayContaining {new (sample: string): any; }試圖給一個類型為 ArrayContaining 的變量賦值,下列這樣賦值行不通:
const a: ArrayContaining = () => 1;遇到錯誤消息:
Type ‘() => number’ is not assignable to type ‘ArrayContaining’.
Type ‘() => number’ provides no match for the signature ‘new (sample: string): any’.
正確的做法:
class Jerry{constructor(private name:string){this.name = name;console.log('name: ', this.name);} }const b: ArrayContaining = Jerry;const c:Jerry = new b('Tom');最后的輸出:
這里的 Jerry,相當于一個構造器,具有 constructor signature,故可以賦給類型為 ArrayContaing 的 變量 b.
換句話說,TypeScript 的 class 和 constructor 關鍵字,具有所謂的 constructor signature.
總結
以上是生活随笔為你收集整理的TypeScript constructor signature 类型的变量赋值方式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 检测 SAP Spartacus 服务器
- 下一篇: 银行回执单有什么用