fix(router): update type for routerLink to include null and undefined (#37018)

PR #13380 added support for `null` and `undefined` but the type on the parameter was not updated.
This would result in a compilation error if `fullTemplateTypeCheck` is enabled.
Fixes #36544

PR Close #37018
This commit is contained in:
Andrew Scott 2020-05-08 10:55:40 -07:00 committed by Kara Erickson
parent 784cf46a93
commit 7de7871dd9
3 changed files with 17 additions and 11 deletions

View File

@ -380,7 +380,7 @@ export declare class RouterLink {
}; };
queryParamsHandling: QueryParamsHandling; queryParamsHandling: QueryParamsHandling;
replaceUrl: boolean; replaceUrl: boolean;
set routerLink(commands: any[] | string); set routerLink(commands: any[] | string | null | undefined);
skipLocationChange: boolean; skipLocationChange: boolean;
state?: { state?: {
[k: string]: any; [k: string]: any;
@ -414,7 +414,7 @@ export declare class RouterLinkWithHref implements OnChanges, OnDestroy {
}; };
queryParamsHandling: QueryParamsHandling; queryParamsHandling: QueryParamsHandling;
replaceUrl: boolean; replaceUrl: boolean;
set routerLink(commands: any[] | string); set routerLink(commands: any[] | string | null | undefined);
skipLocationChange: boolean; skipLocationChange: boolean;
state?: { state?: {
[k: string]: any; [k: string]: any;

View File

@ -139,7 +139,7 @@ export class RouterLink {
} }
@Input() @Input()
set routerLink(commands: any[]|string) { set routerLink(commands: any[]|string|null|undefined) {
if (commands != null) { if (commands != null) {
this.commands = Array.isArray(commands) ? commands : [commands]; this.commands = Array.isArray(commands) ? commands : [commands];
} else { } else {
@ -229,7 +229,7 @@ export class RouterLinkWithHref implements OnChanges, OnDestroy {
} }
@Input() @Input()
set routerLink(commands: any[]|string) { set routerLink(commands: any[]|string|null|undefined) {
if (commands != null) { if (commands != null) {
this.commands = Array.isArray(commands) ? commands : [commands]; this.commands = Array.isArray(commands) ? commands : [commands];
} else { } else {

View File

@ -1821,11 +1821,15 @@ describe('Integration', () => {
expect(native.getAttribute('href')).toEqual('/home'); expect(native.getAttribute('href')).toEqual('/home');
})); }));
it('should not throw when commands is null', fakeAsync(() => { it('should not throw when commands is null or undefined', fakeAsync(() => {
@Component({ @Component({
selector: 'someCmp', selector: 'someCmp',
template: template: `<router-outlet></router-outlet>
`<router-outlet></router-outlet><a [routerLink]="null">Link</a><button [routerLink]="null">Button</button>` <a [routerLink]="null">Link</a>
<button [routerLink]="null">Button</button>
<a [routerLink]="undefined">Link</a>
<button [routerLink]="undefined">Button</button>
`
}) })
class CmpWithLink { class CmpWithLink {
} }
@ -1835,10 +1839,12 @@ describe('Integration', () => {
let fixture: ComponentFixture<CmpWithLink> = createRoot(router, CmpWithLink); let fixture: ComponentFixture<CmpWithLink> = createRoot(router, CmpWithLink);
router.resetConfig([{path: 'home', component: SimpleCmp}]); router.resetConfig([{path: 'home', component: SimpleCmp}]);
const anchor = fixture.nativeElement.querySelector('a'); const anchors = fixture.nativeElement.querySelectorAll('a');
const button = fixture.nativeElement.querySelector('button'); const buttons = fixture.nativeElement.querySelectorAll('button');
expect(() => anchor.click()).not.toThrow(); expect(() => anchors[0].click()).not.toThrow();
expect(() => button.click()).not.toThrow(); expect(() => anchors[1].click()).not.toThrow();
expect(() => buttons[0].click()).not.toThrow();
expect(() => buttons[1].click()).not.toThrow();
})); }));
it('should update hrefs when query params or fragment change', fakeAsync(() => { it('should update hrefs when query params or fragment change', fakeAsync(() => {