I am currently working on creating an array that can have two different value types:
type Loading = {
loading: true
}
type Loaded = {
loading: false,
date: string,
value: number,
}
type Items = Loading | Loaded
const items: Items[] = []
Everything is going smoothly so far, but now I want to retrieve a value using the .find
method:
const firstLoaded = items.find(({ loading }) => !loading)
The variable firstLoaded
should be of type Loaded|undefined
- how can I specify this in TypeScript?
console.log(firstLoaded?.date) // doesn't work
Another question arises: how can I change a value from Loading to Loaded within the array?
const firstUnloaded = items.find(({ loading }) => loading)
firstUnloaded.loading = false // doesn't work