0

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?

toms
  • 515
  • 6
  • 23
  • Just make a Stack Blitz demo. It will help us to better understand the situation. – AlokeT Feb 20 '19 at 11:42
  • 1
    I think [this](https://github.com/Microsoft/TypeScript/issues/29952) is a similar case. –  Feb 21 '19 at 22:20
  • @joseph-climber YES it is. That does explain the issue and the reason for it. Do you want to post it as an answer for future typescripters? – toms Feb 22 '19 at 10:21

0 Answers0