-
Hi, With the update the structure of the generated code has changed and we ran in to the following issue when running our code:
we get this error:
How would we safely access episode with the current generated code of Foo? Our generated code is much more complex but this is a simplified version that highlights the issue.
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
You'll need to narrow down the type of To achieve this, you can use the type A = {
episode: string;
id: number;
}
type B = {
id: number;
}
type Foo = A | B;
function test(value: Foo | undefined) {
if (!value) {
return value;
// ^? (parameter) value: undefined
}
if ('episode' in value) {
return value;
// ^? (parameter) value: A
}
return value;
// ^? (parameter) value: B
} |
Beta Was this translation helpful? Give feedback.
You'll need to narrow down the type of
Foo | undefined
which is ultimatelyA | B | undefined
.To achieve this, you can use the
in
operator to narrow down the type.🔗 TypeScript Playground.