sod
sod

Reputation: 3928

use namespace as type in typescript

I have installed type definitions for google maps that declares a namespace like this:

namespace google.maps {
  export class Map {
    // ...
  }

  // ...
}

So if i just use the API globally it works beautifully:

const map = new google.maps.Map();

For unit testablity I don't want to access the API globally but instead inject it. But it seems I can't type that a variable should by from type google.map

So this doesn't work:

function mapFactory(api: google.maps) {
    return new api.Map();
}

Any solution how to use the namespace as a type?

Upvotes: 47

Views: 22218

Answers (1)

Nitzan Tomer
Nitzan Tomer

Reputation: 164397

Try:

function mapFactory(api: typeof google.maps) {
    return new api.Map();
}

Upvotes: 83

Related Questions