In Angular 2 I have a MyObjectComponent
that shows an array of myObjects
. I get this myObjects
from a MyObjectService
which is invoked by @CanActivate
.
@CanActivate((next: ComponentInstruction, previous: ComponentInstruction) => {
let myObjectService: MyObjectService = //get my ObjectService here....
return new Promise((resolve) => {
myObjectService.getMyObjects()
.subscribe((myObjects) => {
next.params['myObjects'] = myObjects;
resolve(true);
});
})
In my component I get the data via
routerOnActivate(next) {
this.myComponentObjectVariable = next.params.myObjects;
}
This works, but only if the myObjects
that I add to next.params['myObjects']
is a string.
If I add an object I will get a
Typescript error: TS2322: Type 'any[]' is not assignable to type 'string'.
However, the resulting JavaScript works as expected. But I want to get rid of the Typescript error message.
The error results because of the definition of the params object in ComponentInstruction
:
declare class ComponentInstruction {
... some other data ...
params: {
[key: string]: string;
};
}
Of course I can change the class ComponentInstruction
but that would be a bit ugly.
Does anyone have an idea how to get rid of this error message?