admin管理员组文章数量:1023815
How to transform union of similar objects to object type using TypeScript typings?
Input
type I = {
key: 'foo',
value: Foo,
} | {
key: 'bar',
value: Bar,
};
Output
type O = {
foo: Foo,
bar: Bar,
};
I am not sure it is possible. But who knows? Note that the task is not a duplicate to that.
How to transform union of similar objects to object type using TypeScript typings?
Input
type I = {
key: 'foo',
value: Foo,
} | {
key: 'bar',
value: Bar,
};
Output
type O = {
foo: Foo,
bar: Bar,
};
I am not sure it is possible. But who knows? Note that the task is not a duplicate to that.
Share Improve this question edited Dec 20, 2020 at 1:18 jcalz 333k29 gold badges443 silver badges442 bronze badges asked Dec 19, 2020 at 23:52 avdotionavdotion 1371 silver badge10 bronze badges 2- 3 That's not an intersection. – Jared Smith Commented Dec 20, 2020 at 0:01
-
It's not clear what the algorithm would even be to get from your "input" to your "output" unless you had a type that happened to be a union of a bunch of types with key properties of type string and value properties of various types. And how would you use this output? You want an expression that produces
O
fromI
? How does that help you?O
is not that hard to type into your code -- you did it above. So yeah, see whether you can improve or clarify the question, I'd say. – David P. Caldwell Commented Dec 20, 2020 at 0:07
1 Answer
Reset to default 6In TypeScript 4.1 this is a straightforward mapped type with key remapping. You can iterate over each member T
of the I
union (it's a union, not an intersection) and look up the key/value types: T['key']
for the key and T['value']
for the value. Like this:
type O = { [T in I as T['key']]: T['value'] };
/* type O = {
foo: Foo;
bar: Bar;
} */
Playground link to code
How to transform union of similar objects to object type using TypeScript typings?
Input
type I = {
key: 'foo',
value: Foo,
} | {
key: 'bar',
value: Bar,
};
Output
type O = {
foo: Foo,
bar: Bar,
};
I am not sure it is possible. But who knows? Note that the task is not a duplicate to that.
How to transform union of similar objects to object type using TypeScript typings?
Input
type I = {
key: 'foo',
value: Foo,
} | {
key: 'bar',
value: Bar,
};
Output
type O = {
foo: Foo,
bar: Bar,
};
I am not sure it is possible. But who knows? Note that the task is not a duplicate to that.
Share Improve this question edited Dec 20, 2020 at 1:18 jcalz 333k29 gold badges443 silver badges442 bronze badges asked Dec 19, 2020 at 23:52 avdotionavdotion 1371 silver badge10 bronze badges 2- 3 That's not an intersection. – Jared Smith Commented Dec 20, 2020 at 0:01
-
It's not clear what the algorithm would even be to get from your "input" to your "output" unless you had a type that happened to be a union of a bunch of types with key properties of type string and value properties of various types. And how would you use this output? You want an expression that produces
O
fromI
? How does that help you?O
is not that hard to type into your code -- you did it above. So yeah, see whether you can improve or clarify the question, I'd say. – David P. Caldwell Commented Dec 20, 2020 at 0:07
1 Answer
Reset to default 6In TypeScript 4.1 this is a straightforward mapped type with key remapping. You can iterate over each member T
of the I
union (it's a union, not an intersection) and look up the key/value types: T['key']
for the key and T['value']
for the value. Like this:
type O = { [T in I as T['key']]: T['value'] };
/* type O = {
foo: Foo;
bar: Bar;
} */
Playground link to code
本文标签: javascriptTypescript Convert union of similar objects to object typeStack Overflow
版权声明:本文标题:javascript - Typescript: Convert union of similar objects to object type - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745571342a2156755.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论