Introduction to TypeScript
Dec 9th, 2023Hello all! I've recently been learning TypeScript. It's something I've wanted to do for quite some time now, so I'm happy that I'm setting aside time to do so!
This post will briefly introduce TypeScript. Currently, I'm still a novice. However, since TypeScript is a superset of JavaScript (a language I feel quite comfortable with), and since I feel very comfortable with static typing from learning C# in my current workplace, I feel confident that I'll learn TypeScript quite quickly and will thank myself for doing so in the future! Before jumping directly in, I do want to begin by describing why TypeScript is a useful addition to JavaScript.
Also, as a quick disclaimer, all of the information regarding TypeScript comes from what I have learned by using TypeScript, as well as from the JavaScript portion of the docs.
JavaScript is what's known as a loosely (or weakly...or dynamically) typed language. Per the wiki page I provided, "A weakly typed language has looser typing rules and may produce unpredictable or even erroneous results or may perform implicit type conversion at runtime. Advocates of dynamically typed languages find such concerns to be overblown and believe that static typing actually introduces an exponentially larger set of problems and inefficiencies."
That portion of the wiki sheds light on both the pros and cons of loosely typed languages. Speaking as someone who learned to code using JavaScript, I understand the many opinions on loosely typed languages.
For those who are brand new to programming in general, I would recommend them to start with a loosely typed language. Learning to code is already a monumental task in and of itself. A loosely typed language has a lower barrier of entry and gets new programmers writing code that much faster.
Alrighty, back on track.
Like I said before, TypeScript is a superset of JavaScript. This simply means it's built on top of JavaScript. It has all of the features that JavaScript contains and more!
TypeScript determines types by inference. The old saying of, "if it looks like a duck" applies here because TypeScript will infer types based upon what the variable "looks" like. Take the following variable declaration for instance:
let str = "Hello Reader!";
TypeScript determines types based upon contextual information. In the example above, the inference was determined by the variable declaration. Though, if the developer prefers to manually input types, that's also possible.
Speaking of static typing, TypeScript gives the power of static typing to the loosely typed language that is JavaScript! With it, the developer has a firm distinction between data types in their JavaScript code. This distinction will help prevent bugs arising in the future.
TypeScript also introduces interface declarations. Interface declarations are quite useful for declaring an "abstract" for an object before creating the object itself. The following code displays an example interface:
interface Pet {
name: string;
age: number;
}
That interface can now be used to create a "Pet" object. If a "Pet" object is declared incorrectly, TypeScript will display a warning message. Interfaces require more code upfront for less hassle in debugging later.
Following that thought, a big part of TypeScript (and statically typed languages in general) is that more code now is better than more bugs later. For smaller-midsized projects, vanilla JavaScript is less of a hassle. For larger projects, TypeScript will certainly help reduce the amount of debugging required.
There's still quite a bit to TypeScript I haven't touched on. TypeScript's typing system and interface declarations will get novice TypeScript users like myself quite far in their TypeScript projects. So, I think I'll leave it at that.
Thank you for reading!