Git Product home page Git Product logo

curso-typescript's Introduction

Curso de Typescript (Apontamentos)

Instalação do Typescript

sudo npm install -g typescript
tsc --init
tsc introducao/base.ts

Executar Typescript com Code Runner

Code Runner - https://marketplace.visualstudio.com/items?itemName=formulahendry.code-runner

Para utilizar o Code Runner com Typescript é necessário instalar o ts-node sudo npm install -g ts-node

Para executar um ficheiro .ts usar as teclas ctrl + alt + n

Compilação à medida que é feito "save"

 tsc -w

Types

any
void

boolean
number
string

null
undefined

string[]
[string, number]

string | null | undefined

never

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

Exemplos

let name: string = 'Claudio';
let age: number = 27;
let isBoolean: boolean = true;
let myAge: any
myAge = 27
myAge = ‘27’
let hobbies: string[] = ["Hobbie 1", "Hobbie 2"];
let address: [string, number] = ["Avenida X", 6300];
enum Color {
    Green,       //0
    Blue = 100,  //100
    Red = 10,    //10
    Orange,      // 11
    yellow,      // 12
    violet = 100 //100
}
let myColor: Color = Color.Blue;// 100
function returnString(): string {
    return ‘String’;
}
function voidExample(): void {
    console.log('Void');
}

function multiply(numA: number, numB: number): number {
    return numA * numB;
}

function fail(msg: string): never {
    throw new Error(msg);
}
let calculate: (numberA: number, numberB: number) => number = function(numA: number, numB: number): number {
    return numA * numB;
};
let user: {name: string, age: number} = {
    name: 'Claudio',
    age: 27,
}
type Employee = {
    supervisors: string[],
    hitPoint: (hours: number) => string
}
let employee: Employee = {
    supervisors: ['Ana', 'Fernando'],
    hitPoint(hours: number): string {
        if(hours <= 8) {
            return 'Normal Point';
        }
        return 'Out of Hours';
    }
};
let note: number | string = 10;
console.log(`My note is ${note}!`);
note = '10';
console.log(`My note is é ${note}!`);

Arrow functions

const sub = (n1: number, n2: number): number => n1 - n2

Parâmetros Padrão

function example(start: number = 5,
    end: number = start - 5): void {
        console.log(start, end);
}

Spread Operator

function returnArray(...args: number[]): number[] {
    return args
}

function example(...params: [number, string, boolean]) {
    console.log(` --> ${params[0]} ${params[1]} ${params[2]}`)
}

curso-typescript's People

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.