简体   繁体   中英

How to fix `Argument of type number is not assignable to string`?

New to angular 2 & typescript. Trying to test some pipes. I keep getting this error in my tests:

ERROR in [default] .../inflection.pipe.spec.ts:22:47 Argument of type 'number' is not assignable to parameter of type 'string[]'.

Any idea of what I'm doing wrong?

//pipe.ts

import { .... }

    @Pipe({name: 'inflection'})
    export class InflectionPipe implements PipeTransform {
    transform(value: string, args: string[]): any {
        console.log(args);
        return inflection.inflect(value, args)

  }
}

//spec.ts
import {....}

describe('InflectionPipe', () => {
    // provide our implementations or mocks to the dependency injector
    beforeEach(() => TestBed.configureTestingModule({
        providers: [
            InflectionPipe
        ]
    }));

    it('should inflect different values', inject([InflectionPipe], (inflectionPipe: InflectionPipe) => {

        expect(inflectionPipe.transform('goose', 2)).toEqual('geese');

    }));


});

Look at your transform signature

transform(value: string, args: string[])

and look how you are trying to call it

inflectionPipe.transform('goose', 2)

It expects a string[] but you are passing a number. Not sure what you are trying to do, but you should fix it accordingly. Maybe transform('goose', ['2'])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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