See for yourself https://tsplay.dev/m3x4LW
The Playground lets you write TypeScript or JavaScript online in a safe and sharable way.
83 messages · Page 1 of 1 (latest)
See for yourself https://tsplay.dev/m3x4LW
The Playground lets you write TypeScript or JavaScript online in a safe and sharable way.
I'd also appreciate any help making that inner conditional more elegant cause I feel like there must be an easier way to allow nulls than this
😬
they did
!ts
function convert<T extends Record<string, any> | null | undefined>(
object: T,
): T extends Record<string, any>
? {
[key in keyof T]: T[key] extends Date
? Timestamp.AsObject
: T[key] extends Date | null
? Timestamp.AsObject | null
: T[key];
}
: T;
function convert<T extends Record<string, any> | null | undefined>(
object: T,
) {
if (typeof object === 'undefined' || object === null) {
return object;
}
const cloned = {...object};
// ...
return cloned;
}
namespace Timestamp {
export class AsObject {}
}```
magic!
might be a bit more complicated depending on the actual impl
thing is
cloned has wrong type
what on earth is T & ({} | undefined)
T excluding null, i suppose
not sure why that's there
that's not exclusion tho
or not even sure what that is tbh
what is & undefined even
it kinda is
extracting undefined from a union
ok also is it possible to like
"get" the output type based on an input
like all inside the type system
like I mean
let something: convert(someOtherType)
or something like that
huh
Like I mean I could use ReturnType<convert> but that wouldn't adjust for the input generic
wait I got it
ReturnType<typeof convert<typeof someOtherType>>?
yeah
alright thanks
oh one last thing
is it really not possible to simplify this
now that I have to also handle undefined it's going to span a bunch of lines just to handle undefined/null values
*typeof convert?
whoops yeah
i forgot instantiation expressions were a thing
gross
why do you even have something that could be | null | undefined
use a union
that seems like code smell
protobuf reasons
hmm? how exactly?
T[K] extends Timestamp.AsObject | null | undefined ? Date | <nullish in T[K]> : T[K]
where nullish in T[K] could be
Exclude<T[K], Timestamp.AsObject>Extract<T[K], null | undefined>T[K] & (null | undefined)i was thinking
| (null extends ? null : never)
i think this should work tho
would need to be Timestamp.AsObject extends T[K]
but also wouldn't that also handle unions with other values? that wouldn't match the original behavior (although idk what the intended behavior is so eh)
this worked?
wrong order tho
I'm making two functions to go back and forth between the types so yeah
the Date and Timestamp.AsObject are reversed but otherwise it's the same idea as what i have
ah
sounds like you'd want a generic for that
(or maybe just handle all unions like in n_n's)
I did like n_n's
hmm no, generic still seems hella helpful
type Transform<U, From, To = never> = From extends U ? Exclude<U, From> | To : U;
type T = Transform<string | number, number, boolean>;
yeah
since you were confused about the intersection being an "exclude"/"extract" earlier, i think you may just be misunderstanding intersections/unions/Exclude/Extract as a whole
of course
that reply is so relatable tbh