JS Guide
HomeQuestionsTopicsCompaniesResources
BookmarksSearch

Built for developers preparing for JavaScript, React & TypeScript interviews.

ResourcesQuestionsSupport
HomeQuestionsSearchProgress
HomeQuestionstypescript
PrevNext

Learn the concept

Basic Types

typescript
junior
types

What are the basic types in TypeScript?

types
primitives
arrays
tuples
any
unknown
void
never
Quick Answer

TypeScript includes primitive types (string, number, boolean, null, undefined, symbol, bigint), array types, tuple types, object types, and special types like any, unknown, void, and never.

Detailed Explanation

Primitive Types:

  • string: Text values
  • number: All numbers (integer and float)
  • boolean: true/false
  • null / undefined: Absence of value
  • symbol: Unique identifiers
  • bigint: Large integers

Array Types:

  • number[] or Array<number>
  • Can hold multiple values of same type

Object Types:

  • Inline objects with property types
  • Interfaces or type aliases for reuse

Special Types:

  • any: Opt-out of type checking
  • unknown: Type-safe any (must narrow before use)
  • void: No return value
  • never: Never returns (throws/infinite loop)

Code Examples

Basic and primitive typesTypeScript
// Primitive types
let name: string = 'Alice';
let age: number = 25;
let isActive: boolean = true;
let nothing: null = null;
let notDefined: undefined = undefined;

// Number includes all numeric types
let decimal: number = 10;
let float: number = 3.14;
let hex: number = 0xff;
let binary: number = 0b1010;

// BigInt for large integers
let bigNumber: bigint = 9007199254740991n;

// Symbol
const uniqueKey: symbol = Symbol('description');

Real-World Applications

Use Cases

Form Input Validation

Typing form field values with proper primitives and using union types for validation states

Database Schema Modeling

Mapping database column types to TypeScript primitives for type-safe ORM queries

API Response Typing

Defining precise types for API payloads including nullable fields and optional properties

Mini Projects

Type-Safe Calculator

beginner

Build a calculator that uses TypeScript types to prevent invalid operations like dividing by a string

Exhaustive Status Handler

intermediate

Implement a state machine for order status using union types and never-based exhaustive checking

Industry Examples

Prisma

Maps database column types to TypeScript primitives for fully type-safe queries

Zod

Runtime validation library that infers TypeScript types from schemas

Resources

TypeScript - Everyday Types

docs

TypeScript - Narrowing

docs

Related Questions

What is the difference between interfaces and type aliases in TypeScript?

junior
interfaces

What are union and intersection types in TypeScript?

junior
unions
Previous
What is TypeScript and what benefits does it provide over JavaScript?
Next
What is the difference between interfaces and type aliases in TypeScript?
PrevNext