Jquery with Typescript : Property 'css' does not exist on type 'ElementFinder'

不打扰是莪最后的温柔 提交于 2019-12-06 13:08:43

问题


I'm trying to use jquery in my angular2 project which is getting build with webpack . I got the webpack angular2 starter kit from here : here

However ,I can't figure out how to get rid of these errors as my build is failing because of them .

This is my typings.json

     {
       "globalDependencies": {
         "angular-protractor": "registry:dt/angular-protractor#1.5.0+20160425143459",
         "angular2-beta-to-rc-alias": "file:./node_modules/@angularclass/angular2-beta-to-rc-alias/src/beta-17/typings.d.ts",
         "core-js": "registry:dt/core-js#0.0.0+20160317120654",
         "hammerjs": "registry:dt/hammerjs#2.0.4+20160417130828",
         "jasmine": "registry:dt/jasmine#2.2.0+20160505161446",
         "jquery": "github:DefinitelyTyped/DefinitelyTyped/jquery/jquery.d.ts#7de6c3dd94feaeb21f20054b9f30d5dabc5efabd",
         "node": "registry:dt/node#6.0.0+20160514165920",
         "selenium-webdriver": "registry:dt/selenium-webdriver#2.44.0+20160317120654",
         "source-map": "registry:dt/source-map#0.0.0+20160317120654",
         "uglify-js": "registry:dt/uglify-js#2.6.1+20160316155526",
         "webpack": "registry:dt/webpack#1.12.9+20160321060707"
       }
     }

I've tried :

   typings install dt~jquery --global --save

As well , but the error is still here :

      Property 'css' does not exist on type 'ElementFinder'.

And :

      Subsequent variable declarations must have the same type.  Variable '$' must be of type 'cssSelectorHelper', but here has type 'JQueryStatic'.

回答1:


You'll need to define jQuery typings, I'm using :

https://raw.githubusercontent.com/DefinitelyTyped/DefinitelyTyped/7de6c3dd94feaeb21f20054b9f30d5dabc5efabd/jquery/jquery.d.ts

And it's working fine for me .

I had the same issue and I had to download this typings myself and put it in my folders somewhere .

Then in your tsconfig.js

  "files": [
    "./src/app/**/*.ts",
    "./src/platform/**/*.ts",
    "!./node_modules/**",
    "./src/custom-typings.d.ts",
    "./src/jquery-typings-custom/index.d.ts",  !!!!!!! This is the folder where I put those typings downloaded from the url
    "./src/vendor/slick/slick-typings.d.ts",
    "./typings/index.d.ts"
  ],

And then , in one of your components ( probably the root one , which is normally app.ts ) you should do :

       declare var jQuery : JQueryStatic;

And then instead of using $ , you should start using jQuery(There is a workaround for this issue as well , but I didn't care ) .



来源:https://stackoverflow.com/questions/37605846/jquery-with-typescript-property-css-does-not-exist-on-type-elementfinder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!