Prover funcionalidades do Javascript ainda planejadas hoje (ex: decorators, private instance methods, class fields) e não planejadas (Enum)
class Pessoa {
constructor(public cumprimento: string) {}
cumprimenta() {
return `<h1>${this.cumprimento}</h1>`;
}
};
var pessoa = new Pessoa("Ata");
pessoa.cumprimenta();
var Pessoa = /** @class */ (function () {
function Pessoa(cumprimento) {
this.cumprimento = cumprimento;
}
Pessoa.prototype.cumprimenta = function () {
return "<h1>" + this.cumprimento + "</h1>";
};
return Pessoa;
}());
var pessoa = new Pessoa("Ata");
pessoa.cumprimenta();
http://www.typescriptlang.org/play/
interface Square {
height: number;
width: number;
}
interface Rect {
height: number;
width: number;
}
let rect: Rect;
const square = {
width: 4,
height: 4
};
rect = square; // Ok
/* @flow */
class Pessoa {
cumprimento: string;
constructor(cumprimento: string) {
this.cumprimento = cumprimento;
}
cumprimenta() {
return `<h1>${this.cumprimento}</h1>`;
}
}
const pessoa = new Pessoa('Ata');
pessoa.cumprimenta();
class Pessoa {
constructor(cumprimento: string) {
this.cumprimento = cumprimento;
}
cumprimenta() {
return `<h1>${this.cumprimento}</h1>`;
}
}
const pessoa = new Pessoa('Ata');
pessoa.cumprimenta();
https://flow.org/try/
const body = document.body; // HTMLElement | undefined
body.querySelector("nav"); // Erro
body!.querySelector("nav"); // Sem erro
opaque type CPF = string;
opaque type RG = string;
function validateCpf(cpf: string): CPF | null {
if (isCpfValid(cpf)) return cpf;
else return null;
}
function validateRg(rg: string): RG | null { /*...*/ }
let myRg = validateRg(res.rg);
let myCpf = validateCpf(res.cpf);
myCpf = myRg; // Erro
myRg = myCpf; // Erro
type Exclude<T, U> = T extends U ? never : T;
type NumberAlias = Exclude<string | number, number>;
type ReturnType<T extends Function> = T extends (...args: any[]) => infer R ? R : never;
type StringAlias = ReturnType<() => string>;
type Entity = "Contact" | "Lawsuit";
type Map<K, V> = { [K]: V }
export const humanizeEntity: Map<Entity, *> = {
Contact: "Contato",
Lawsuit: "Processo",
};
interface Entity<Type extends string> {
type: Type;
name: string;
}
interface Organization extends Entity<"org"> {
location: string;
}
function isEntityOrganization(
entity: Entity<any>
): entity is Organization {
return entity.type === "org";
}
const entity: Entity<any> = { /*...*/ };
if (isEntityOrganization(entity) {
entity.location; // O tipo agora é Organization
}
// schema.gql
type Organization {
title: String
id: Int!
}
// types.ts
export interface Organization {
title?: string | null;
id: number;
}
export type Resolver<Result, Parent = any, Context = any, Args = any> = (
parent: Parent,
args: Args,
context: Context,
info: GraphQLResolveInfo
) => Promise<Result> | Result;
export namespace OrganizationResolvers {
/* ... */
}
Flow’s lighter-weight approach may suit dynamic-typing advocates better, whereas TypeScript’s DefinitelyTyped library and approach may perhaps be more appealing to strong-typing advocates coming from other languages.
Fonte: "Diminishing returns of static typing"