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

Error trying to access layer attributes with Typescript #945

Closed
lucas-santosP opened this issue Jul 1, 2021 · 5 comments
Closed

Error trying to access layer attributes with Typescript #945

lucas-santosP opened this issue Jul 1, 2021 · 5 comments

Comments

@lucas-santosP
Copy link

Hi, I'm trying to listen to the pm:create event using Typescript, but when I try to access any attribute of the layer like this:

map.on("pm:create", (e) => {
  e.layer.toGeoJSON();
}

I got this error:

Property 'toGeoJSON' does not exist on type 'Layer'.ts(2339)

I tried the same code on js and works fine... So this is expected behavior because the lib doesn't have complete Typescript support yet or I'm doing something wrong?

Package versions:

"leaflet": "^1.7.1",
"@geoman-io/leaflet-geoman-free": "^2.11.2",
"@types/leaflet": "^1.7.3",
@Falke-Design
Copy link
Collaborator

The return type of e.layer is L.Layer and it has no function toGeoJSON(). So you need first to cast the e.layer to the correct Class. You can get this over e.shape:

map.on("pm:create", (e) => {
  if(e.shape === 'Polygon'){
     (e.layer as Polygon).toGeoJSON();
  }
}


@lucas-santosP
Copy link
Author

The return type of e.layer is L.Layer and it has no function toGeoJSON(). So you need first to cast the e.layer to the correct Class. You can get this over e.shape:

map.on("pm:create", (e) => {
  if(e.shape === 'Polygon'){
     (e.layer as Polygon).toGeoJSON();
  }
}

Alright, this works now thanks.

But there is no documentation about how to use Typescript with the lib?
I would prefer to use TS, but I'm thinking that will be more difficult... if you know a repo example using it would be great to take a look too.

@Falke-Design
Copy link
Collaborator

I created a discussions post to create a documentation of the "confusing" Typescript points #949. But there should not much of this.

@ryan-morris maybe you have example repo

@lucas-santosP
Copy link
Author

Ok thanks. I appreciate the effort, I'll take a look and try to help if I found something.

The main question of this issue has been resolved, so I'll close it.

@ryan-morris
Copy link
Contributor

@Falke-Design, @lucas-santosP the closest I would have to an example would be this link, posted in #790 . It's a little angular specific but the geoman interactions should apply as basic typescript implementation.

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

No branches or pull requests

3 participants