WeCodeAcademy

any, unknown and why to avoid any

any switches type checking off for a value. It is an escape hatch, and it hides bugs.

Exampletypescript
Open in editor »

Run it: TypeScript says nothing, then the program crashes. Use unknown instead. It accepts any value but forces you to check before using it.

Exampletypescript
Open in editor »
Quick check

What does `unknown` force you to do before using a value?

Your turn: write the code

Write describe(v: unknown): string that returns "text" for a string, "number" for a number and "other" for anything else. Print describe("a"), describe(1) and describe(true).

Finish the quick check (and any code exercise) to complete this lesson, or mark it done yourself.

Tip: use the left and right arrow keys to move between lessons.