Skip to content

Instantly share code, notes, and snippets.

@Rogersiver
Created July 5, 2021 01:43
Show Gist options
  • Save Rogersiver/b41e110e3cf67dbf624fe924d205c308 to your computer and use it in GitHub Desktop.
Save Rogersiver/b41e110e3cf67dbf624fe924d205c308 to your computer and use it in GitHub Desktop.

Typescript

  • what is typescript?
  • why would we use it instead of vanilla js?
  • what does it mean for something to be compiled at runtime
  • what is a type system?

Typescript is javascript with more rules. At this point in "Operation Spark" the coding bootcamp that I'm in, we're getting pretty familiar with vanilla javascript. we've managed a filesystem with bare node, and instantiated objects every possible way up to ES6 class creation. It's hard to imagine yet another way to create things in your javascript environment but here we are. I set out to answer a handful of questions for my class.

What is TypeScript?

Well according to them

"TypeScript offers all of JavaScript’s features, and an additional layer on top of these: TypeScript’s type system."

In javascript you can reassign a variable to whatever dataType you would like. In javascript you have to run your code to see how the javascript interpreter is going to compile it, and you won't see dataType/variable assignment type errors until runtime. Typescript solves this problem by compiling your code in the editor, and showing any run time errors related to syntax or datatype right in your editor. In typescript when you declare a variable it has a set datatype that cannot be changed

Everybody has seen https://gist.github.com/052a5c9e89bf3c7b208cc92e2bbbf1a0

TypeError: Message is not a function

You have to write all your code and run it to get this far. that sucks. In a typescript file with the same code in it, in your editor of choice, you will get a line annotation

This expression is not callable. Type 'String' has no call signatures.

##Useful, helpful, verbose.

These examples are from Typescript Docs: The Basics, by the way.

https://gist.github.com/e4ee398bc5092cc48d8db7bdf122c5d4

Were gonna see that user location doesnt exist without a terminal, no chrome console is needed to see that we need to change user.location or our code isnt gonna work. Have you seen the handy refactoring features inside vscode? We can extract a function out of its scope or easily convert asyncronous functions. Typscript is handling this in the background! VScode is written in typescript and ran in an electron environment. Good stuff, I'm excited for the day when I can ditch javascript for typescript for large projects with variables and datatypes flying around. We can write javascript and run it way more confidently, at a larger scale. Typescript Typscript Typescript.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment