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

Darryl Yeo đŸ› ïž pfp
Darryl Yeo đŸ› ïž
@darrylyeo
https://warpcast.com/darrylyeo/0x12b1a935
0 reply
0 recast
0 reaction

typeof.eth đŸ”” pfp
typeof.eth đŸ””
@typeof.eth
type: Type; should probably be type: T Still not gonna work, though, cause the transform function is gonna assume the returned object has optional a and b keys. You could cast the returned value to ToType<T> (https://tinyurl.com/27735582) or you could do some weird function overloading (https://tinyurl.com/mw2m4nuz)
1 reply
0 recast
1 reaction

greg pfp
greg
@gregfromstl
"jUsT aSk cUrSor"
1 reply
0 recast
1 reaction

matthias pfp
matthias
@iammatthias
:any easy
1 reply
0 recast
2 reactions

Marc BHE Season đŸ–â˜Łïž pfp
Marc BHE Season đŸ–â˜Łïž
@bighamenergy.eth
maybe it's easier to find out with a link from here https://www.typescriptlang.org/play/ but as far as i can read from the images is because is not able to cast the type, possibly because the optionals
1 reply
0 recast
1 reaction

hawaiiđŸŽ©đŸŽ­ pfp
hawaiiđŸŽ©đŸŽ­
@ihawaii.eth
i wish i could help..all my coding experience was back in college for taking CSS and HTML😅
1 reply
0 recast
1 reaction

Blayne pfp
Blayne
@drilkmops
Did you end up figuring it all out, chef?
1 reply
0 recast
1 reaction