TypeScript Evolui Rapido
Se voce aprendeu TypeScript ha 2 anos e parou de acompanhar, esta perdendo features poderosas. Aqui estao as que mais uso em 2025.
1. satisfies Operator
Valida o tipo sem perder a inferencia:
const config = {
port: 3000,
host: 'localhost',
debug: true,
} satisfies Record<string, string | number | boolean>;
// config.port ainda e inferido como number (nao string | number | boolean)
2. Template Literal Types
type EventName = 'click' | 'focus' | 'blur';
type Handler = on${Capitalize<EventName>};
// "onClick" | "onFocus" | "onBlur"
3. const Type Parameters
function createConfig<const T extends readonly string[]>(routes: T) {
return routes;
}
const routes = createConfig(['/', '/about', '/blog']);
// tipo: readonly ['/', '/about', '/blog'] (nao string[])
4. Discriminated Unions
Pattern essencial para tratar diferentes estados:
type Result<T> =
| { success: true; data: T }
| { success: false; error: string };
function handle(result: Result<User>) {
if (result.success) {
console.log(result.data.name); // TypeScript sabe que data existe
} else {
console.log(result.error); // TypeScript sabe que error existe
}
}
5. Utility Types Avancados
// Tornar todos os campos opcionais, exceto id
type UpdateUser = Partial<Omit<User, 'id'>> & Pick<User, 'id'>;
// Extrair tipo de retorno de funcao async
type UserData = Awaited<ReturnType<typeof getUser>>;
6. Branded Types
Para evitar misturar IDs de tipos diferentes:
type UserId = string & { __brand: 'UserId' };
type PostId = string & { __brand: 'PostId' };
function getUser(id: UserId) { ... }
function getPost(id: PostId) { ... }
const userId = 'abc' as UserId;
getUser(userId); // OK
getPost(userId); // ERRO - UserId nao e PostId
7. Inferencia com infer
type ExtractPromise<T> = T extends Promise<infer U> ? U : T;
type Result = ExtractPromise<Promise<string>>; // string
Conclusao
TypeScript em 2025 e muito mais que tipagem basica. Essas features permitem expressar regras de negocio no sistema de tipos, pegando bugs em compile time que so apareceriam em producao.