简体   繁体   English

d3 typescript 地理路径这

[英]d3 typescript geopath this

I have a geometric transformation function in my React typescript app that looks like below.我的 React typescript 应用程序中有一个几何变换 function,如下所示。

        // Use Leaflet to implement a D3 geometric transformation.
        const projectPoint = ( x: any, y: any) => {
          const point = map.latLngToLayerPoint(new L.LatLng(y, x));
          this.stream.point(point.x, point.y);
        }

However I get an error "Object is possibly 'undefined'.ts(2532)" for this .但是我收到一个错误“对象this是‘undefined’.ts(2532)”。 So I looked at doing an If statement to check if it is undefined (see below) but that doesn't work所以我看着做一个 If 语句来检查它是否未定义(见下文)但这不起作用

        const projectPoint = ( x: any, y: any) => {
          const point = map.latLngToLayerPoint(new L.LatLng(y, x));
          if (this != undefined) {
           this.stream.point(point.x, point.y);
          } else {
            return null
          }
        }   

And even tried below..甚至在下面尝试过..

        const projectPoint = ( x: any, y: any) => {
          const point = map.latLngToLayerPoint(new L.LatLng(y, x));
           this!.stream.point(point.x, point.y);
        }

But i get the error that Property 'stream' does not exist on type 'never'.但我得到的错误是属性“流”在类型“从不”上不存在。 Thanks for the help!谢谢您的帮助!

I found the problem was unrelated to "this" (pardon the pun).我发现问题与“这个”无关(请原谅双关语)。 Though I have incorrectly written this projectPoint function. The correct function should declare this with this: any as follows.虽然我错误地写了这个 projectPoint function。正确的 function 应该声明这个:any如下。

        // Use Leaflet to implement a D3 geometric transformation.
        const projectPoint = ( this:any, x: any, y: any) => {
          const point = map.latLngToLayerPoint(new L.LatLng(y, x));
          this.stream.point(point.x, point.y);
        }

What would be a better type than "any" for "this" that I could use out of interest?对于我出于兴趣可以使用的“this”,有什么比“any”更好的类型?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM