125. 对象的嵌套属性类型
困难
get
函数 是 Lodash 中一个非常方便的工具,用于访问 JavaScript 中的嵌套值。然而,当我们在 TypeScript 中使用类似的函数时,往往会丢失类型信息。随着 TypeScript 4.1 版本推出的 模板字面量类型 特性,现在可以实现正确地为 get
函数添加类型。
你能实现这个功能吗?
例如:
type Data = {
foo: {
bar: {
value: 'foobar',
count: 6,
},
included: true,
},
hello: 'world'
}
type A = Get<Data, 'hello'> // expected to be 'world'
type B = Get<Data, 'foo.bar.count'> // expected to be 6
type C = Get<Data, 'foo.bar'> // expected to be { value: 'foobar', count: 6 }
在这个挑战中,不需要考虑数组的访问。