by @leocaseiro
"Microsoft TypeScript is an open-source programming language developed and maintained by Microsoft. It is a strict syntactical superset of JavaScript, and adds optional static typing to the language."
1100 1010
1010 0101
0101 1100
compile to
transpile to
different level of abstration
same level of abstration
*.ts
*.js
* TypeScript interface (object)
** not covered on this course
typeof "text";
// string
let a = null;
console.log(a);
// null
typeof a;
// 'object' What?
There are two features of null you should understand:
Undefined most typically means a variable has been declared, but not defined.
You can also explicitly set a variable to equal undefined:
let b;
console.log(b);
// undefined
var d = {};
console.log(d.fake);
// undefined
var u = undefined;
let a = true;
console.log(a);
// true
let b = 1;
console.log(b);
// 1
console.log(a == b);
// true
console.log(a === b);
// false
let c = false;
console.log(c);
// false
let c = 0;
console.log(d);
// 0
console.log(c == d);
// true
console.log(c === d);
// false
let a = true;
typeof a;
// boolean
let b = 1;
typeof b;
// number
Not the same as truthy and falsy
let a = 123;
typeof a;
// number
let b = 12.3;
typeof b;
// number
console.log(a > b);
// true
let c = "123";
typeof c;
// string
let d = "12.3";
typeof d;
// string
console.log(c > d);
// true
let a = "John";
typeof a;
// string
let b = 'Doe';
typeof b;
// string
console.log(a + b);
// 'JohnDoe'
console.log(a + " " + b);
// 'John Doe'
let a = `John`;
typeof a;
// string
let b = `Doe`;
typeof b;
// string
console.log(`${a} ${b}`);
// 'John Doe'
typeof `123.00`;
// string
` (back-ticks)
let person = {
firstName: "John",
lastName: "Doe"
};
typeof person;
// object
typeof person.firstName;
// string
typeof window;
// object
typeof document;
// object
function hello(name) {
return `hello ${name}`;
}
typeof hello;
// function
class Calculator {
sum = (a, b) => a + b
}
typeof Calculator;
// function
*syntax sugar for functions
var hello = (name) => {
return `hello ${name}`;
}
typeof hello;
// function
let mixed = Array(1, 2, 'text', {name: 'John'})
// mixed = [1, 2, 'text', {name: 'John'}] // same
typeof mixed
// object
Array.isArray(mixed)
// true
typeof mixed[0];
// number
typeof mixed[2];
// string
typeof mixed[3];
// object
typeof mixed[4];
// undefined
let bday = new Date(1991, 11, 25);
typeof bday // Object
console.log(bday);
// Wed Dec 25 1991 00:00:00
// GMT+1100 (Australian Eastern Daylight Time)
new Date(year, monthIndex [, day [, hours [, minutes [, seconds [, milliseconds]]]]]);
let user = "John Doe"
declaration
set
value
name
let user: string = "John Doe"
declaration
set
value
name
annotation
type
let xyz: number | string = r;
or
type
type
const numbers: Array<number> = [1,2,3,4]
const oddNumbers: number[] = [1,3,5,7,9];
interface Person {
firstName: string;
}
let user: Person = {
firstName: 'John'
}
user = 'John Doe';
// Type '"John Doe"' is not assignable to type 'Person'.
interface Person {
firstName: string;
}
let user: any = {
firstName: 'John'
}
user = 'John Doe';
--noImplicitAny = false
function foo(arg: string): string {
return arg;
}
foo('test'); // return string
foo(123);
// Argument of type '123' is not assignable
// to parameter of type 'string'.
function foo<T>(arg: T): T {
return arg;
}
foo('test'); // return string
foo(123); // return number
function foo<T>(list: T[]): T[] {
console.log(list.length);
return list;
}
5 minutes
npm start
function sum (n1, n2) {
const r = n1 + n2;
return r;
}
declaration
output
return
arguments
name
function sum (n1: number, n2: number): number {
const r = n1 + n2;
return r;
}
declaration
output
return
argument
name
argument
arg. type
arg. type
output type
const greeting = (name: string): string => {
const r = `hello ${name}`;
return r;
}
declaration
output
return
argument
name
output type
argument type
const setName = (n: string): void => {
name = n;
}
no output
10 minutes
npm start
enum State {
ACT = "Australian Capital Territory",
NSW = "New South Wales",
QLD = "Queensland",
SA = "South Australia",
TAS = "Tasmania",
VIC = "Victoria",
WA = "Western Australia"
}
const myState: State = State.NSW;
console.log(myState);
// New South Wales
enum Direction {
Up, // 0
Down, // 1
Left, // 2
Right // 3
}
var d = Direction.Up;
console.log(d); // 0
enum Direction {
Up = 1, // 1
Down, // 2
Left, // 3
Right // 4
}
var d = Direction.Up;
console.log(d); // 1
class Person {
firstName: string;
lastName: string;
constructor(first: string, last: string) {
this.firstName = first;
this.lastName = last;
}
}
const user = new Person('John', 'Doe');
console.log(user.lastName);
// Doe
class Animal {
move(distanceInMeters: number = 0) {
console.log(`Animal moved ${distanceInMeters}m.`);
}
}
class Dog extends Animal {
bark() {
console.log('Woof! Woof!');
}
}
const dog = new Dog();
dog.bark();
dog.move(10);
dog.bark();
class Animal {
name: string;
constructor(n: string) { this.name = n; }
move(distanceInMeters: number = 0) {
console.log(`moved ${distanceInMeters}m.`);
}
}
class Horse extends Animal {
constructor(name: string) {
super(name);
}
move(distanceInMeters = 45) {
console.log("Galloping...");
super.move(distanceInMeters);
}
}
class Person {
private firstName: string;
lastName: number; // public
constructor(first: string, last: string) { ... }
}
const user = new Person('John', 'Doe');
console.log(user.firstName);
// Property 'firstName' is private
// and only accessible within class 'Person'.
class Person {
private age: number;
protected name: string;
constructor(n: string, a: number) { this.name = n; this.age = a; }
}
class Employee extends Person {
constructor(name: string, age: number) {
super(name, age);
}
getName() {
return `Hello, my name is ${this.name}`;
}
getAge() {
return `Hello, my age is ${this.age}`;
// Property 'age' is private
// and only accessible within class 'Person'.
}
}
class Calendar {
private _month: number;
get month() {
if (this._month != null) {
return this._month;
}
}
set month(m: number) {
if (m > 0 && m < 13) {
this._month = m;
}
}
}
strictPropertyInitialization = false
typescriptlang.org/docs/handbook/release-notes/typescript-2-7.html#strict-class-initialization
abstract class Animal {
move(distanceInMeters: number = 0) {
console.log(`Animal moved ${distanceInMeters}m.`);
}
abstract foo(n: string): string; // like an interface
}
class Dog extends Animal {
bark() {
console.log('Woof! Woof!');
}
foo(n: string) { return n; }
}
const dog = new Dog();
dog.move(5); // inherited
dog.foo('bar'); // class 'Dog' implements inherited
// abstract member 'foo' from class 'Animal'.
interface IPerson {
age?: number;
firstName: string;
lastName: string;
}
let user: IPerson = {
firstName: 'John',
lastName: 'Doe'
};
user.age = 18;
interface Person {
firstName: string;
lastName: string;
}
interface
name
property
braces
type
semicolon
(no equal sign)
interface SearchFunc {
(source: string): boolean;
}
parentheses
interface Person {
firstName: string;
}
let user: Person = {
firstName: 'John'
}
interface Person {
firstName: string;
age?: number;
}
const user: Person = {
firstName: 'John'
}
user.age = 18;
optional property
(question mark)
function setPerson(name: string, age?: number) {
// ...
}
optional property
(question mark)
interface Person {
readonly firstName: string;
}
const user: Person = {
firstName: 'John'
}
user.firstName = 'Jane'; // error*
// Cannot assign to 'firstName' because it is a read-only property.
15 minutes
npm start
let foo: string = 'John Doe';
foo = 0;
// Type '0' is not assignable to type 'string'.
let foo: string = 'John Doe';
(foo as any) = 123;
if (false) {
// @ts-ignore
console.log("hello");
}
{
"compilerOptions": {
"outDir": "./built",
"allowJs": true,
"target": "es5"
},
"include": [
"./src/**/*"
]
}
tsc --init
⚠️ TSLint will be deprecated some time in 2019. See this issue for more details: Roadmap: TSLint → ESLint. If you're interested in helping with the TSLint/ESLint migration, please check out our OSS Fellowship program
Enables ESLint to support TypeScript
Code Analysis
+
Code Style Checker
cd app;
yarn;
yarn start;
npm install typescript
# or
yarn add typescript
npm install @types/node @types/react @types/react-dom
# or
yarn add @types/node @types/react @types/react-dom