TypeScript: Definition
What is TypeScript?
In web development, TypeScript is one of the most popular programming languages. It is similar to JavaScript but with some additional features. In fact, TypeScript cannot be executed by a browser or Node.js runtime whereas JavaScript can.
TypeScript is a superset of JavaScript, meaning it includes all of JavaScript’s functionalities and additional language constructions for advanced features.
TypeScript is a strictly typed programming language, meaning it concerns with the shape of objects and functions. This helps to detect type errors early in the development process, allowing for better maintainability and scalability of the code. Additionally, TypeScript is compatible with JavaScript, so developers can continue to use existing JavaScript libraries and tools while benefiting from the additional features of TypeScript.
An object’s shape describes what properties and methods. For example, in the following object:
const person = {
firstName: 'Eric',
lastName: "Alvarez",
age: 29
}
The shape of the person
object is:
firstName
is astring
lastName
is astring
age
is anumber
.
If we want to define this shape as a custom type, it would be something like this:
type Person = {
firstName: string,
lastName: string,
age: number
}
Therefore, we can define an object with an explicit type like this:
const person: Person = {
firstName: 'Eric',
lastName: "Alvarez",
age: 29
}
You can find more information in the TypeScript handbook.
Why would you use TypeScript instead of JavaScript?
Essentially, TypeScript provides a more robust environment for detecting common errors during development. The adoption of TypeScript can result in more robust software and more efficient development. Some of the reasons would be:
- Offers strict typing
- Greater ease of refactoring, the support of strict typing allows for greater ease in refactoring existing code.
- Greater organization and structure, the support of strict typing in a certain way guides you to generate more structured code and to follow certain guidelines.
- Offers compatibility with JavaScript, TypeScript does not prevent you from continuing to use JavaScript where you consider it appropriate.
When should you choose JavaScript over TypeScript?
TypeScript can be excessive for the development of small projects with few lines of code. On small or low complexity projects, the strict typing system can be considered a little bit overkill.
In development teams that are not familiar with TypeScript, it can be a decrease in development speed. JavaScript is a more widely known and used language so using it can simplify development in a certain way.
Each situation should be evaluated and careful decision should be taken.
What is zod?
Zod is a library of TypeScript schema declarations and validations. Zod provides
a strict and readable syntax for describing and validating data schemas: string
, number
, object
, boolean
,…
With Zod, you can describe the types of variables, functions and objects using a simple and easy-to-understand syntax, which helps to detect type errors at runtime and to ensure that the data is valid.
The goal of Zod is to eliminate duplicate type declarations. With Zod, you can declare a validator and Zod will automatically infer the TypeScript static type. You can find more information about inference in this post.