Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
40
rated 0 times [  47] [ 7]  / answers: 1 / hits: 92420  / 6 Years ago, tue, march 27, 2018, 12:00:00

I am learning Angular (which is written in TypeScript) and I stumbled upon this error:



Class 'SnackbarService' incorrectly extends base class 'MatSnackBar'.
Types have separate declarations of a private property '_overlay'.



when trying to extend MatSnackBar from @angular/material.



This is my code:



import { MatSnackBar } from '@angular/material';
import { Overlay } from '@angular/cdk/overlay';
import { LiveAnnouncer } from '@angular/cdk/a11y';
...

export class SnackbarService extends MatSnackBar {

constructor(
private _overlay: Overlay,
private _liveAnnouncer: LiveAnnouncer,
...
) {
super(_overlay, _liveAnnouncer, ...);
}
}
}


Any help with any type of explanation on why this happens would be really be appreciated.


More From » typescript

 Answers
84

This happens because by declaring the constructor as taking a private _overlay parameter, you have created your own _overlay, but that is already defined in the base class MatSnackBar.



Remove the private part from the declaration and inherit it from the base class. Do the same for the other constructor parameters.



export class SnackbarService extends MatSnackBar{

constructor(
_overlay: Overlay,
_liveAnnouncer: LiveAnnouncer,
...
) {
super(_overlay, _liveAnnouncer, ...);
}
}
}


You can still access them via this.


[#54836] Sunday, March 25, 2018, 6 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
lesli

Total Points: 348
Total Questions: 105
Total Answers: 119

Location: United States Minor Outlying Island
Member since Fri, Jan 6, 2023
1 Year ago
;