2

After installing types for a library such as "@types/openlayers" how can I then use these for inline type definitions for things like React props:

import Map from "ol/Map";    
import { Map as MapTypes } from "@types/openlayers"; // Error: Cannot import type declaration files.

export type Props = {
    mapInstance: MapTypes;
}

export const OlMap: FunctionComponent<Props> = (props): JSX.Element => {
   const currentMap = props.mapInstance;
   ...
}
7
  • openlayers seems to be deprecated. Did you mean to use (or are you actually using) "ol"? Commented Nov 16, 2020 at 15:37
  • npmjs.com/package/@types/openlayers which is the import above is not deprecated. Commented Nov 16, 2020 at 15:40
  • 1
    Which package are you actual applying types to though? openlayers or ol? Typically, the @types/* matches the package name exactly. Which is why I think you would want npmjs.com/package/@types/ol instead. Commented Nov 16, 2020 at 15:41
  • 1
    I have added the actual package above as an import, its 'ol' Commented Nov 16, 2020 at 15:42
  • Ah, that may be the issue then. Can you try with the @types/ol package instead and pull the type out of ol? It may still be exported somewhere else, but I think the types and actual package need to match first. Commented Nov 16, 2020 at 15:44

1 Answer 1

3

Use it from the actual package that is being typed, not the @types/* module. For example, with react-router, you add @types/react-router but to pull out the RouteProps interface, you use import {RouteProps} from "react-router";.


Additionally, from your edits, it seems that you may be using the wrong @types/* package. You mention using @types/openlayers but then mention that you are using the package ol, which should probably use the @types/ol package for types.

Note declaration file consumption:

For the most part, type declaration packages should always have the same name as the package name on npm, but prefixed with @types/

After running yarn add ol @types/ol (or npm i ol @types/ol)

This seems to be using the right types:

import MapTypes from "ol/Map"; // Note that I don't need "{}" or "as" here
// import {Map as MapTypes} from "ol"; or pull it out of the root export

export type Props = {
    mapInstance: MapTypes;
}

export const OlMap: FunctionComponent<Props> = (props): JSX.Element => {
   const currentMap = props.mapInstance;
   ...
}
Sign up to request clarification or add additional context in comments.

1 Comment

But in many cases the definitions type exports have the same name as the package export. For example with OpenLayers and @types/openlayers the types for the OpenLayers module "Map" are also exported as "Map" from @types/openlayers. How can this be distinguished?

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.