#Modifying discriminated union doesn't work when nested in another object

1 messages · Page 1 of 1 (latest)

craggy bloomBOT
#
bnason#1204

Preview:```ts
//
// Step1 is a discriminated union on the phase key
//
type PendingStep1 = {
order: 1;
phase: 'PENDING';
progress: number;
};

type RunningStep1 = {
order: 1;
phase: 'RUNNING';
startedAt: Date;
progress: number;
};

type CompletedStep1 = {
order: 1;
...```

graceful dirge
#
// Shouldn't steps.step1 be a RunningStep1, instead error says it's still a PendingStep1
steps.step1.startedAt;
#

not really

#

thing is, you defined steps as being of type Steps

#

and it will stay that type, unless you give it a new type later on

#

you can tell at line 42, steps is still of type Steps

#

and since type Steps is an union of the more precise steps, you can only access the properties that have in common: order, phase, progress

#

@glacial bear

#

at line 66, you give it type Step1
but the variable then changes type at line 78, to become RunningStep1
after you assigned the phase RUNNING

#

the reason for that difference between those 2 examples is the "level" the change occurs at

#

as you figured

#

also Steps.step1 will always be defined as Step1
which is very broad, accepts any state for the step 1 and won't change

glacial bear
#

Ok I understand what you are saying. Any idea how I can actually accomplish what I'm trying to?