-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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: updated field_dropdown to properly infer its Field type with TS 5.3.3 #7939
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -408,7 +408,13 @@ export class FieldDropdown extends Field<string> { | |
* @param newValue The input value. | ||
* @returns A valid language-neutral option, or null if invalid. | ||
*/ | ||
protected override doClassValidation_(newValue?: string): string | null { | ||
protected override doClassValidation_( | ||
newValue: string, | ||
): string | null | undefined; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just noticed a nitpick - even thoug the base class has There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. class validators are allowed to return undefined: https://developers.google.com/blockly/guides/create-custom-blocks/fields/validators#return_values I think we need to keep the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sounds good! I'll make the update. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done! |
||
protected override doClassValidation_(newValue?: string): string | null; | ||
protected override doClassValidation_( | ||
newValue?: string, | ||
): string | null | undefined { | ||
const options = this.getOptions(true); | ||
const isValueValid = options.some((option) => option[1] === newValue); | ||
|
||
|
@@ -426,7 +432,7 @@ export class FieldDropdown extends Field<string> { | |
} | ||
return null; | ||
} | ||
return newValue as string; | ||
return newValue; | ||
} | ||
|
||
/** | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.
Is this a bad diff, or are there three lines in a row declaring the same function? And if it's the latter, why is that necessary and allowed?
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 is accurate. This is actually how we have it defined in
field.ts
, too:https://github.com/google/blockly/blob/master/core/field.ts#L1183
What this does is allow inferring type info based on what you're supplying. Taking
Field
'sdoClassValidation
as an example, implementation-wise,undefined
means to take new value as is instead of transforming it in some way. What happens is if you provide anewValue
as a non-undefined value, it infers the first definition that matches.