如何修复TypeError:fiatService.findById不是函数

时间:2019-07-19 09:18:01

标签: typescript generics inheritance

我有服务的继承结构,我喜欢调用父类的函数。

import { BehaviorSubject } from "rxjs";

import { Model } from "../../model/interface/model";

export abstract class Service<M extends Model> {

protected _list: BehaviorSubject<M[]> = new BehaviorSubject<M[]>([]);

public destroy(): void {
    // clear the list
    this._list = new BehaviorSubject<M[]>([]);
}

public constructor() {
}

public updateItem(m: M, id: string = m.getId()): void {
    const currentList = this._list.value;
    currentList[id] = m;
    this._list.next({...currentList});
}

public upsert(m: M): void {
    this._list.next({...this._list.value, ...m});
}

public hasItem(m: M): boolean {
    let hasThisItem = false;
    this._list.forEach(array => {
        array.forEach(item => {
            if(item === m){
                hasThisItem = true;
            }
        });
    });
    return hasThisItem;
}

public addItem(m: M): void {
    this._list.next([m]);
}

public getItemById(id: string): M {
    return this.list[id];
}

public getList(): BehaviorSubject<M[]> {
    return this._list;
}

public get list(): M[] {
    return this._list.value;
}

public set list(m: M[]) {
    this._list.next([...this._list.value, ...m]);
}

public deleteItem(m: M): void {
    let currArr = this._list.value;
    currArr.forEach((item, index) => {
        if(item === m) {
            currArr.splice(index, 1);
        }
    })
    this._list.next(currArr);
}
}

CurrencyService类别:

import { Service } from "./service";
import { Currency } from '../../model/interface/currency';

export abstract class CurrencyService<C extends Currency> extends Service<C> {

public constructor() {
    super();
    this.findById = this.findById.bind(this);
}


public findById(s: string): C {
    let returnValue: C;
    let currList = this._list.value;

    Object.keys(currList).forEach(function(key,index) {

        console.log(currList);
        if(currList[key].getId() === s){
            returnValue = currList[key];

        }
    });
    return returnValue;
}



}

法定服务等级:

import { CurrencyService } from './interface/currency.service';
import { Currency } from '../model/interface/currency';

export class FiatService<Fiat extends Currency> extends CurrencyService<Fiat> {
    constructor(){
        super();
    }
}

和test.spec.ts:

import { COINS } from '../static_model/cryptocoins';
import { FIATS } from '../static_model/fiats';
import { METALS } from '../static_model/metals';
import { Currency } from './model/interface/currency';

import { FiatService } from './service/fiat.service';


    // create FiatService
    let fiatService = new FiatService<Currency>();

    let fiat = new Fiat("1", "EUR", "Euro");

    fiatService.upsert(fiat);

    fiatService.findById('1');

在这里我得到:  TypeError:fiatService.findById不是函数

  25 | 
  26 |     console.log('content: ', fiatService.list);
> 27 |     fiatService.findById('1');

我希望该功能正常工作,但它告诉我这不是一个功能。也许我在继承方面做错了什么,但我不明白为什么不能将其识别为函数。

1 个答案:

答案 0 :(得分:0)

我找到了障碍。测试编译器使用的是在我的findById()函数出现之前生成的js文件。