Skip to content

Instantly share code, notes, and snippets.

@brauliodiez
Last active October 1, 2017 12:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brauliodiez/9fc2258c806a99725e9803faf01fd50a to your computer and use it in GitHub Desktop.
Save brauliodiez/9fc2258c806a99725e9803faf01fd50a to your computer and use it in GitHub Desktop.
Very basic intro, typescript types

Typescript getting started

Let's get started with Typescript.

We will use Typescript online playground for this demos:

http://www.typescriptlang.org/play/

Steps

  • Let's start with something very basic, if we paste this code on codepen (javascript):
let a = 3;
let b = 5;

b= "5";

const result = a + b;

console.log(result);

Instead of getting 8 as expected result, we will get the following result: 35

  • Let's paste this code into typescript playground and we will notice that we get type checking for free.

Type already infers that a and b vars are typed (number) and throws an error indicating that the b assignment "5" is wrong.

  • We can be more explicit and define the types:
- let a = 3;
+ let a : number = 3;
- let b = 5;
+ let b : number = 5;

b= 5;

const result = a + b;

console.log(result);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment