Sunny
Sunny

Reputation: 2332

Why does TypeScript require "declaration files" to use external libraries?

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)?

Upvotes: 0

Views: 440

Answers (1)

Louay Alakkad
Louay Alakkad

Reputation: 7408

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');

Upvotes: 4

Related Questions