多级索引类型Typescript

多级索引类型Typescript,typescript,Typescript,大家好,我想知道是否有人能帮我 我有一个函数,它将对象的每一层作为一个带点分隔符的字符串 乙二醇 因此,如果我想得到test2的值,我会将函数编写为 foo('test.test1.test2') 如果我采用以下type fooTypeProps=keyof-typeof-fooType 它给了我一个test | testing | bottom 我想知道在typescript中是否有一种方法可以获得该键,但是对于所有的布局,因此在这种情况下,我将有一个test.test1.test2 | te

大家好,我想知道是否有人能帮我

我有一个函数,它将对象的每一层作为一个带点分隔符的字符串

乙二醇

因此,如果我想得到test2的值,我会将函数编写为

foo('test.test1.test2')

如果我采用以下
type fooTypeProps=keyof-typeof-fooType

它给了我一个
test | testing | bottom

我想知道在typescript中是否有一种方法可以获得该键,但是对于所有的布局,因此在这种情况下,我将有一个
test.test1.test2 | testing.testable | bottom

的并集,您可以使用您的类型并获得表示路径树叶子的元组并集:

type AllLeaves = Leaves<typeof fooType>
/* type AllLeaves = ["test", "test1", "test2"] | ["testing", "testable"] | ["bottom"] */
type AllLeaves=Leaves
/*键入AllLeaves=[“test”、“test1”、“test2”]|[“testing”、“testable”]|[“bottom”]*/


但是您不能在类型系统中将它们转换为虚线字符串,因为它没有字符串文字操作的概念。

没有类型级别的字符串操作,因此编译器无法将
“test”
“test1”
转换为
“test.test1”
。你能得到的最好的元组是
[“test”,“test1”,“test2”]|[“testing”,“testable”]|[“bottom”]
type AllLeaves = Leaves<typeof fooType>
/* type AllLeaves = ["test", "test1", "test2"] | ["testing", "testable"] | ["bottom"] */