My Angular application includes an object that mocks out an ActivatedRouteSnapshot
. I've fully mocked out all the properties in this object. In order to create another mock object, I'm spreading the first object's properties into the second one.
export class AddEditResolverMocks {
static routeWithId: ActivatedRouteSnapshot = {
params: { id: 123 },
data: { entity: EntityTypes.PayBillCycle },
url: [],
queryParams: {},
fragment: '',
outlet: '',
component: '',
routeConfig: null,
root: new ActivatedRouteSnapshot(),
parent: new ActivatedRouteSnapshot(),
firstChild: new ActivatedRouteSnapshot(),
children: [],
pathFromRoot: [],
paramMap: null,
queryParamMap: null,
};
static routeWithoutId: ActivatedRouteSnapshot = {
...this.routeWithId,
};
}
I initially expected all the properties to be brought over, but Visual Studio Code is showing a complaint:
Type '{
url: UrlSegment[];
params: Params;
queryParams: Params;
fragment: string;
data: Data;
outlet: string;
component: string | Type<any>;
routeConfig: Route;
}' is missing the following properties from type 'ActivatedRouteSnapshot':
- root,
- parent,
- firstChild,
- children,
- and 3 more.ts(2740)
After letting Visual Studio Code refactor it, the "missing" properties are added and the error disappears. So why doesn't it work without explicit initialization? (I feel like I might be missing something obvious)
I'm also surprised that Visual Studio Code allows a this
reference in a static initializer.
UPDATE:
Changing this
to AddEditResolverMocks
:
static routeWithoutId: ActivatedRouteSnapshot = {
...AddEditResolverMocks.routeWithId,
};
had no effect.
UPDATE 2: I have now included the definition of ActivatedRouteSnapshot in the description as per a comment recommendation.