The fun parts!
Loiane Groner
@loiane
github.com/loiane
loiane.com
loiane.training
Java, JavaScript/HTML5,
Sencha, Angular, Phonegap/
Ionic, etc
Disponível (inglês) na amazon.com.br
https://novatec.com.br/livros/estruturas-de-dados-algoritmos-em-javascript/
Ambiente DEV
TypeScript 2.5
EcmaScript 2017
ES5
vanilla JS
2017
JavaScript que escala
Tipagem estática em tempo
de compilação
Compila para JavaScript
(vanilla, ES201X)
TypeScript
Fortemente tipada?
https://twitter.com/ahejlsberg/status/792762247239995392?s=09
Iniciando
npm install -g typescript
mv main.js main.ts
tsc main.ts
Pode até achar problemas no seu código!
TypeScript
Type
System
Don't
Repeat
Yourself!
// Sim
let varString = 'BrazilJS';
// NÃO!!!
let string2: string = 'BrazilJS';
Evite código verboso
OPCIONAL: add tipo de retorno
function ordernarNome(a: Pessoa[]): Pessoa[] {
var result = a.slice(0);
result.sort(function(x, y) {
return x.nome.localeCompare(y.nome);
});
return result;
}
Às vezes pode ajudar a encontrar algum erro no código
interface
interface Pessoa {
nome: string;
idade: number;
}
Interface é uma COISA, definição
interface Pessoa {
nome: string;
idade: number;
}
interface PessoaTel extends Pessoa {
telefone: string;
}
Também pode ser usada com OO
Duck
Typing
Definindo
type e
union e
intersection
type Figura =
{ tipo: 'circulo'; raio: number }
| { tipo: 'quadrado'; l: number };
function calcularArea(figura: Figura): number {
switch (figura.tipo) {
case 'circulo':
return Math.PI * figura.raio ** 2;
case 'retangulo':
return figura.a * figura.l;
case 'quadrado':
return figura.l ** 2;
}
throw new Error('Figura inválida');
}
const circulo: Figura = { tipo: 'circulo', raio: 2 };
calcularArea(circulo);
update(body: T & { id }): Observable<T> {
return this.http.put(
`${this.API_SERVICE_URL}/${body.id}`,
this.getBody(body)
) as Observable<T>;
}
enum
enum UsuariosActionTypes {
LOAD_REQUEST = '[Usuarios] Load Request',
LOAD_SUCCESS = '[Usuarios] Load Success'
}
enum UsuariosActionTypes {
LOAD_REQUEST = '[Usuarios] Load Request',
LOAD_SUCCESS = '[Usuarios] Load Success'
}
type UsuarioActions =
| { type: UsuariosActionTypes.LOAD_REQUEST }
| {
type: UsuariosActionTypes.LOAD_SUCCESS;
payload: Usuario[]
};
OO e
Generics
export class CRUDService<T> {
load(): Observable<T[]> {
// …
}
create(body: T): Observable<T> {
// …
}
}
export class TaskService
extends CRUDService<Task> {
// …
}
Frameworks
export interface Props {
name: string;
enthusiasmLevel?: number;
onIncrement?: () => void;
onDecrement?: () => void;
}
function Hello({ name, enthusiasmLevel = 1, onIncrement, onDecrement }: Props) {
if (enthusiasmLevel <= 0) {
throw new Error('You could be a little more enthusiastic. :D');
}
return (
<div className="hello">
<div className="greeting">
Hello {name + getExclamationMarks(enthusiasmLevel)}
</div>
<div>
<button onClick={onDecrement}>-</button>
<button onClick={onIncrement}>+</button>
</div>
</div>
);
}
import Vue from 'vue';
import { Component, Prop } from 'vue-property-decorator';
import { Todo } from '../models';
@Component
export default class TodoItem extends Vue {
@Prop() public todo: Todo;
}
@ts-check
TS 2.5
Agosto 2017
@ts-check e refactoring
github.com/loiane/typescript-fun-parts
Obrigada!
@loiane
github.com/loiane
loiane.com
loiane.training
youtube.com/loianegroner

Typescript: the Fun Parts (BrazilJS 2017)