Search code examples
angulartypescriptgoogle-picker

How can I define types for external libraries. TypeScript , Angular and Google Picker


I'm trying to use the Google Picker API in my Angular 4 app. To be able to use the features of Google Picker I have to import the library, and I do that in my index.html file.

index.html:

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="theme-color" content="#2185d0">
  <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
  <title>Librostic</title>
  <base href="/">

  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="favicon.ico">
  <script src="https://use.fontawesome.com/18845e1b3c.js"></script>

  <link href="https://fonts.googleapis.com/css?family=Alfa+Slab+One" rel="stylesheet">
</head>
<body>
  <app-root>
  </app-root>
  <!-- See the bellow script -->
  <script type="text/javascript" src="https://apis.google.com/js/api.js?onload=onApiLoad"></script> 
</body>

<script>
  $('.ui.dropdown')
    .dropdown()
  ;
</script>
</html>

So the problem is where I try to use some methods because Typescript doesn't know what that is. Let's see that in an example. I got the following function:

function createPicker() {
      if (pickerApiLoaded && oauthToken) {
        // Here I get errors like: Cannot find name 'google'.
        var view = new google.picker.View(google.picker.ViewId.DOCS);
        view.setMimeTypes('application/pdf');
        var picker = new google.picker.PickerBuilder()
          .enableFeature(google.picker.Feature.NAV_HIDDEN)
          .enableFeature(google.picker.Feature.MULTISELECT_ENABLED)
          .setAppId(appId)
          .setOAuthToken(oauthToken)
          .addView(view)
          .addView(new google.picker.DocsUploadView())
          .setDeveloperKey(developerKey)
          .setCallback(pickerCallback)
          .build();
        picker.setVisible(true);
      }
    }

This is just a TypeScript error meaning that the function is excecuted without a TS compiler. How can I solve this problem? Thanks!


Solution

  • Augmenting CRice's answer,

    you're looking for npm install @types/google.picker @types/gapi.client --save-dev