简体   繁体   English

打字稿:嵌套函数的泛型类型

[英]Typescript: generics type for nested functions

I have looked for answers to this problem but I can not implement them correctly. 我一直在寻找这个问题的答案,但是我无法正确实现它们。 Maybe you can tell me the right way. 也许您可以告诉我正确的方法。

I'll explain: I want to use typescript generics not to use any more 'Any' in my methods. 我将说明:我想使用打字稿泛型而不在我的方法中使用更多的“任何”。

I am writing the code and what I would like to do. 我正在编写代码以及我想做什么。

IN THE COMPONENT: 在组件中:

this.DatabaseService.getCollection <IUser> ('users', 'lastname'). subscribe (loadData =>
{
    this.listUser = loadData;
});

when I call getCollection () I will call the following nested methods: 当我调用getCollection()时,我将调用以下嵌套方法:

  • getCollectionSnapshot() getCollectionSnapshot()
  • getCollectionRef() getCollectionRef()

I want the type (IUser) passed to getCollection () to be forwarded to the nitidicated methods until you reach getCollectionRef () obtaining a return collection of this type: collection 我希望将传递给getCollection()的类型(IUser)转发给已初始化的方法,直到到达getCollectionRef()以获得该类型的返回集合:collection

FILE SERVICE: database.service.ts 文件服务:database.service.ts

getCollection <T> (path: string, sortBy ?: string): Observable <T []>
{
    // >>>how do I pass the type (T) to getCollectionSnapshot() ? 

    return this.getCollectionSnapshot (path, sortBy) .pipe (
    map (changes =>
    {
        return changes.map (change =>
        {
            const data = change.payload.doc.data ();
            const id = change.payload.doc.id;
            return {id, ... data};
        });
     }
     ));
}


getCollectionSnapshot (path: string, sortBy ?: string): Observable <any []>
{
    return this.getCollectionRef (path, sortBy) .snapshotChanges ();
}



getCollectionRef (path: string, sortBy ?: string): AngularFirestoreCollection
{
    if (sortBy === undefined)
    {
      return this.afs.collection (path);
    }
    else
    {
      return this.afs.collection (path, ref => ref.orderBy (sortBy));
    }
}

Thank you ! 谢谢 ! I hope to fill my gap in generics 我希望填补我在仿制药方面的空白

I am not sure if i understand your problem. 我不确定我是否理解你的问题。 You want to pass the type parameter given for the method getCollection to the methods called within getCollection . 你想传递的方法getCollection到名为内getCollection方法给出的类型参数。 If that's the case would this fit for you: 如果是这样,那么它适合您:

getCollection <T> (path: string, sortBy ?: string): Observable <T []>
{ 

    return this.getCollectionSnapshot<T> (path, sortBy) .pipe (
    map (changes =>
    {
        return changes.map (change =>
        {
            const data = change.payload.doc.data ();
            const id = change.payload.doc.id;
            return {id, ... data};
        });
     }
     ));
}


getCollectionSnapshot<T> (path: string, sortBy ?: string): Observable <any []>
{
    return this.getCollectionRef<T> (path, sortBy) .snapshotChanges ();
}

getCollectionRef<T> (path: string, sortBy ?: string): AngularFirestoreCollection
{
    if (sortBy === undefined)
    {
         return this.afs.collection (path);
    }
    else
    {
         return this.afs.collection (path, ref => ref.orderBy (sortBy));
    }
} 

notice the changes: 注意更改:

getCollectionSnapshot <T> (... getCollectionSnapshot <T> (...

getCollectionRef <T> (... getCollectionRef <T> (...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM