Skip to content
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

Merged
merged 7 commits into from
Nov 10, 2017

Conversation

amcdnl
Copy link
Contributor

@amcdnl amcdnl commented Sep 24, 2017

Addresses #6370

@googlebot googlebot added the cla: yes PR author has agreed to Google's Contributor License Agreement label Sep 24, 2017
@@ -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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this._checked = coerceBooleanProperty(value);?

Copy link
Member

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done :)

Copy link
Contributor

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 !! :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#facepalm

get touchUi() {
return this._touchUi;
}
set touchUi(value: any) {
Copy link
Contributor

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).

Copy link
Contributor Author

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.

Copy link
Contributor

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

@@ -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); }
Copy link
Contributor

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)

Copy link
Contributor Author

@amcdnl amcdnl Oct 1, 2017

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?

Copy link
Contributor

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?

Copy link
Member

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works well.

@Input() overlapTrigger = this._defaultOptions.overlapTrigger;
@Input()
set overlapTrigger(value: any) {
this._defaultOptions.overlapTrigger = coerceBooleanProperty(value);
Copy link
Member

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.

@@ -224,7 +231,7 @@ export class MatDatepicker<D> implements OnDestroy {
}
}

ngOnDestroy() {
ngOnDestroy(): void {
Copy link
Member

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

/** Whether the toggle indicator should be hidden. */
@Input() hideToggle: boolean = false;
@Input()
get hideToggle(): any {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any -> boolean

Copy link
Member

@jelbourn jelbourn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@jelbourn jelbourn added pr: lgtm action: merge The PR is ready for merge by the caretaker labels Oct 26, 2017
@jelbourn
Copy link
Member

jelbourn commented Oct 26, 2017

@amcdnl can you change the PR title to

fix: consistently coerce boolean and number properties to the correct type

?

@amcdnl amcdnl changed the title chore(audit): audit boolean inputs #6370 fix: consistently coerce boolean and number properties to the correct type Oct 30, 2017
@amcdnl
Copy link
Contributor Author

amcdnl commented Oct 30, 2017

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; }
Copy link
Member

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?

@josephperrott josephperrott merged commit 3ca801a into angular:master Nov 10, 2017
@amcdnl amcdnl deleted the coerce-6360 branch November 19, 2017 19:13
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 7, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
action: merge The PR is ready for merge by the caretaker cla: yes PR author has agreed to Google's Contributor License Agreement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants