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
Ideally I'd just use TypeScript's inline control flow logic because making a type guard function for every possible variant I need would get repetitive. This looks like a promising step in the right direction though: https://warpcast.com/darrylyeo/0x326763ab https://warpcast.com/darrylyeo/0x12b1a935
0 reply
0 recast
0 reaction