Skip to content

Instantly share code, notes, and snippets.

@jorgeas80
Created November 28, 2016 08:47
Show Gist options
  • Save jorgeas80/a9c7d4db9cac9a21a0aa014e931fb458 to your computer and use it in GitHub Desktop.
Save jorgeas80/a9c7d4db9cac9a21a0aa014e931fb458 to your computer and use it in GitHub Desktop.
TypeScript quick workshop

Basic Types

Classic ones

Boolean

let isDone: boolean = false;

Number

let hex: number = 0xf00d;
let binary: number = 0b1010;
let octal: number = 0o744;```

## String

```let color: string = "blue";
color = 'red';```

```let fullName: string = \`Bob Bobbington\`;
let age: number = 37;
let sentence: string = \`Hello, my name is ${ fullName }.

I'll be ${ age + 1 } years old next month.\` ```


## Array

let list: number[] = [1, 2, 3];
let list: Array<number> = [1, 2, 3];


## Tuple

// Declare a tuple type
let x: [string, number];
// Initialize it
x = ["hello", 10]; // OK
// Initialize it incorrectly
x = [10, "hello"]; // Error

console.log(x[0].substr(1)); // OK
console.log(x[1].substr(1)); // Error, 'number' does not have 'substr'

x[3] = "world"; // OK, 'string' can be assigned to 'string | number'

console.log(x[5].toString()); // OK, 'string' and 'number' both have 'toString'

x[6] = true; // Error, 'boolean' isn't 'string | number'

## Enum

enum Color {Red, Green, Blue};
let c: Color = Color.Green;

enum Color {Red = 1, Green, Blue};
let c: Color = Color.Green;

enum Color {Red = 1, Green = 2, Blue = 4};
let c: Color = Color.Green;

enum Color {Red = 1, Green = 2, Blue = 4};
let c: Color = Color.Green;


## Any

Las variables de tipo *any* no pasan por el control de tipos del compilador

let notSure: any = 4;
notSure = "maybe a string instead";
notSure = false; // okay, definitely a boolean

Ojo, no es lo mismo que Object

let notSure: any = 4;
notSure.ifItExists(); // okay, ifItExists might exist at runtime
notSure.toFixed(); // okay, toFixed exists (but the compiler doesn't check)

let prettySure: Object = 4;
prettySure.toFixed(); // Error: Property 'toFixed' doesn't exist on type 'Object'.

También vale para arrays

let list: any[] = [1, true, "free"];

list[1] = 100;

## Void

function warnUser(): void {
    alert("This is my warning message");
}

Para variables no nos vale. Solo les podemos asignar *undefined* o *null*

let unusable: void = undefined;


## Null and undefined

Existen como tipos en TypeScript. No son demasiado útiles

// Not much else we can assign to these variables!
let u: undefined = undefined;
let n: null = null;

Son subtipos de cualquier cosa, así que podemos asignar a una variable de tipo *number* un *null*, por ejemplo. Pero el compilador de ts tiene un flag -<kbd>-strictNullChecks</kbd> que impide asignarlos a cualquier cosa que no sea de tipo *void*, para evitar problemas.

## Never

Para cosas que nunca suceden

// Function returning never must have unreachable end point
function error(message: string): never {
    throw new Error(message);
}

// Inferred return type is never
function fail() {
    return error("Something failed");
}

// Function returning never must have unreachable end point
function infiniteLoop(): never {
    while (true) {
    }
}


# Variables

# Interfaces

# Classes

# Functions

# Generics

# Enums

# Type Inference

# Type Compatibility

# Advanced Types

# Symbols

# Iterators and generators

# Modules

# Namespaces

# Namespaces + Modules

# Module resolution

# Declaration merging

# JSX

# Decorators

# Mixins

# Triple-slash directives

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