Abhijith Nagaraja Abhijith Nagaraja - 1 year ago 137
Javascript Question

Using JavaScript in typescript with JQuery

I am trying to use a 3rd party library (autonumeric) with Typescript + Angular2.

I have tried the following

/// <reference path="/typings/main/ambient/jquery/index.d.ts" />
declare interface JQuery {
autoNumeric: any;

and on
I am trying to use it.

ngOnInit() {
$('selector').autoNumeric('init', {options});

But when I compile, I am getting the following error.

error TS2339: Property 'autoNumeric' does not exist on type 'JQuery'

I am not getting where I am going wrong. Any help would be appreciated


I have included the autonumeric.js in the index.html

Answer Source

I did it exactly like you and it works fine. You should create definition file to extend JQuery interface and add reference to *.d.ts file to main.ts:

/// <reference path="./typings/declarations.d.ts" />

where declarations.d.ts contains:

declare interface JQuery {
    autoNumeric: any;