Can TypeScript interact with jQuery without a definition file?

后端 未结 3 1866
广开言路
广开言路 2021-02-05 06:15

I have been attempting to get to know this new \'TypeScript\' stuff, and I am a bit curious on something.

Can it still work with existing javascript frameworks like jQuer

相关标签:
3条回答
  • 2021-02-05 06:25

    Yes you can. For example just write:

    declare var $;
    

    and you can basically use the JQuery framework without having to define anything else. This is also very handy when you are converting your existing libraries / porting code.

    0 讨论(0)
  • 2021-02-05 06:39

    Typescript allows you to declare variables in the descired scope using the declare variable or declare function syntax (see Section 1.1 on page 9 in the language specification). However, using ambient declarations can only be a short-term solution since you will effectively loose all of Typescript's static type checking and hence one of the most important advantages of Typescript over Javascript.

    0 讨论(0)
  • 2021-02-05 06:47

    The simple answer is yes.

    TypeScript is able to interact fully with any existing Javascript library. You only need the definition file if you want tooling in the IDE to make it easier to use.

    Also, if you don't include the definition file, the TypeScript compiler might get mad at you for using a variable that hasn't been defined in your code (like $). To get around that you might have to do something like

    declare var $;
    

    That said, I'm not sure why you wouldn't want to use the jQuery definition file. It surely makes it much more pleasant to write jQuery with.

    0 讨论(0)
提交回复
热议问题