SECRET OF CSS

TypeScript for Deno: Types and Typing | by Eduardo Rico | Jun, 2022


Get started with data types

0*OWjEjtneCtstylOR
Photo by Nate DeWaele on Unsplash

It is always necessary to update and learn new things that’s why I made this series of notes for those who want to get into the world of TypeScript and especially to make applications with Deno. Obviously, this information is useful if you want to program with Nest.js or include typing in your applications.

Specially made for beginners but surely if you already know TS you can reinforce something.

TypeScript is great for when you want to undertake a medium to large project because it increases the readability of the code in many aspects, it is also great when you are working in a team because you avoid being watching what kind of parameter a function needs and the best of all from my perspective are the observations and the autocomplete that is autogenerated.

JavaScript knows about typing in fact it has several data types but because it is dynamically typed and weakly typed JS is not interested in talking about data types. Dynamic typing is when a variable can first be an integer and then a string in runtime, weak typing tells us that a variable can be declared without the data type.

The primary difference in typing between TS and JS is that TS uses static typing, that is, it checks everything in the compiler and if there is an error it warns you, so it is wonderful when you are programming. On the other hand, JS is dynamically typed, that is, you assign types to variables but at runtime.

In TypeScript primitive types are lowercase: string, number, boolean. It also has other types such as ani, void, null and undefined.

Although TypeScript is typed, the compiler can infer the data type. All types are declared after the variable name and with a colon, for example:

Strings

Numbers

Booleans and Arrays

Booleans are over understood, however, TS allows us to “forget” about Truthy and Falsy because now TS allows us to verify before executing the code, however, at runtime they behave the same. I remind you:

The ones that when evaluated take the value of “true” are:

And those that take the value of “false” are:

Arrays also exist as a type, but it is what is called a generic type, that’s why a <T> appears when you initialize it.

Keep in mind that just as in JS, when you declare either an array or an object as a constant, what is constant is the object (or array) pointer and can be modified perfectly well.

Tuples and objects

They are the same as the arrays with the difference that you cannot change the length of the array, neither can you change the type.

The tuples here are used to organize information according to the type of data and how many of them there are.

On the other hand, the objects literals or literal objects can also be typed, as in the second person. But this is not necessary because TS infers the data of each property, even of the methods.

Enums

Basically it is a list of numbers that are human readable although it can also be handled the other way around, as a list of words with one number per key. By convention the first letter is capitalized. And by default the enumeration starts at 0 but you can change it and the following values will be the subsequent ones starting from the chosen number.

In line 10, you can clearly read that my favorite color is blue, in comparison to line 11 where, just reading that line, you don’t know what color it is.

It also happens with lines 12 and 13, where the readability of the code is much higher in line 13 than in the previous line. The enums are a marvel.

In the next post I will talk about the data types: Any, the union types, the custom types and the aliases types in addition to the literal types.



News Credit

%d bloggers like this: