Skip to content

Commit 17c8983

Browse files
devversionjelbourn
authored andcommitted
fix(slider): displayWith function never called with "null" (#16707)
Currently the "displayWith" function for a `MatSlider` is typed in a way that implies that the function is sometimes called with null as value. This is never the case because the `value` property of the slider always returns a number but is just typed as `null`-able because getters and setters cannot have different types. This fixes the type for the `displayWith` function as it will never be called with `null`.
1 parent eedf314 commit 17c8983

File tree

4 files changed

+7
-13
lines changed

4 files changed

+7
-13
lines changed

src/material-examples/material/slider/slider-formatting/slider-formatting-example.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,7 @@ import {Component} from '@angular/core';
99
styleUrls: ['slider-formatting-example.css'],
1010
})
1111
export class SliderFormattingExample {
12-
formatLabel(value: number | null) {
13-
if (!value) {
14-
return 0;
15-
}
16-
12+
formatLabel(value: number) {
1713
if (value >= 1000) {
1814
return Math.round(value / 1000) + 'k';
1915
}

src/material/slider/slider.spec.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1531,11 +1531,7 @@ class SliderWithThumbLabel { }
15311531
styles: [styles],
15321532
})
15331533
class SliderWithCustomThumbLabelFormatting {
1534-
displayWith(value: number | null) {
1535-
if (!value) {
1536-
return 0;
1537-
}
1538-
1534+
displayWith(value: number) {
15391535
if (value >= 1000) {
15401536
return (value / 1000) + 'k';
15411537
}

src/material/slider/slider.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ export class MatSlider extends _MatSliderMixinBase
261261
* in the thumb label. Can be used to format very large number in order
262262
* for them to fit into the slider thumb.
263263
*/
264-
@Input() displayWith: (value: number | null) => string | number;
264+
@Input() displayWith: (value: number) => string | number;
265265

266266
/** Whether the slider is vertical. */
267267
@Input()
@@ -287,7 +287,9 @@ export class MatSlider extends _MatSliderMixinBase
287287
/** The value to be used for display purposes. */
288288
get displayValue(): string | number {
289289
if (this.displayWith) {
290-
return this.displayWith(this.value);
290+
// Value is never null but since setters and getters cannot have
291+
// different types, the value getter is also typed to return null.
292+
return this.displayWith(this.value!);
291293
}
292294

293295
// Note that this could be improved further by rounding something like 0.999 to 1 or

tools/public_api_guard/material/slider.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export declare class MatSlider extends _MatSliderMixinBase implements ControlVal
2424
};
2525
readonly change: EventEmitter<MatSliderChange>;
2626
readonly displayValue: string | number;
27-
displayWith: (value: number | null) => string | number;
27+
displayWith: (value: number) => string | number;
2828
readonly input: EventEmitter<MatSliderChange>;
2929
invert: boolean;
3030
max: number;

0 commit comments

Comments
 (0)