DEV Community

Cover image for TypeScript: Any vs Unknown
Luís Von Muller
Luís Von Muller

Posted on

9 4

TypeScript: Any vs Unknown

Se você acabou de chegar no mundão do TypeScript, aqui está um conteúdo essencial, onde diferencia a ideia de 2 tipos primitivos. E também o porquê de que se você sair usando arbitrariamente any você não está realmente usando o TypeScript e deveria fazer apenas fazer em JavaScript...

Vamos pegar um exemplo bem simples e a partir dele debater a ideia.

let fulano: any = "João";
let ciclado: unknown = "Augusto"
Enter fullscreen mode Exit fullscreen mode

Ao tipar a variável "Fulano" com any, nós literalmentes dizemos para o Transpilador/Compilador do Typescript que ela é literalmente: Qualquer coisa.

Por exemplo, para ele, o seguinte código seria totalmente válido.

fulano.funcao(); 
Enter fullscreen mode Exit fullscreen mode

Todavia, não existe essa função em fulano. Logo, em tempo de execução, isto é: em runtime, nos retornaria um erro (ou seja, ia dar merda).
(ノಠ益ಠ)ノ彡┻━┻

Já com o "Unknown" o negócio é diferente, a gente usa o propósito mor do TypeScript: Type Safety (Segurança de Tipos ante execução).

ciclado.funcao();
Enter fullscreen mode Exit fullscreen mode

Typescript error because of a calling on a unkown

Ou seja, o Transpilador/Compilador do Typescript não vai deixar esse código realmente deixar ser executado, porque, o sistemas de tipo não é capaz de garantir que realmente haja essa função dentro de ciclano.

Simples né? ¯_(ツ)_/¯

Vem pro meu twitter & muito mais: https://allmylinks.com/luisvonmuller

Aqui tem mais conteúdo sobre TypeScript em PT-BR: https://luisvonmuller.gitbook.io/typescript-para-brasileiros/

Beijão (っ˘з(˘⌣˘ )

Sentry blog image

How to reduce TTFB

In the past few years in the web dev world, we’ve seen a significant push towards rendering our websites on the server. Doing so is better for SEO and performs better on low-powered devices, but one thing we had to sacrifice is TTFB.

In this article, we’ll see how we can identify what makes our TTFB high so we can fix it.

Read more

Top comments (6)

Collapse
 
hgoulart profile image
Hingo Rocha

Legal.

Collapse
 
abhj profile image
Abhijay Mitra

Dev can't support translation? You can always translate pages in chrome by right clicking on the screen.

Collapse
 
pekosong profile image
Peko

nice

Some comments may only be visible to logged-in visitors. Sign in to view all comments.

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

👋 Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay