-
Notifications
You must be signed in to change notification settings - Fork 6.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: consistently coerce boolean and number properties to the correct type #7283
Conversation
src/lib/slide-toggle/slide-toggle.ts
Outdated
@@ -125,6 +125,7 @@ export class MdSlideToggle extends _MdSlideToggleMixinBase implements OnDestroy, | |||
@Input() | |||
get checked(): boolean { return this._checked; } | |||
set checked(value) { | |||
value = coerceBooleanProperty(value); | |||
this._checked = !!value; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this._checked = coerceBooleanProperty(value);
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, it should be that
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
coerceBooleanProperty
already returns a boolean, you don't need !!
:)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#facepalm
src/lib/datepicker/datepicker.ts
Outdated
get touchUi() { | ||
return this._touchUi; | ||
} | ||
set touchUi(value: any) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think its best to leave the types as boolean
that way when people @ViewChild
this and try to programmatically set it they at least know that they should be passing a boolean (and then behind the scenes we coerce it anyways it just in case it came from a template binding).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just followed the pattern that disabled had right below it. I'll update both.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah I think there's a lot that use any
, but we can use the expected type for new ones we add and maybe eventually fix all the any
ones
src/lib/tabs/tab-group.ts
Outdated
@@ -111,7 +111,7 @@ export class MatTabGroup extends _MatTabGroupMixinBase implements AfterContentIn | |||
|
|||
/** The index of the active tab. */ | |||
@Input() | |||
set selectedIndex(value: number | null) { this._indexToSelect = value; } | |||
set selectedIndex(value: number | null) { this._indexToSelect = coerceNumberProperty(value); } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it looks like the author was intentionally trying to allow null
here in which case I think you want coerceNumberProperty(value, null)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The coerceNumberProperty
signature does not support null
as the second parameter. Should we extend it to support that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, in a lot of cases people really do just want number. What if we did this?
export function coerceNumberProperty(value: any): number;
export function coerceNumberProperty<D>(value: any, fallback: D): number | D;
export function coerceNumberProperty(value: any, fallbackValue = 0) { ... }
It would allow people to get just a number
in 1-arg case and number|whatever
in 2-arg case. @jelbourn WDYT?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That seems good to me
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Works well.
src/lib/menu/menu-directive.ts
Outdated
@Input() overlapTrigger = this._defaultOptions.overlapTrigger; | ||
@Input() | ||
set overlapTrigger(value: any) { | ||
this._defaultOptions.overlapTrigger = coerceBooleanProperty(value); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this one is quite right. The current behavior is to default the value to this._defaultOptions.overlapTrigger
; the default shouldn't be changed.
src/lib/datepicker/datepicker.ts
Outdated
@@ -224,7 +231,7 @@ export class MatDatepicker<D> implements OnDestroy { | |||
} | |||
} | |||
|
|||
ngOnDestroy() { | |||
ngOnDestroy(): void { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't need to add types to lifecycle methods
src/lib/expansion/expansion-panel.ts
Outdated
/** Whether the toggle indicator should be hidden. */ | ||
@Input() hideToggle: boolean = false; | ||
@Input() | ||
get hideToggle(): any { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
any
-> boolean
…erce-6360 # Conflicts: # src/lib/expansion/expansion-panel.ts
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
@amcdnl can you change the PR title to
? |
done :) |
@@ -41,8 +42,10 @@ export class CdkAccordionItem implements OnDestroy { | |||
|
|||
/** Whether the AccordionItem is expanded. */ | |||
@Input() | |||
get expanded(): boolean { return this._expanded; } | |||
set expanded(expanded: boolean) { | |||
get expanded(): any { return this._expanded; } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this getter now return boolean
?
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
Addresses #6370