Justin Young Justin Young - 1 month ago 8
TypeScript Question

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');

}));


});

Answer

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'])