-
Notifications
You must be signed in to change notification settings - Fork 6.8k
fix: adjust types in autocomplete-display-example #9240
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
Conversation
rafaelss95
commented
Jan 5, 2018
startWith({} as User), | ||
map(user => user && typeof user === 'object' ? user.name : user), | ||
startWith<string | User>(this.myControl.value), | ||
map(value => value && typeof value === 'object' ? value.name : value as string), |
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.
as
was necessary to run properly on Stackblitz.
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 typeof something === 'object'
is a little tricky since it'll also be true for null
. This might be nicer if you changed it to:
return typeof value === 'string' ? value : value.name;
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 typeof something === 'object' is a little tricky since it'll also be true for null"
In this case, it won't happen because the check is value && typeof value === 'object'
. Anyway, I changed it since it's more readable.
displayFn(user: User): string { | ||
return user ? user.name : user; | ||
displayFn(user?: User): string | undefined { | ||
return user ? user.name : undefined; |
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 was doing the following:
displayFn(user?: User): string | undefined {
return user ? user.name : user; <---
}
... however it doesn't go well in Stackblitz (maybe because there we don't have strictNullChecks
).
The class |
@@ -31,8 +31,8 @@ export class AutocompleteDisplayExample { | |||
ngOnInit() { | |||
this.filteredOptions = this.myControl.valueChanges | |||
.pipe( | |||
startWith({} as User), | |||
map(user => user && typeof user === 'object' ? user.name : user), | |||
startWith<string | User>(this.myControl.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.
myControl.value
will always be undefined. You could start it off with an empty string instead.
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.
Changed.
startWith({} as User), | ||
map(user => user && typeof user === 'object' ? user.name : user), | ||
startWith<string | User>(this.myControl.value), | ||
map(value => value && typeof value === 'object' ? value.name : value as string), |
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 typeof something === 'object'
is a little tricky since it'll also be true for null
. This might be nicer if you changed it to:
return typeof value === 'string' ? value : value.name;
Out of curiosity, can someone explain why these errors weren't caught by the compiler? |
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
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
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. |