Typescript: Initial Impressions
Recently I attended a coding meetup where there was a presentation on typescript. I was left so inspired by what I saw and learned that I immediately signed up for classes to find out more about this useful tool. Previously I had been noticing the requirement for aptitude with typescript showing up on many positions I had been applying for. I have been working with it for a bit now and this article will cover some of the things I have learned and perhaps provide a brief presentation of typescript that will inspire others to begin learning it as well. First we will go into how to install and use typescript
How To Install Typescript
In order to install typescript you will first need Node.js as an environment to run the package. If you are using node package manager enter the following into terminal to install typescript:
npm install typescript --save-dev
This will allow you to create .ts files in your text editor which you will compile into javascript by the tsc command followed by the filename (as long as your terminal is in the same directory as the file). An example of compiling a .ts file called app.ts via the terminal would be by entering:
tsc app.ts
this will create a file in the same directory consisting of the javascript code compiled by app.ts the file will be called app.js it is important to note that if you are running javascript as a script in your html file you will need to use this app.js file. This is because html has no idea what to do with a .ts file. If you are interested in some of the syntax that this .ts file might be written in please examine the typescript docs. Next lets try’n answer a question you might be asking…’but WHAT IS typescript???’.
What Is Typescript?
Typescript is javascript that adds additional syntax to support type enforcement. It is written in a .ts file and compiles to javascript that is cleaner and more concise than what you would generally write on your own. It also provides many handy error messages which reference type enforcement that will fire off once the typescript is compiled into javascript. These error messages will not prevent this javascript from running or be presentable on a browser (browsers don’t know what to do with typescript, it is a language for development only). These types are closely aligned with javascript datatypes. If you followed both of those links you will notice a few distinct differences. We will go into some of these types in the next section.
A Few Data Types In Typescript
I’ll now go into a review of some of the types I have learned about and the syntax required to define values with them. Without further a-do here are some of the different types in typescript:
- Tuple-is an array with fixed specified types. Here are some examples of how to define a tuple:
- Enum-is a type which by default provides labels to numbers in sequence. This sequence can be changed at any point during initialization and the sequence will continue where ever you left off. An example of an enum initialization would be:
- Any-is a type that means it’s value can be of any type. They are less useful because they negate a lot of the development error message guard rails that typescript provides and should be avoided. A use case scenario may be if you are unsure of what types something might be and need to support all types for that value. An example of setting an array to being able to hold any type would be:
- Union — is a type which allows for multiple defined types. This is different than the any type because you are still limiting its value to the types you define. An example of a union type might be:
- Literal — is a type that will only allow specified values(s). This can be useful to detect a misspelling for a string argument in a function that uses a case switch statement, or to only allow certain enum values as arguments for a function without throwing a typescript error. An example of a function that utilizes a literal type might be:
Conclusion
Typescript is a useful tool for writing clean javascript code that functions exactly the way you intend it to. It also easily communicates to other developers what you desire your code to do. When developing with typescript you are compelled to adhere to the types you define thus unknown values occur less frequently (if at all). I am still learning this technology but as of right now I like to use the analogy in my head of: I can draw a circle freehand, or I can draw a circle better using a protractor. I can write working javascript, or I can write precise javascript using typescript.