#Optional to null or T
41 messages · Page 1 of 1 (latest)
export type OptionalToUndefined<T> = {
[K in keyof Required<T>]: Exclude<T[K], undefined> | null;
};
type T0 = OptionalToUndefined<{ x?: string; }>
!ts T0
type T0 = {
x: string | null;
} /* 5:6 */```
Hm wait that would also remove legit undefineds
yeah but i failed to mention that i do not care abt those
x?: string | undefined --> x: string | null
Preview:```ts
type UndefinedToNull<T, R> = T extends undefined
? [undefined] extends [R]
? T
: null
: T
export type OptionalToNull<T> = {
[K in keyof Required<T>]: UndefinedToNull<
T[K],
Required<T>[K]
}
type A = OptionalToNull<{
foo: string
bar?: number
baz: string | undefined
...```
my problem stems out of a translatio lib java <-> ts where undef does not exist.
the java side treats null the same as ts treats undefined
Fully safe this way I think
Ah you actually want them out?
Well the code above just takes them out when the original object had the property as optional
and leaves it in if it was required
that seems the most reliable
hm, true
On a bit of a different topic, i chose to turn strictnullchecks on in my project but don't understand this output my vscpde is showing me and i am not even sure how to phrase the question
Preview:```ts
type UndefinedToNull<T, R> = T extends undefined
? [undefined] extends [R]
? T
: null
: T
export type OptionalToNull<T> = {
[K in keyof Required<T>]: UndefinedToNull<
T[K],
Required<T>[K]
}
type A = OptionalToNull<{
foo: string
bar?: number
baz: string | undefined
...```
why does t1 hover show a different type for b and c.a ?
logically it should be the same, but its confusing
I don't get it
one sec ima make a sc
they have the same type in my browser, disregarding the boolean vs string
how do i even google this ? XD
yeah, like it does only L1 resolve
I have no plugins for typescript in vscode, just jest
yeah I think it's just deferring resolution when you try to getTypeOfSymbol on the outer one
but it will have the | undefined present when type checking
Well TS tries to delay recursing into properties as much as possible
In order to support recursive types without going into an infinite recursion
!resolved
@inland musk
Because your issue seemed to be resolved, this post was marked as resolved by @orchid smelt.
If your issue is not resolved, you can reopen this post by running !reopen.
If you have a different question, make a new post in #1057653400046674112.