Skip to content

Commit e181749

Browse files
committed
fix(materialsnack-bar): Replace any with unknown
1 parent a6eff18 commit e181749

File tree

4 files changed

+36
-20
lines changed

4 files changed

+36
-20
lines changed

src/material/snack-bar/simple-snack-bar.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,17 @@ import {MatSnackBarRef} from './snack-bar-ref';
1212
import {MAT_SNACK_BAR_DATA} from './snack-bar-config';
1313
import {MatSnackBarAction, MatSnackBarActions, MatSnackBarLabel} from './snack-bar-content';
1414

15+
/** Input data for the snack bar. */
16+
export interface TextOnlySnackBarData {
17+
message: string;
18+
action: string;
19+
}
20+
1521
/**
1622
* Interface for a simple snack bar component that has a message and a single action.
1723
*/
1824
export interface TextOnlySnackBar {
19-
data: {message: string; action: string};
25+
data: TextOnlySnackBarData;
2026
snackBarRef: MatSnackBarRef<TextOnlySnackBar>;
2127
action: () => void;
2228
hasAction: boolean;
@@ -36,7 +42,7 @@ export interface TextOnlySnackBar {
3642
})
3743
export class SimpleSnackBar implements TextOnlySnackBar {
3844
snackBarRef = inject<MatSnackBarRef<SimpleSnackBar>>(MatSnackBarRef);
39-
data = inject(MAT_SNACK_BAR_DATA);
45+
data = inject<TextOnlySnackBarData>(MAT_SNACK_BAR_DATA);
4046

4147
constructor(...args: unknown[]);
4248
constructor() {}

src/material/snack-bar/snack-bar-config.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {AriaLivePoliteness} from '@angular/cdk/a11y';
1111
import {Direction} from '@angular/cdk/bidi';
1212

1313
/** Injection token that can be used to access the data that was passed in to a snack bar. */
14-
export const MAT_SNACK_BAR_DATA = new InjectionToken<any>('MatSnackBarData');
14+
export const MAT_SNACK_BAR_DATA = new InjectionToken<unknown>('MatSnackBarData');
1515

1616
/** Possible values for horizontalPosition on MatSnackBarConfig. */
1717
export type MatSnackBarHorizontalPosition = 'start' | 'center' | 'end' | 'left' | 'right';
@@ -22,7 +22,7 @@ export type MatSnackBarVerticalPosition = 'top' | 'bottom';
2222
/**
2323
* Configuration used when opening a snack-bar.
2424
*/
25-
export class MatSnackBarConfig<D = any> {
25+
export class MatSnackBarConfig<D = unknown> {
2626
/** The politeness level for the MatAriaLiveAnnouncer announcement. */
2727
politeness?: AriaLivePoliteness = 'assertive';
2828

src/material/snack-bar/snack-bar.spec.ts

+10-5
Original file line numberDiff line numberDiff line change
@@ -595,10 +595,11 @@ describe('MatSnackBar', () => {
595595
});
596596

597597
it('should be able to inject arbitrary user data', () => {
598+
const data: BurritosNotificationData = {
599+
burritoType: 'Chimichanga',
600+
};
598601
const snackBarRef = snackBar.openFromComponent(BurritosNotification, {
599-
data: {
600-
burritoType: 'Chimichanga',
601-
},
602+
data,
602603
});
603604

604605
expect(snackBarRef.instance.data)
@@ -1025,17 +1026,21 @@ class ComponentWithChildViewContainer {
10251026
`,
10261027
})
10271028
class ComponentWithTemplateRef {
1028-
@ViewChild(TemplateRef) templateRef: TemplateRef<any>;
1029+
@ViewChild(TemplateRef) templateRef: TemplateRef<unknown>;
10291030
localValue: string;
10301031
}
10311032

1033+
interface BurritosNotificationData {
1034+
burritoType: string;
1035+
}
1036+
10321037
/** Simple component for testing ComponentPortal. */
10331038
@Component({
10341039
template: '<p>Burritos are on the way.</p>',
10351040
})
10361041
class BurritosNotification {
10371042
snackBarRef = inject<MatSnackBarRef<BurritosNotification>>(MatSnackBarRef);
1038-
data = inject(MAT_SNACK_BAR_DATA);
1043+
data = inject<BurritosNotificationData>(MAT_SNACK_BAR_DATA);
10391044
}
10401045

10411046
@Component({

src/material/snack-bar/snack-bar.ts

+16-11
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export class MatSnackBar implements OnDestroy {
6363
* If there is a parent snack-bar service, all operations should delegate to that parent
6464
* via `_openedSnackBarRef`.
6565
*/
66-
private _snackBarRefAtThisLevel: MatSnackBarRef<any> | null = null;
66+
private _snackBarRefAtThisLevel: MatSnackBarRef<unknown> | null = null;
6767

6868
/** The component that should be rendered as the snack bar's simple component. */
6969
simpleSnackBarComponent = SimpleSnackBar;
@@ -75,12 +75,12 @@ export class MatSnackBar implements OnDestroy {
7575
handsetCssClass = 'mat-mdc-snack-bar-handset';
7676

7777
/** Reference to the currently opened snackbar at *any* level. */
78-
get _openedSnackBarRef(): MatSnackBarRef<any> | null {
78+
get _openedSnackBarRef(): MatSnackBarRef<unknown> | null {
7979
const parent = this._parentSnackBar;
8080
return parent ? parent._openedSnackBarRef : this._snackBarRefAtThisLevel;
8181
}
8282

83-
set _openedSnackBarRef(value: MatSnackBarRef<any> | null) {
83+
set _openedSnackBarRef(value: MatSnackBarRef<unknown> | null) {
8484
if (this._parentSnackBar) {
8585
this._parentSnackBar._openedSnackBarRef = value;
8686
} else {
@@ -98,11 +98,11 @@ export class MatSnackBar implements OnDestroy {
9898
* @param component Component to be instantiated.
9999
* @param config Extra configuration for the snack bar.
100100
*/
101-
openFromComponent<T, D = any>(
101+
openFromComponent<T, D = unknown>(
102102
component: ComponentType<T>,
103103
config?: MatSnackBarConfig<D>,
104104
): MatSnackBarRef<T> {
105-
return this._attach(component, config) as MatSnackBarRef<T>;
105+
return this._attach(component, config);
106106
}
107107

108108
/**
@@ -113,9 +113,9 @@ export class MatSnackBar implements OnDestroy {
113113
* @param config Extra configuration for the snack bar.
114114
*/
115115
openFromTemplate(
116-
template: TemplateRef<any>,
116+
template: TemplateRef<unknown>,
117117
config?: MatSnackBarConfig,
118-
): MatSnackBarRef<EmbeddedViewRef<any>> {
118+
): MatSnackBarRef<EmbeddedViewRef<unknown>> {
119119
return this._attach(template, config);
120120
}
121121

@@ -187,14 +187,19 @@ export class MatSnackBar implements OnDestroy {
187187
/**
188188
* Places a new component or a template as the content of the snack bar container.
189189
*/
190+
private _attach<T>(content: ComponentType<T>, userConfig?: MatSnackBarConfig): MatSnackBarRef<T>;
191+
private _attach<T>(
192+
content: TemplateRef<T>,
193+
userConfig?: MatSnackBarConfig,
194+
): MatSnackBarRef<EmbeddedViewRef<T>>;
190195
private _attach<T>(
191196
content: ComponentType<T> | TemplateRef<T>,
192197
userConfig?: MatSnackBarConfig,
193-
): MatSnackBarRef<T | EmbeddedViewRef<any>> {
198+
): MatSnackBarRef<T | EmbeddedViewRef<unknown>> {
194199
const config = {...new MatSnackBarConfig(), ...this._defaultConfig, ...userConfig};
195200
const overlayRef = this._createOverlay(config);
196201
const container = this._attachSnackBarContainer(overlayRef, config);
197-
const snackBarRef = new MatSnackBarRef<T | EmbeddedViewRef<any>>(container, overlayRef);
202+
const snackBarRef = new MatSnackBarRef<T | EmbeddedViewRef<unknown>>(container, overlayRef);
198203

199204
if (content instanceof TemplateRef) {
200205
const portal = new TemplatePortal(content, null!, {
@@ -231,11 +236,11 @@ export class MatSnackBar implements OnDestroy {
231236

232237
this._animateSnackBar(snackBarRef, config);
233238
this._openedSnackBarRef = snackBarRef;
234-
return this._openedSnackBarRef;
239+
return snackBarRef;
235240
}
236241

237242
/** Animates the old snack bar out and the new one in. */
238-
private _animateSnackBar(snackBarRef: MatSnackBarRef<any>, config: MatSnackBarConfig) {
243+
private _animateSnackBar(snackBarRef: MatSnackBarRef<unknown>, config: MatSnackBarConfig) {
239244
// When the snackbar is dismissed, clear the reference to it.
240245
snackBarRef.afterDismissed().subscribe(() => {
241246
// Clear the snackbar ref if it hasn't already been replaced by a newer snackbar.

0 commit comments

Comments
 (0)