I'm trying to create a class I can use to load data asynchronously. Instances of this class are used in a Pinia store and watched by Vue components to enable automatic UI updates when data is loaded.
However, I get a compile-time error:
Type 'T' is not assignable to type 'UnwrapRef<T> | undefined'
Here's the code:
export class AsyncData<T> {
state: AsyncDataState
data?: T
constructor(request?: Promise<T>) {
if (request) {
this.state = AsyncDataState.Loading
// use reactive proxy to modify state later in case anyone is listening
const reactiveThis = reactive(this)
request.then(d => {
// got it
reactiveThis.state = AsyncDataState.Loaded
reactiveThis.data = d // <= ERROR
},
reason => {
// no dice
reactiveThis.state = (reason == DataError.NotFound ? AsyncDataState.NotFound : AsyncDataState.LoadFailed)
})
}
else {
this.state = AsyncDataState.New
}
}
}
I'm not an expert on the mysteries of Typescript or Vue 3, but it seems to me I should be able to assign a value of type T to the property on a reactive wrapper corresponding to a property of type T.
What am I missing?