I have these types:
type Exercise = {
type: string
prompt: string
answer: string
}
type ComplexExercise = {
type: string
prompt: string,
subExercises: Exercise[]
}
Im trying to make a component that fetches data from an api and renders it according to the type
<script lang="ts">
let promise = getExercise(params.exerciseId).then((ex) => (exercise = ex));
let exercise: ComplexExercise | Exercise;
</script>
...
{#await promise}
<p>Loading exercise</p>
{:then}
{#if exercise.type !== "COMPLEX"}
<BaseEditor {exercise} />
{:else}
{#each exercise.subExercises as ex}
<BaseEditor {ex} />
{/each}
{/if}
{/await}
I get the following error
Property 'subExercises' does not exist on type Exercise
Casting it throws this error
{#each (exercise as ComplexExercise).subExercises as ex}
^ Unexpected token svelte(parse-error)
Only thing that seems work is setting exercise type to any, I'm avoiding it for the obvious reason. Any help is appreciated