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

PointFeature properties inaccurate type #35

Closed
robert-nash opened this issue May 25, 2023 · 1 comment
Closed

PointFeature properties inaccurate type #35

robert-nash opened this issue May 25, 2023 · 1 comment

Comments

@robert-nash
Copy link

I don't know if it is possible to solve this but I think it could be helpful if it was. Perhaps I am missing something and there is a better solution.

In the readme it states: "Currently supported Point properties are null, boolean, number, string. The rest will be discarded when the supercluster is created. If you need to store other properties you can always turn them into a JSON."

This makes sense but it would be useful if there was some way of reflecting this in the types to avoid errors where properties other than these types were passed to the class Supercluster.

Currently, the follow code could lead to problems if the an attempt was made to access PointFeature["properties"]["price"]["weekday"] since the type would suggest this property existed when, in fact, this will have been stripped out.

type InternalPointType = {
    name: string,
    price: {
        weekday: number;
        weekend: number;
    }
}
const superClusterIndex = new Supercluster<InternalPointType >();

In my own code I have used the following type "PickByType" (credits: https://stackoverflow.com/a/69756175) to create a new version of the original type which reflects the runtime object more accurately.

type PickByType<T, Value> = {
    [P in keyof T as T[P] extends Value | undefined ? P : never]: T[P];
};

type ClusteredInternalPointType = PickByType<
    InternalPointType,
    null | boolean | number | string
>
const superClusterIndex = new Supercluster<ClusteredInternalPointType >();

I wonder if a similar technique could be used internally to modify the type passed to Supercluster. I don't think I have a deep enough understanding of this project at the moment to create a pull request but I think this could be helpful if someone else thought this would be useful to implement.

@JiriHoffmann
Copy link
Owner

No need for picking types, this limitation is removed with v2.0.0 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants