Typescript: Initial Impressions

How To Install Typescript

npm install typescript --save-dev
tsc app.ts

What Is Typescript?

A Few Data 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:
In this example if you added another enumerator between the brackets you would have to initialize it with a value much like NEW_COUNT did when it was set to 10. This is because STR breaks the count by being a of a type other than a number (in the case of STR it is of type string).
  • 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:
by declaring this without initializing it you can later set its value to something like: arr3 = [“string”] OR you might also set it to something like: arr3 = [1, true] …again NOT generally a good idea to use this type if you can avoid it.
  • 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:
It is a good idea to include some run time checks when using union types to provide different use cases such as the if (typeof) statement in the function above. In this example if input1 & input2 are of type number the return will be both of these added together. If they are of type string a concatenated string will be returned.
  • 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:
In this example the argument of role not only has to be of the string type but must be either admin, user, or guest. This function will return an object with privileges that correspond with the given role.

Conclusion

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Google, Intel, and Twitter Talk Node.js

Phase-2 “REACT.JS”

SPFx extension — Convert to PDF

React Context and the UseContext hook

A coding screen

An Introduction To NodeJS

React PureComponent Pitfalls

Removing the Magic Veil of Algorithms

Javascript Function Chaining. How jQuery ruled the Web.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ryan Schleck

Ryan Schleck

More from Medium

Typescript: Type VS Interface

Syntactical Types in JavaScript: TypeScript

7 Reasons to run React JS for speed boosting in software development vs Angular JS

Const myFirstJavascriptBlog = “Declaring Variables”