0 reply
0 recast
2 reactions
8 replies
1 recast
11 reactions
```ts
enum Type {
A = 'A',
B = 'B',
}
type FromThing<T extends Type = Type> = (
& {
type: Type,
}
& (
T extends Type.A ?
{
A: number
}
: T extends Type.B ?
{
B: string
}
:
Record<string, never>
)
)
type ToThing<T extends Type = Type> = (
& {
type: Type,
}
& (
T extends Type.A ?
{
a: number
}
: T extends Type.B ?
{
b: string
}
:
Record<string, never>
)
)
const transform = <
T extends Type = Type
>(
fromThing: FromThing<T>
): (
ToThing<T>
) => ({
type: fromThing.type,
...(
fromThing.type === Type.A && 'A' in fromThing && fromThing.A && {
a: fromThing.A,
}
),
...(
fromThing.type === Type.B && 'B' in fromThing && fromThing.B && {
b: fromThing.B,
}
),
})
``` 3 replies
0 recast
1 reaction
1 reply
0 recast
1 reaction
1 reply
0 recast
0 reaction