4

I have already seen other SO questions. But still confused with my question.

I have seen many examples which use Jquery(js), Toastr(js) toastr without typings and other JS library directly in angular2 app without using their type definition (.d.ts) files and they work just fine.

So,

1) basically what is gain or lose, going and not going with type definition file?

2) Angular2 files are also javascript files. So what about their type definition files ?

Note : I haven't tried with NPM so I don't know if when you actually install Angualr2 package ,it adds all related t.ds files in typings folder.

1 Answer 1

12

Typescript Definition files contain the type information for code written in Javascript.

Javascript doesn't contain type information itself, so Typescript can't magically retrieve that information. To solve that problem Definition files are created which tell Typescript what types are being used where.

So, you can use 3rd party libraries without a Definition file, but you'll lose out on the type safety that Typescript offers.

For example, if you include the jQuery definition file then your IDE can now provide intellisense/auto-suggestions for the jQuery API. Also the Typescript compiler can give you a warning when you try to pass a boolean to a function that expects a string.

So definition files aren't required for Typescript to work, but without them you lose a lot of the benefits Typescript provides.

Sign up to request clarification or add additional context in comments.

3 Comments

First, thanks for the correction.Second, so ultimately It doesn't matter whether you add .d.ts file or not?
I always add them because I always want the type information. Why use Typescript if you don't want the typing? The type safety helps you catch errors in your code and can make coding faster by providing hints as to what you can use. EDIT: To more directly answer your question ... They aren't required to make the code work. But they add a lot of value in the development process.
Thanks a lot for your answer... but still wait for few mins and let's see if some one else wants say something else I will accept it as an answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.