I encountered challenges while working with the JSTS library and its Typescript bindings. Despite eventually getting it to work, I am still unsure of the reasons behind its functionality.
My journey began with installing JSTS and its corresponding typings:
npm i --S jsts
npm i --D @types/jsts
Initially, my code looked like this:
import jsts from 'jsts';
const {OL3Parser} = jsts.io;
const parser = new OL3Parser();
Although there were no type errors, the code crashed at runtime with:
Uncaught TypeError: Cannot read property 'io' of undefined
.
When trying to explicitly assign a type to the parser
variable:
const parser: OL3Parser = new OL3Parser();
I received the error message:
TS2749: 'OL3Parser' refers to a value, but is being used as a type here. Did you mean 'typeof OL3Parser'?
Subsequently, my attempt to import the module in a different way:
import OL3Parser from 'jsts/org/locationtech/jts/io/OL3Parser';
Resulted in a failed type check with:
TS7016: Could not find a declaration file for module 'jsts/org/locationtech/jts/io/OL3Parser'.
After conducting some research online, I found a solution:
const jsts = require('jsts');
const parser: jsts.io.OL3Parser = new jsts.io.OL3Parser();
While this approach both passes type checking and functions correctly, I am dissatisfied with the lengthy qualified names and remain puzzled by the necessity of using the library in this specific manner.
Any insights or suggestions?