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: updated field_dropdown to properly infer its Field type with TS 5.3.3 #7939

Merged

Conversation

btw17
Copy link
Member

@btw17 btw17 commented Mar 15, 2024

The basics

The details

Resolves

Fixes #7715

Proposed Changes

Update FieldDropdown's type information for doClassValidation to match the template in Field so it properly infers FieldDropdown as Field<string> instead of Field<string|undefined>.

Reason for Changes

To enable Blockly to update to TypeScript 5.3.3 and beyond.

Test Coverage

N/A

Documentation

N/A

Additional Information

None

@btw17 btw17 requested a review from a team as a code owner March 15, 2024 20:32
@btw17 btw17 force-pushed the fix_field_dropdown_type_inferrence branch 2 times, most recently from 7174983 to 5d8fe68 Compare March 15, 2024 20:38
@btw17 btw17 changed the title Updated field_dropdown to properly infer its Field type with TS 5.3.3 [fix] updated field_dropdown to properly infer its Field type with TS 5.3.3 Mar 15, 2024
Copy link

conventional-commit-lint-gcf bot commented Mar 15, 2024

🤖 I detect that the PR title and the commit message differ and there's only one commit. To use the PR title for the commit history, you can use Github's automerge feature with squashing, or use automerge label. Good luck human!

-- conventional-commit-lint bot
https://conventionalcommits.org/

@btw17 btw17 changed the title [fix] updated field_dropdown to properly infer its Field type with TS 5.3.3 fix: updated field_dropdown to properly infer its Field type with TS 5.3.3 Mar 15, 2024
@github-actions github-actions bot added the PR: fix Fixes a bug label Mar 15, 2024
@btw17 btw17 force-pushed the fix_field_dropdown_type_inferrence branch from 5d8fe68 to ef899fe Compare March 15, 2024 20:49
@@ -408,6 +408,10 @@ export class FieldDropdown extends Field<string> {
* @param newValue The input value.
* @returns A valid language-neutral option, or null if invalid.
*/
protected override doClassValidation_(
Copy link
Collaborator

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?

Copy link
Member Author

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's doClassValidation 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 a newValue as a non-undefined value, it infers the first definition that matches.

  // Providing a non-undefined value allows returning `undefined` (which means to take that value as is)
  protected doClassValidation_(newValue: T): T | null | undefined;
  // Providing an undefined or possibly undefined value means you can't return `undefined` since
  // that would mean take the default, though default could be `undefined` which is not valid to set.
  protected doClassValidation_(newValue?: AnyDuringMigration): T | null;
  // This accepts all the implementations above and we handle the cases in code through this.
  protected doClassValidation_(
    newValue?: T | AnyDuringMigration,
  ): T | null | undefined {
    if (newValue === null || newValue === undefined) {
      return null;
    }

    return newValue as T;
  }

@@ -408,6 +408,10 @@ 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 | undefined;
Copy link
Member Author

Choose a reason for hiding this comment

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

Just noticed a nitpick - even thoug the base class has | undefined, this class has never expected that and it's actually not needed here. (I confirmed locally.) Updating the slight tweak.

Copy link
Contributor

Choose a reason for hiding this comment

The 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 | undefined to allow subclasses of Dropdown to continue returning undefined

Copy link
Member Author

Choose a reason for hiding this comment

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

Sounds good! I'll make the update.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done!

@rachel-fenichel rachel-fenichel merged commit b6b5721 into google:develop Mar 15, 2024
6 checks passed
@btw17 btw17 deleted the fix_field_dropdown_type_inferrence branch March 22, 2024 17:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
PR: fix Fixes a bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Typescript error for dropdown field
3 participants