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

Support typing for extra fields in component instance #70

Open
LastLeaf opened this issue Jul 26, 2023 · 3 comments
Open

Support typing for extra fields in component instance #70

LastLeaf opened this issue Jul 26, 2023 · 3 comments
Assignees
Labels
enhancement New feature or request module:core
Milestone

Comments

@LastLeaf
Copy link
Member

LastLeaf commented Jul 26, 2023

When adding a custom field in a component (defined with definition API), its type is hard to specify.

Solution 1

Add a definition field for specifying types for the extra fields, e.g.

componentSpace.defineComponent({
  extraThisFieldsType: undefined as unknown as { /* SOME CUSTOM FIELDS TYPE */ }
})

The data type of extraThisFieldsType can be merged into ThisType of the component.

Solution 2

Add another initiation function, which returns some fields that will be written into this (of the method caller, if any), e.g.

componentSpace.defineComponent({
  extraThisFields: () => { /* SOME CUSTOM FIELDS */ }
})

If some fields cannot be set during initiation, its type should be given manually, e.g.

componentSpace.defineComponent({
  extraThisFields: () => {
    delayedField: undefined as SomeType | undefined
  }
})

Solution 3

Add a sopport for chaining only.

componentSpace.define()
  .extraThisFieldsType<{ delayedField: SomeType }>()
  .registerComponent()

This requires a polyfill to run on legacy framework, and requires more code changes.

@LastLeaf LastLeaf added the enhancement New feature or request label Jul 26, 2023
@LastLeaf LastLeaf self-assigned this Jul 26, 2023
@LastLeaf
Copy link
Member Author

Solution 1 does not require real JavaScript logic, which means it does not introduce version compatibility problems when running on external glass-easel environment.

Solution 2 looks cleaner.

@LastLeaf
Copy link
Member Author

LastLeaf commented Aug 1, 2023

Note that extra fields should pass from behaviors to their users.

@LastLeaf LastLeaf modified the milestones: v0.2, v1.0 Sep 4, 2023
@LastLeaf LastLeaf added the need discussion Further discussion is needed to decide label Feb 4, 2024
@LastLeaf
Copy link
Member Author

LastLeaf commented May 8, 2024

Added solution 3 as the final agreement.

@LastLeaf LastLeaf removed the need discussion Further discussion is needed to decide label May 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request module:core
Projects
None yet
Development

No branches or pull requests

1 participant