DEV Community

Discussion on: Seven lessons I wish I learned earlier about Typescript

Collapse
 
jwp profile image
John Peters

I am a Typescript advocate, who also loves JavaScript, I am fluent in both styles. I do understand your frustration as I recall my first few months in JavaScript after 10 years in Java and C#. Just like you described, I was highly irritated that things I knew how to do in Java or C#, were impossible for me (at first) to do in JavaScript. I didn't even know how to ask questions that made sense to JavaScript people. For example why is it when I console.log a JavaScript object it shows all the properties and values, but when inspecting the object at runtime it only shows [object, object]. Wasn't there any way to use reflection on the object to find property values and names? Only later did I learn about Object.keys and values.

However there are some attributes to TypeScript that are interesting

  • Any filename.js file can simply be renamed to filename.ts and it's now a typescript file.
  • It should work if there are no errors in the code.
  • This means that nobody has to become fluent in Typescript immediately, they can pick it up a little bit at a time.

The parts you mentioned on TypeGuards and Generics are a bit more advanced for new folks and are not immediately required unless your company demands it.

My summary on Typescript is that it's nothing more than annotation. E.g.

// the type of person follows an interface definition or class definition.
// even it getPerson only returns a non typed object.
let person :Person = getPerson();
person.firstName = "FirstName";
person.MiddleName = "Middle"
// This is flagged as an error while typing, opposed to runtime.
person.LastName = 504;

// A class is the same thing as an interface definition except it allows initialization!


class Person {
lastName:string;
firstName:string;
middleName:string;
}

I suggest learning it slowly. It does save time after you know it as runtime errors are cut in half. Plus intellisense auto discovers APIs as we type.