Code Monkey home page Code Monkey logo

Comments (11)

ahocevar avatar ahocevar commented on June 19, 2024 1

Thanks @KyllianGautier, you are right. In #15843 I made it so the options and the layer have the same generics again - both the FeatureType, no longer SourceType<FeatureType>.

from openlayers.

KyllianGautier avatar KyllianGautier commented on June 19, 2024 1

@ahocevar thanks for this fix. I still have issue with typing in typescript :'(
I am able to give a TileLayer<TileSource> as a Layer<Source, Renderer>.
But I can't give a VectorLayer<FeatureLike> as a Layer<Source, Renderer>.

image

I'm sorry to disturbing you, but I don't find solution, and I don't know how to get the info to understand if I'm doing something wrong.

You can test a simple example of the typing problem in Typescript:

image

from openlayers.

ahocevar avatar ahocevar commented on June 19, 2024 1

@KyllianGautier You're still using the old type signature for the options. This should now be:

import { Feature as OLFeature } from 'ol';
import OLVectorLayer, { Options as OLVectorLayerOptions } from 'ol/layer/Vector.js';
import OLVectorSource from 'ol/source/Vector.js';

const options: OLVectorLayerOptions<OLFeature> = {
  source: new OLVectorSource<OLFeature>()
};

export const layer = new OLVectorLayer(options);

from openlayers.

ahocevar avatar ahocevar commented on June 19, 2024 1

In #15869, the source type is added back as 2nd type parameter to vector and vectortile layers.

from openlayers.

ahocevar avatar ahocevar commented on June 19, 2024

@KyllianGautier The type of the Options has to include the source, because the options are used for several BaseVector layer subclasses. For the VectorLayer, the source type is determined and does not require generics for the source. Does this cause a real-life problem?

from openlayers.

KyllianGautier avatar KyllianGautier commented on June 19, 2024

@ahocevar Ok, I understand. I tryed to adapt, but I still have some very strange problems with the typing :
Here I created a factory to instantiate a VectorLayer:
image
(I prefix all Openlayers object by "OL" in my imports, I hope you can understand easily)

from openlayers.

ahocevar avatar ahocevar commented on June 19, 2024

Maybe you got some of the imports wrong? This works fine for me:

import { FeatureLike as OLFeatureLike } from 'ol/Feature.js';
import { Options as OLBaseVectorLayerOptions } from 'ol/layer/BaseVector.js';
import OLVectorLayer from 'ol/layer/Vector.js';
import OLVectorSource from 'ol/source/Vector.js';

export const vectorLayer = <F extends OLFeatureLike>(options?: OLBaseVectorLayerOptions<OLVectorSource<F>>) => new OLVectorLayer<F>(options);

from openlayers.

KyllianGautier avatar KyllianGautier commented on June 19, 2024

@ahocevar Unfortunately, I have errors in typescript trying your solution:
image
But this works fine in javascript indeed

from openlayers.

ahocevar avatar ahocevar commented on June 19, 2024

I am able to give a TileLayer as a Layer<Source, Renderer>.

Yes, but you don't need to specify the Renderer type. And other than the Vector and VectorTile sources, TypeScript needs to distinguish between the different ol/layer/Tile subclasses here, because subclasses have additional API methods.

But I can't give a VectorLayer as a Layer<Source, Renderer>.

Correct, that's no longer the type signature.

You can test a simple example of the typing problem in Typescript:
export const vectorLayer: OLVectorLayer<OLFeatureLike> = new OLVectorLayer<OLFeature>();

That's also expected. There is no inheritance relationship between the allowed types of FeatureLike. Feature and RenderFeature only have a small subset of a common API. So in TypeScript, you have to decide when you declare the variable what you're going to store there. When using JavaScript, the correct type will be inferred. The following will work in TypeScript:

export const vectorLayer: OLVectorLayer<OLFeature> = new OLVectorLayer<OLFeature>();

from openlayers.

KyllianGautier avatar KyllianGautier commented on June 19, 2024

Thanks for the details. I tried what you proposed, and I still have issues in Typescript:

image
image

And I tried to change the type of for options and source:

image
image

from openlayers.

KyllianGautier avatar KyllianGautier commented on June 19, 2024

@ahocevar Ok, problem solved, thanks for the support.

I have an additional question about the Layer.getSource() method. How can we type the source returned by a VectorLayer ?

If I use a Custer source for example, I instantiate a VectorLayer with this Cluster source, and i'm not able to use the Cluster source specific methods such as setDistance() or setMinDistance():

image

Other side effect, it seems that a VectorLayer with an other source than a Vector source is not assiciable with a Layer ?

image

from openlayers.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.