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

TypeScript: VectorSource<Feature<Point>> can't be used as a source for VectorLayer #15832

Closed
perliedman opened this issue May 16, 2024 · 1 comment
Labels

Comments

@perliedman
Copy link

Describe the bug

I have a somewhat surprising typing issue where TypeScript will not let me use a VectorSource<Feature<Point>> as source for a VectorLayer, which seems surprising:

const source = new VectorSource({
  features: [new Feature(new Point([0, 0]))],
});
const layer= new VectorLayer({
  source,
});

This gives the error:

Type 'VectorSource<Feature<Point>>' is not assignable to type 'VectorSource<FeatureLike>'.
  Types of property 'setLoader' are incompatible.
    Type '(loader: FeatureLoader<Feature<Point>>) => void' is not assignable to type '(loader: FeatureLoader<FeatureLike>) => void'.
      Types of parameters 'loader' and 'loader' are incompatible.
        Types of parameters 'arg4' and 'arg4' are incompatible.
          Type '((arg0: Feature<Point>[]) => void) | undefined' is not assignable to type '((arg0: FeatureLike[]) => void) | undefined'.
            Type '(arg0: Feature<Point>[]) => void' is not assignable to type '(arg0: FeatureLike[]) => void'.
              Types of parameters 'arg0' and 'arg0' are incompatible.
                Type 'FeatureLike[]' is not assignable to type 'Feature<Point>[]'.
                  Type 'FeatureLike' is not assignable to type 'Feature<Point>'.
                    Type 'Feature<Geometry>' is not assignable to type 'Feature<Point>'.typescript(2322)

It looks similar to #15356, but that should have been fixed and I'm using 9.2.0 when I see this error.

To Reproduce
Steps to reproduce the behavior:

  1. Visit the code sandbox I made: https://codesandbox.io/p/sandbox/ol-vectorlayer-typings-mkyp3m?file=%2Fsrc%2Findex.ts%3A12%2C1-18%2C1
  2. A typing error is indicated on line 16 where source is provided to VectorLayer's contstructor, see error message above

This issue occurred in my app code which of course is a lot more complicated. It used to work, but after an upgrade (probably updating my TypeScript version), several errors like this one occurred. I tried upgrading to latest OpenLayers, but the issue persists.

Expected behavior

I expect to be able to use a VectorSource<Feature<Point>> as a source for VectorLayer without having to resort to type casting.

@perliedman perliedman added the bug label May 16, 2024
@perliedman
Copy link
Author

Apparently did a bad job of searching the issues (searched for "typescript"), I see that my issue seems to be a duplicate of #15830. Sorry

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

No branches or pull requests

2 participants