define a function of an object in typescript


Good morning, I am new to Typescript and would like to know how I can pass my JavaScript code to Typescript, since I am rewriting my code in Angular 2.


var o = geotab.addin.addinangular= function () {
    'use strict';
    return {

In this case, geotab is an object defined when the JavaScript file is uploaded to the GEOTAB platform, and its addin attribute contains an initialization function called addinangular . That is, I need to be able to define a function to the geotab.addin object in Typescript with this structure: geotab.addin.funcion() .

asked by Alejandro 11.04.2017 в 23:50

1 answer


Typescript is a programming language superimposed on Javascript, so all the code written in Javascript is valid in Typescript. But in addition, it controls the types to reinforce the code and to avoid that errors appear in execution taking care that when writing the code the values of the variables are respected.

The problem is that for this you need a file in which you define the types of the variable. Normally the packages installed by NPM already add these types to your project. If geotab has a package in NPM , install it and insert it into your code with import ... from 'geotab'; instead of adding the library to index.html.

If the library is not in NPM or you do not have typings , you must declare them manually by adding the following line to the beginning of the file:

declare var geotab: any;
answered by 12.04.2017 в 17:26