我正在為 JSON Schema 使用 TS 型別:https : //github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/json-schema/index.d.ts
精簡版:
interface JSONSchema4 {
properties?: {
[k: string]: JSONSchema4;
} | undefined;
}
這種型別是遞回的。我想用自定義屬性擴展這種型別。例如目前,這樣的事情與型別不匹配:
{ instanceof: 'Date' }
我想instanceof?: string作為一個欄位添加到JSONSchema4. 這可能嗎?
uj5u.com熱心網友回復:
在 TypeScript 中,介面是開放的,與封閉的型別不同。因此,您可以簡單地通過宣告一個包含您的關鍵字的同名介面來添加您的關鍵字。該介面有效地擴展了原始型別。
interface JSONSchema4 {
instanceof?: string;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/315122.html
