#Optional to null or T

41 messages · Page 1 of 1 (latest)

inland musk
#

Hi!
I am looking for something like this

export type OptionalToUndefined<T> = {
  [K in keyof Required<T>]: T[K];
};

But instead of turning
x?: string -> x: undefined | string

I need
x?: string -> string | null

glacial needle
#
export type OptionalToUndefined<T> = {
  [K in keyof Required<T>]: Exclude<T[K], undefined> | null;
};

type T0 = OptionalToUndefined<{ x?: string; }>
#

!ts T0

distant daggerBOT
#
type T0 = {
    x: string | null;
} /* 5:6 */```
inland musk
#

wouldn't that make all props T | null ?

#

yep

orchid smelt
#

Hm wait that would also remove legit undefineds

inland musk
#

yeah but i failed to mention that i do not care abt those

#

x?: string | undefined --> x: string | null

distant daggerBOT
#
angryzor#0

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
...```

inland musk
#

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

orchid smelt
#

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

inland musk
#

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

distant daggerBOT
#
zweieuro#0

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
...```

inland musk
#

why does t1 hover show a different type for b and c.a ?

#

logically it should be the same, but its confusing

orchid smelt
#

I don't get it

inland musk
#

one sec ima make a sc

orchid smelt
#

they have the same type in my browser, disregarding the boolean vs string

inland musk
#

you'need to pause it

orchid smelt
#

huh

#

that is curious indeed

inland musk
#

how do i even google this ? XD

orchid smelt
#

when you hover over c it does expand the undefined

#

it's like it doesn't go deeper

inland musk
#

yeah, like it does only L1 resolve

#

I have no plugins for typescript in vscode, just jest

orchid smelt
#

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

inland musk
#

that's super strange imo

#

weirdly inconsistent

orchid smelt
#

Well TS tries to delay recursing into properties as much as possible

#

In order to support recursive types without going into an infinite recursion

orchid smelt
#

!resolved

distant daggerBOT
#

@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.