我是打字稿的新手。因此有人可以描述我如何以編程方式實作這種型別的深層嵌套物件:
type typeDeepNestedGeneric<T> = T
| {[key: string]: T}
| {[key: string]: {[key: string]: T}}
| {[key: string]: {[key: string]: {[key: string]: T}}}
| {[key: string]: {[key: string]: {[key: string]: {[key: string]: T}}}}
| {[key: string]: {[key: string]: {[key: string]: {[key: string]: {[key: string]: T}}}}}
| {[key: string]: {[key: string]: {[key: string]: {[key: string]: {[key: string]: {[key: string]: T}}}}}}
| {[key: string]: {[key: string]: {[key: string]: {[key: string]: {[key: string]: {[key: string]: {[key: string]: T}}}}}}}
在我的類物件中,我想定義具有無限型別深度的物件的屬性,包括:

uj5u.com熱心網友回復:
這滿足嗎?
type DeepNestedGeneric<T> = T | {[key: string]: DeepNestedGeneric<T> }
const x : DeepNestedGeneric<string> = {
foo: "wsdfhsdf",
bar: {
whatever: "akjdhakjs",
baz: {
blerg: "dsfsdsdfsd",
errors: 5 // Type 'number' is not assignable to type 'DeepNestedGeneric<string>'.
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/495750.html
