TypeScript 将通用对象从蛇形转换为驼色盒 [英] TypeScript convert generic object from snake to camel case
问题描述
我想编写一个函数,该函数接受一个带有蛇形大小写键的对象并将其转换为带有驼色大小写键的对象.假设我们知道输入对象的类型,但希望解决方案是通用的,那么在 TypeScript 中输入此类函数的最佳方式是什么?
type InputType = {snake_case_key_1:数字,snake_case_key_2:字符串,...}函数snakeToCamelCase(对象:T):U{...}
输入 T
和 U
可以做的最好的工作是什么.
我希望 U
的类型尽可能窄,U
的类型最好基于 T
.>
理想情况下,如果 T
是我的示例 InputType
我希望 U 输入为
<代码>{snakeCaseKey1:数字,snakeCaseKey2:字符串,...}
不幸的是,这样的事情是不可能的.当前形式的 Typescript 不支持类型键转换/映射.
I would like to write a function that accepts an object with snake case keys and converts it to an object with camel case keys. What is the best way such a function could be typed in TypeScript, assuming we know the type of the input object, but want the solution to be generic.
type InputType = {
snake_case_key_1: number,
snake_case_key_2: string,
...
}
function snakeToCamelCase(object: T): U {
...
}
What is the best job that could be done to type T
and U
.
I would like U
to be as narrowly typed as possible, and the type of U
to be based on T
ideally.
Ideally, if T
is my example InputType
I would like U to be typed as
{
snakeCaseKey1: number,
snakeCaseKey2: string,
...
}
Unfortunately, something like this is not possible. Typescript in its current form does not support type keys transformation/mapping.
这篇关于TypeScript 将通用对象从蛇形转换为驼色盒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!