39

I'm using Typescript with AngularJS. I have a problem with modals using typed definition of jQuery library. I get the following error: 'error TS2339: Property 'modal' does not exist on type 'JQuery'.'

Version: jQuery library, version 1.10.x / 2.0.x Definitions: https://github.com/borisyankov/DefinitelyTyped

Code

$scope.delete = function (id) {
  Photo.get({id: id}, function(result) {
     $scope.photo = result;
     $('#deletePhotoConfirmation').modal('show');// error line 
  });
};

I'm referencing to jquery.d.ts in angular.d.ts

<reference path="../jquery/jquery.d.ts" />

and my global vendor reference file looks like:

<reference path='../vendor/types/angular/angular.d.ts' />
<reference path='../vendor/types/angular/angular-mocks.d.ts' />
<reference path='../vendor/types/jasmine/jasmine.d.ts' />
4
  • $scope.delete = function (id) { Photo.get({id: id}, function(result) { $scope.photo = result; $('#deletePhotoConfirmation').modal('show'); // error line }); };
    – TomekB
    Commented Sep 23, 2015 at 9:20
  • I'm referencing to 'jquery.d.ts' in 'angular.d.ts' file: /// <reference path="../jquery/jquery.d.ts" /> and my global vendor reference file looks like: /// <reference path='../vendor/types/angular/angular.d.ts' /> /// <reference path='../vendor/types/angular/angular-mocks.d.ts' /> /// <reference path='../vendor/types/jasmine/jasmine.d.ts' />
    – TomekB
    Commented Sep 23, 2015 at 9:24
  • 2
    @TomekB please do not post code in comments, edit the question post it there, there is edit link at bottom of question below tags
    – Shehary
    Commented Sep 23, 2015 at 9:40
  • stackoverflow.com/a/53098414/5361964 explains all steps to get rid of this issue
    – Zohab Ali
    Commented Nov 1, 2018 at 9:29

12 Answers 12

80

with newer versions of typescript (>v2 i believe):

npm install -D @types/bootstrap

Edit: As hughjdavey said in his comment, you also need the following import lines:

import * as bootstrap from "bootstrap";
import * as $ from 'jquery';
6
  • 1
    This worked for me in a project with jquery types also installed - it added to the jQuery interface such that $(modal-selector).modal('show') etc compiles fine
    – hughjdavey
    Commented Nov 13, 2017 at 13:40
  • 11
    I also had to add import * as bootstrap from "bootstrap"; to my app.module.ts otherwise IntelliJ stopped complaining but it still wouldn't compile on command line with the same error. Doesn't matter what you call it (doesn't have to be bootstrap). For absolute clarity, I also have import * as $ from "jquery"; in the same file which lets me use $(...) in my components.
    – hughjdavey
    Commented Nov 13, 2017 at 14:17
  • 2
    thank you @hughjdavey!!! for including those specific import statements... i can't believe how obscure this is for how common of a need it must be?!?
    – Beej
    Commented Dec 12, 2017 at 1:34
  • I don't know why but this does not work properly for me...A times it works. But most of the times, it does not work... Do you know why ? I ran the command and imported bootstrap.js in the component ts file but it still fails
    – Jason Krs
    Commented Apr 18, 2018 at 10:35
  • You can avoid polluting your code with unused references to 'bootstrap' if you add "bootstrap" to the "types" property of the "compilerOptions" object in tsconfig.json. Commented Feb 1, 2019 at 11:14
18

Your problem is caused by the lack of property with name modal in the jquery.d.ts file.

If you sure that this work in pure JS you can trick with it like this

$scope.delete = function (id) {
  Photo.get({id: id}, function(result) {
     $scope.photo = result;
     (<any>$('#deletePhotoConfirmation')).modal('show');
  });
};

Also, you can find additional d.ts file where this option has already been defined.

Try to consider this library that has already had modal option

Good Luck!

1
  • 1
    I've checked 'jquery.simplemodal.d.ts '. It works fine, thanks!
    – TomekB
    Commented Sep 23, 2015 at 10:24
13

In my case I had to use

npm install -D @types/bootstrap

and then I had to import bootstrap

import * as bootstrap from 'bootstrap';

but most important step was to remove jquery

import * as $ from 'jquery';

otherwise it was giving me this error:

TypeError: $(...).modal is not a function

0
12

When I upgrade angular to version 9, I faced this issue.

Here are my solution, I shared for whom concerned.

Property 'modal' does not exist on type 'JQuery'

$('#ManualMinMaxModal').modal('show');

change to

($('#ManualMinMaxModal') as any).modal('show');

And

Property 'DataTable' does not exist on type 'JQuery'

$('#realTimeTable').DataTable()

change to

($('#realTimeTable') as any).DataTable()

With

!$.fn.DataTable.isDataTable('#realTimeTable')

change to

!($.fn as any).DataTable.isDataTable('#realTimeTable')

Updated:

cast to any sometime not work, and finally solution is change declare from import * as $ from 'jquery' to declare var $: any;

9

this work for me, i add in first row:
declare var $ :any;
this declare $ type is any ,

2
  • where did u add this Commented Apr 21, 2020 at 7:00
  • first row in file that i use that
    – izik f
    Commented Feb 6, 2022 at 21:42
5

I could resolve it as follows:

import * as $ from "jquery";

($('#menuModal') as any).modal('toggle');
4

Try installing the typings for Bootstrap DefinitelyTyped

typings install --global --save dt~bootstrap

4

Just add

import * as bootstrap from "bootstrap";
import * as $ from "jquery";

in your app.module.ts

And install these packages

npm install @types/jquery --save-dev

npm install @types/bootstrap --save-dev

3

Solution for Angular 8+ versions :

Not working:

$('#deletePhotoConfirmation').modal('show');

Instead use this:

($('#deletePhotoConfirmation') as any).modal('show');

For Import :

import * as $ from 'jquery';

Good Luck

1

Yep, I too had to include both of these imports in order to make my app publishable by angular:

import * as bootstrap from "bootstrap";
import * as $ from "jquery";

I added them in the component that was using jQuery.

0

Did you add bootstrap.js to your angular.json?

"scripts": [
   "node_modules/jquery/dist/jquery.min.js",
   "node_modules/bootstrap/dist/js/bootstrap.min.js"
],
0

declare var $ :any;

This worked for me.

Not the answer you're looking for? Browse other questions tagged or ask your own question.