JSONPath-Plus / JSONPath

A fork of JSONPath from http://goessner.net/articles/JsonPath/
Other
963 stars 169 forks source link

JSONPath cache, toPathArray(), toPathString(), toPointer() are not accessible when using typescript #150

Closed sdolski closed 3 years ago

sdolski commented 3 years ago

I am working with typescript and facing an issue by trying to use the following property and methods:

When accessing those property or methods I am getting the following errors: Property 'toPathArray' does not exist on type 'JSONPathType'.ts(2339)

My guess is that the typescript declaration file jsonpath.d.ts is not correct.

sdolski commented 3 years ago

I have made some further analysis.

The problem might be the static modifiers in the class JSONPathClass of the jsonpath.d.ts. By removing those static modifiers the cache property and helper methods will become available.

brettz9 commented 3 years ago

Suggest taking a look at #113 . Not in a position to investigate myself, but PRs welcome.

sdolski commented 3 years ago

I have seen #113. I am currently not using the sandbox. So I am not facing that issue.

My problem is that those, above mentioned helper methods are not accessible at all with typescript. I have delivered a small PR #151 to make them accessible

brettz9 commented 3 years ago

Ok, sorry, not looking carefully here.

Can you provide a source showing why this works? I would think static could be exported. Is it because of how the class is being part of an intersection or something?

sdolski commented 3 years ago

Of course exporting static in general is not a problem. As well as the intersection. Exporting types, classes, object in the typescript declaration file is all fine.

But a static function need to be accessed in a static manner. ==> JSONPathClass.toPathArray(jsonPath)

In our case we are defining the type of JSONPath, which is a function exported by jsonpath.js. And because this is a function and not a class, we can not access the functions in a static manner.

But what we can do is, to declare that the exported JSONPath has additional functions (toPathArray(), toPathString(),....) as well as additional properties (cache). This is what we are trying to do with the JSONPathClass. But because we are not exporting a class here, it does only work without the static modified.

Hope that helps for clarification

brettz9 commented 3 years ago

Fixed by #151 . Released as part of v5.0.6. Thanks for the report!