Common Mistakes While Learning TypeScript
Mistake 2: Annotating Everything
When you begin your learning experience with Typescript, you would want to be annotating for expressing the type you expect. This would also feel like the obvious thing to do in the beginning, but it is much better to make use of annotations sparingly and let TypeScript do the work of figuring out the type. A type annotation serves as a way for expressing where a contract requires checking. If type annotations are added to a variable declaration, it tells the compiler to check the type and match it during the assignment.
A type annotation is a way for you to tell the compiler where contracts have to be checked. If a function returns a type that is not compatible with the assigning variables, an error is shown by the Typescript compiler. In order to use annotation, you want to ensure that you are not making mistakes with the types, and using the correct ones.
Once a type is identified, TypeScript will treat it like that, and any more properties in that type will not be allowed to access if the type does not match. If type annotation is applied to the return value of the function signature, it is essentially telling the compiler to match the moment the value is returned. If the function returns something that does not match the variable on the other side of the assignment, an error will be indicated by the Typescript compiler.
It is imperative to ensure that the correct type is being returned. This is useful when you are dealing with functions that are constructing big objects from different sources. If you are adding type annotations to any of the parameters of a function signature, you are essentially indicating to the compiler to be checking the type matches the moment it is passed along as arguments. Make sure annotation is used only when it cannot be inferred.
Annotations should always be used with the parameters of a function because this is where the checking of contracts is done. This is convenient and also has many advantages such as polymorphism. If too many annotations are used, there may be a need to create numerous types and checks than is actually necessary, with no extra benefits. When beginning with Typescript, not using annotations too much will also help you understand and get a feel for what is the meaning of working with a system of structural types.
Mistake 3: Mistaking Types For Values
TypeScript is confusing because it does not work similar to other programming languages and needs to make a separation between types and values. There can be types of calls and classes can also make contributions to 2 types. It is important to understand what contributes to a type and what to a value, where the boundaries lie and which direction to move. While learning typescript, it is good to keep your focus on variables, functions and simple aliases for types. This will be quite helpful to you in gaining a good understanding of what goes on in the type layer and value layer.
Mistake 4: Go Go Go In The Beginning
Mistake 5: Learning The Wrong Typescript
If you make use of keywords such as declare, namespace or module, you probably are using TypeScript wrong. It does not mean that this keyword makes no contribution and are unnecessary for variety in type-cases. But avoid working with them when beginning with Typescript.