我有一些元素,其子元素的結構與其父元素相同。當我使用 TypeScript 創建它們的型別時,我遇到了一些錯誤。我該如何解決這個問題?
type RowRecord = Record<string, CellData>;
type CellData = string | number | RowRecord;
錯誤:
- 型別別名“CellData”回圈參考自身。
- 型別別名 'RowRecord' 回圈參考自身。
示例記錄:
{
"id": "60f49dbb3ee1a9241749abb6",
"gender": "female",
"firstName": "Millicent",
"lastName": "Harrington",
"birthDate": "2013-05-14T04:35:49.400Z",
"age": 8,
"email": "[email protected]",
"address": {
"country": "USA",
"state": "Utah",
"city": "Cecilia",
"street": "Newkirk Placez",
"houseNumber": 969
}
}
uj5u.com熱心網友回復:
您可以通過擴展Record到它代表的物件來完成
type RowRecord = { [K in string]: CellData }; // or { [k: string]: CellData }
type CellData = string | number | RowRecord;
TypeScript 不允許你的定義,因為它Record<string, CellData>被急切地評估。如果你給 TS 一個物件型別,那么 TypeScript 就會認識到它可以推遲對該型別的評估,允許遞回定義。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/387159.html
下一篇:不同介面型別
