3

I want to try setting up a simple map in openlayers using typescript:

https://github.com/borisyankov/DefinitelyTyped/blob/master/openlayers/openlayers.d.ts

I take that file, put it into

/typings/openlayers/openlayers.d.ts

I have an app.ts file at the top in my root directory that I put:

import { Map } from "olx";

In my tsconfig.json, I've included the path to the openlayers.d.ts file. But I am gettting olx is not defined when I use the tsc command from npm's typescript package.

All I am going for is drawing a simple openlayers3 map, hope to not deviate from javascript too much if possible.

2 Answers 2

8

First install openlayers:

npm install openlayers

Then install types openlayers to your dev env:

npm install --save-dev @types/openlayers

To import I tried:

import * as ol from 'openlayers';

Then you can invoke anything in openlayers like this:

new ol.Map();
Sign up to request clarification or add additional context in comments.

Comments

0

import { Map } from "olx";

This will not work as there is no ambient external module called "olx". also olx refers to something else on NPM https://www.npmjs.com/package/olx so we recommend not using this name.

Recommend : https://www.npmjs.com/package/openlayers

With a definition file extended as:

declare module "openlayers" {
    export = olx;
}

3 Comments

Why does the definition not contain the export = olx part? Or any export?
Because its designed for script tag inclusion and not an ES6 module exclusion. I guess what the module will be called if used with an external module loader wasn't established when the definition was written. See test file github.com/borisyankov/DefinitelyTyped/blob/master/openlayers/… for intended usage
I am new to typescript, can you elaborate what you mean by script tag incluson and not an ES6 module exclusion? Do you mean someone would compile the openlayers.ts.d flle independently, then include it in the html page via script tag or use something in this question: stackoverflow.com/questions/14015899/…. If so, is your answer what should be "best practice/current" to manually add export = olx to the current definition? Or is there a better way for me to use openlayers without the typescript ts.d file? (seems like there are alot out-of-dateness)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.