Last active
May 12, 2022 11:28
-
-
Save thelbouffi/43e10e0741f6e7c64970d33a40ed9eb8 to your computer and use it in GitHub Desktop.
What are Typings in Typescript?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
JavaScript is untyped, meaning that we can pass around data and objects as we want. | |
We can write code that calls methods that don't exist on an object, or variables that we don't have. | |
These problems are hard to discover when you are writing code and it can lead to unstable code, | |
and doing big changes of your code can become very difficult and risky as you don't immediately see if your changes | |
conflicts with the rest of the code. | |
TypeScript is mainly about adding types to JavaScript. That means that TypeScript requires you to accurately | |
describe the format of your objects and your data. When you do that, that means that the compiler can investigate | |
your code and discover errors. It can see that you are trying to call a function that does not exist or use a variable | |
that is not accessible in the current scope. | |
When you write TypeScript yourself, the description of the code is part of the code itself. | |
However, when you use external libraries like jQuery or moment.js, there are no information of the types in that code. | |
So in order to use it with TypeScript, you also have to get files that describe the types of that code. | |
These are type definition files, most often with the file extension name .d.ts, and fortunately people have written | |
those kinds of definition files for most common javascript libraries out there. | |
Typings was (is) just a tool to install those files. | |
When you have installed those files, which only means downloading them and placing them in your project, the TypeScript | |
compiler will understand* that external code and you will be able to use those libraries. | |
Otherwise you would only get errors everywhere. | |
* Depending on how you have set up your project and configured it, you might have to configure typescript to look | |
for those files specifically, or it might just work without any configuration from your part. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment