Content pfp
Content
@
0 reply
0 recast
2 reactions

Darryl Yeo 🛠️ pfp
Darryl Yeo 🛠️
@darrylyeo
Any TypeScript gurus in the house who can help me troubleshoot this? What am I fundamentally misunderstanding about type parameters / conditionals here?
8 replies
1 recast
11 reactions

Darryl Yeo 🛠️ pfp
Darryl Yeo 🛠️
@darrylyeo
```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

blaynem pfp
blaynem
@drilkmops
This looks lowkey disgusting and reallllly hacky. What if we take a step back. What exactly are you trying to do? Making a super generic that allows you to transform from ANYTHING to ANYTHING doesn’t feel worth creating.
2 replies
0 recast
1 reaction

blaynem pfp
blaynem
@drilkmops
Like I see you have in another post, I’d use TypeGuards and try to not spread the object conditionally. Be explicit. It will save future you from headaches. :)
1 reply
0 recast
1 reaction

Darryl Yeo 🛠️ pfp
Darryl Yeo 🛠️
@darrylyeo
I have multiple variants of a thing, and I want to ensure the information unique to any given variant correctly persists across a transform operation. It's not that wild of a use case, really – it's just that TypeScript has some notoriously obtuse type-level logical operators that are hard to grok at first.
0 reply
0 recast
0 reaction