Skip to content

Instantly share code, notes, and snippets.

@jonepl
Last active July 22, 2020 01:54
Show Gist options
  • Save jonepl/afaabdb98d43dd2ef4c943625f4af756 to your computer and use it in GitHub Desktop.
Save jonepl/afaabdb98d43dd2ef4c943625f4af756 to your computer and use it in GitHub Desktop.

TypeScript Basics

TypeScript is a superset of JavaScript. TypeScript uses transpilation to transpiles the TypeScript into vanilla JavaScript. TypeScript gives you build time checking to avoid runtime errors by enforcing static typing.

Feature Enabled by using typescripts

  • Static Typing
  • Interfaces
  • Class Properties
  • Public/Private Accessiblity levels

Static Typing

Here are some examples static typing. View more static types HERE

let name : string
let age : number
let birthDate : date

Interface

interface ICat {
	name: string
	age? : number  // ? optional
}

Classes

TypeScript classes are public by default. Below is a basic example of a TypeScript class

class Cat {			
	name:string                 // static type safe
	color			            // no type safe coverage
	constructor(name){
		this.name = name
	}

	speak() { console.log("Speak")}
}

let fluffy:ICat                 // class variable declaration

Accessiblity levels (Public/Private)

You can change the accessibilty of member variable or function. Example below:

class Cat {
	private name: string
	prviate color: string
	constructor(name, color){
		this.name = name            // initializes memeber variable
		this.color = color
	}
    private speak() { console.log("Speak")}
}

Alternative class method to use accessibilty in classes

class Cat {
	constructor(private name, private color) {
	...
	}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment