#Optional chaining in typescript?

6 messages · Page 1 of 1 (latest)

echo bluff
#

Hello there, how do I fix the following:

type Config = {
    time: string;
} | undefined;

type time = Config["time"]; // Property 'time' does not exist on type 'Config'.(2339)

I think it can be done with conditional types or smth?

quartz pineBOT
#

type Config = {
    time: string;
} | undefined;

type time = NonNullable<Config>["time"];
stone hedge
#

that does it

#

but yes, you could do it using conditional types

tropic mural
#
type Config = {
    time: string;
} | undefined;

type Time<T extends Config> = T extends NonNullable<Config> ? T["time"] : undefined;
#

depends what they want the final result to be