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

[BaseFeature] Make property "properties" modifiable #390

Open
3 tasks done
ChristophMaskos opened this issue Jan 24, 2025 · 1 comment
Open
3 tasks done

[BaseFeature] Make property "properties" modifiable #390

ChristophMaskos opened this issue Jan 24, 2025 · 1 comment
Labels
enhancement New feature or request

Comments

@ChristophMaskos
Copy link
Contributor

Issue description

BaseFeature as a representation of a feature should have the ability to change the properties.

properties?: Readonly<Record<string, unknown>> => properties?: Record<string, unknown>

Validations

@ChristophMaskos ChristophMaskos added enhancement New feature or request good first issue A simple feature that can easily be implemented by new contributors. labels Jan 24, 2025
@mbeckem
Copy link
Contributor

mbeckem commented Jan 24, 2025

This is a good point, but simply making the properties writable will probably not be good enough (since the changes would not be detectable by react components etc.). I think a real solution would need something like a Feature class with properties backed by the reactivity API, as an "evolution" of the BaseFeature interface.

For the time being, I can recommend two approaches:

  • Instead of changing the existing feature instance, create a new one as a copy, with the updated properties. For example:
    const oldFeature = ...;
    const newFeature = { ...oldFeature, properties: { ...oldFeature.properties, updatedProperty: "foo" }};
    This will work well with react components that detect changes based on value "sameness" (mutating existing values would be a bad idea).
  • If it works in your use case, simply cast away the "readonlyness". It is just a TypeScript annotation and could be ignored. For example:
    type WritableFeature = BaseFeature & { properties: Record<string, unknown> };
    
    const existingFeature = ...; 
    const writableFeature = feature as WritableFeature; // use at your own risk
    writableFeature.properties.foo = 3;

@mbeckem mbeckem removed the good first issue A simple feature that can easily be implemented by new contributors. label Jan 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants