0

If TypeScript is supposed to be a strict superset of JavaScript (as advertised), then why can't I simply import an external library without having to reference a corresponding d.ts file for it to work? Why can't I just use the plain JavaScript library as is (without type checking)?

Sunny
  • 2,232
  • 7
  • 24
  • 36

1 Answers1

4

It doesn't "require" you to do so. You can use external libs without declarations.

The whole idea behind typescript is to avoid typos and incorrect type handling. If you were to use external modules without any type information then what's the point of typescript?

Lastly, you can avoid the need for declarations by using the following syntax:

const module = require('module');
Louay Alakkad
  • 7,132
  • 2
  • 22
  • 45
  • I use TypeScript to compile down ES6 code for old browsers, and for type checking my own code. I don't need 3rd party libraries to be typed, but Visual Studio still complains "cannot find name" if I don't reference a `d.ts` file for it, that's the issue. – Sunny Dec 20 '15 at 22:44
  • For the 1st case you can just use babel. It's all about type-checking. For the second, just use "const" instead of "import". – Louay Alakkad Dec 20 '15 at 22:48
  • I'm using `const` now but I still get the error. I'm using Visual Studio 2015 and TypeScript 1.7.4. – Sunny Dec 20 '15 at 22:57
  • I'm not sure about VS15, but I use VSCode and it works well – Louay Alakkad Dec 20 '15 at 22:58
  • Same error on Visual Studio Code, I don't think `const` has anything to do with it though, it wants a `d.ts` file. – Sunny Dec 20 '15 at 23:08
  • I think the issue could be in another file then. [There is a difference.](http://stackoverflow.com/questions/29859748/typescript-var-vs-import) – Louay Alakkad Dec 20 '15 at 23:09