This way, before you run the codes, you will be able to spot errors. Moreover, you will also get a better idea about how you can resolve the errors which are provided by the TypeScript compiler.
What is TypeScript?
Popularity of TypeScript
If we look at the journey of TypeScript from 2016 to 2018, you will see that the number of users who have used TypeScript and are willing to use again has increased from 20.8% to 46.7%. One of the main reasons why developers choose it is that it is prone to fewer errors.
So let’s compare them…
|Code Compilation||You don’t need to compile the code in TypeScript.||You need to compile the code in TypeScript.|
Top Features of TypeScript that Developers Enjoy
Unknown Top Type
It was in TypeScript 3.0 that ‘unknown type’ was introduced as a new ‘type-safe’ which is counterpart of ‘any’ type. What differentiate the two ‘unknown’ and ‘any’ is that ‘unknown’ is much lenient that ‘any’ (‘any’ has been the part of TypeScript since 2012). It does much like ‘any’ as you can assign any value to ‘unknown’, though type assertion is significant here as it cannot do anything without it. Besides, you are not allowed to access or call/construct any properties off of an ‘unknown’ type.
Mapped Types on Arrays and Tuples
TypeScript didn’t go ahead with the way of having a new concept when it comes to a mapping over a tuple, rather in TypeScript 3.1 you have mapped object types. This works exactly the way it is supposed to be when interacting with arrays and tuples. This means if you by any means is making use of mapped types like Required or Partial, which already exists, you will see that they work on arrays and tuples automatically. This way, TypeScript can work well for functions which are like Promise.all.
Node.js packages lead to inheritance of tsconfig.json
With the TypeScript version 3.2, you can resolve tsconfig.json from node_modules.TypeScript will look into thenode_modules package when you are trying to reach “extends” field in tsconfig.json.It will go through the folder of node_modules to look for the package of @my-team/tsconfig-base. Before getting further with the package, TypeScript looks for whether tsconfig field is present in package.json and once it finds it, from the field it loads a configuration file. If such a field is not found, then it will go for tsconfig.json. Such a step proves to be useful when handling larger projects.
A new construct for literal values have been introduced with TypeScript 3.4 and it is called as const assertions. Here in the syntax, in the place of type name it makes use of type assertion with const. By building literal expressions along with const assertions, you can signal the language that the object literals will come with read-only properties or the arrays will possess read-only tuples.
Type-checking for globalThis
The issues with accessing values within the global scope have been a problem with TypeScript and this issue got taken care with TypeScript 3.4. This has been achieved with the support that it offers for type-checking globalThis from ECMAScript. globalThis is nothing but a global variable which works for global scope. This variable offers easy access to the worldwide scope from any environment that you prefer. Again, you need to know that globalThis does not show up two of the global variables namely const and let.
Support for JSX from defaultProps
TypeScript 3.0 comes with support for a new type for the React development within JSX called as LibraryManagedAttributes. This type works as a helper to define changes in the components of prop types before it is used. This way, it becomes possible to have modifications around inferred and provided mappings and props.
You need to make a note that it will not be possible for the compiler to identify the default properties because of the added explicit type annotation as the default properties are inferred from property type of defaultProps.
Benefits of TypeScript
It is an object-oriented language
This object-oriented language comes with features like interfaces, classes, inheritance, modules, etc.
Static typing is optional
TypeScript comes with the optional static type system and it can be considered as one of the main reasons to choose it. You can add types to properties, functions, variables, etc. So before the app even runs, it will help the compiler to display any warnings regarding any potential issues if present. By letting developers know what kind of type APIs need or want, types work as a helping hand when using frameworks and libraries.
Something that you need to remember at this point is that using types is an optional thing. This means that developers are not forced to use any type which they don’t want forcefully by TypeScript. However, it has to be noted that types will prove to be helpful when the apps become huge and complex with time.
TypeScript is portable
When it comes to front-end development, it is possible for the developers to save enough time if they have some help which can spot any errors made during the programming process. Now it is at this point you need TypeScript. With TypeScript, you can be sure that your codebase will be free from invalid code.
One of the main benefits of using TypeScript for your project is Intellisense which helps with code completion. As the code gets added, Intellisense offers active hints. All the important IDEs in the market today enjoys the support of the facility of code completion which includes Sublime Text, Atom, WebStorm, VScode and this also includes command-line editors.
TypeScript is like JS