Using typescript 3, in angular 7 framework.
I'm trying to prepare my local Store array elements for firebase, using an object's Id property as it's key in a wrapping object, and vise versa when fetching from firestore.
I've set up an interface that should work with any of my local Store elements, as they all have an id property:
interface localItem {id:string, [x:string]:any}
and a firebase equivalent, as they all use the id as key
interface firebaseItem {[id:string]:{[x:string]:any}}
I'm trying to iterate the firebase item, and turning it into an array again but i'm getting a type error:
unwrapForLocalStore<T extends localItem>(firebaseItems:firebaseItem):T[]{
let itemArray: T[] = [];
for(let item in firebaseItems){
let newItem:T = {id:item, ...firebaseItems[item]}
/*
[ts] Type '{ id: string; }' is not assignable to type 'T'. [2322]
let newItem: T extends localItem
*/
itemArray.push(newItem)
}
return itemArray
}
I've read the typescript docs, and unless i'm missing something I don't see why the object literal I create is not assignable to type T. I know I can typecase on my object literal, which would resolve it, but I don't understand why I need it. Typescript has all the information to find the types match.
What's the reason it is not recognised in this instance?