Skip to the solution
EasyFrontend9 min read

What are the basic types in TypeScript?

674 views

The problem

Question presented to candidate: "Walk me through the basic types in TypeScript. Which ones map to JavaScript values, and what are the special ones like any, unknown, void and never for?"

What a strong answer should cover:

  • The seven primitives, lowercase: string, number, bigint, boolean, symbol, null, undefined (and why String is the wrong type)
  • Arrays (number[] / Array<number>), tuples ([string, number]) and object types with optional properties
  • The special types: any disables checking, unknown must be narrowed, void means no useful return, never means no possible value
  • unknown as the top type and never as the bottom type, with any outside the ordering
  • Classic traps: {} accepts 5, typeof null is "object", bigint does not mix with number, and types are erased at run time

Code / implementation expected: Optional β€” a few annotated declarations and one rejected assignment.

  • types
  • basics

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

9 min read Β· 9 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

TypeScriptPrimitives, arrays, tuples and object types
TypeScriptThe special types: any, unknown, void, never and literals

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 6 of 81 in TypeScript

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextEasy

What is TypeScript and why use it over JavaScript?

Continue