Friday 21/08: 8AM – 2PM
I finished a PR today and spent the rest of the day learning about TypeScript (TS). I’ve been following a LinkedIn Learning tutorial supplemented with some YouTube tutorials.
TS is a super set of JavaScript (JS) which introduces type definitions to JS. TS, when implemented correctly, makes your application more robust and easier to debug. Alone, JS is loosely typed as well as dynamically typed. JS can do type conversions automatically and doesn’t require type declaration. This is often the reason some people love JS and some people despise it. In comes TS, which introduces both basic types such as string, int, bool etc as well as custom types which you can declare yourself.
TS types don’t only apply to variables, you can assign types to pretty much everything, functions, parameters and variables can all be typed in TS. TS also makes it easier to make your code OO by utilizing classes, interfaces and inheritance.
TS type definition examples
let isDone: boolean = false;let decimal: number = 6;let hex: number = 0xf00d;let color: string = "blue";enum Color {Red,Green,Blue,}let c: Color = Color.Green;
With the introduction of types comes better error handling. Since TS is aware of the types of data, it can notify you when you are breaking these declarations, assuming you’ve defined them of course. Sometimes you may not know what type something is in which case you can assign the Any type.
While I was learning about the OO capabilities TS introduces, I learned about the prototype object in JS. It’s important to understand how regular JS handles inherticance using the prototype object so you can see how TS builds upon this to make it more like a pure OO language such as C#. This video is a good explanation of how regular JS uses the prototype object.