Type Annotations and Basic Types

The main feature of TypeScript is its type system. You tell TypeScript what types your values should be, and it checks that you use them correctly. You do that with type annotations. An annotation is a colon followed by a type. You write it after a variable name, after a parameter, or in the return position.

const fullName: string = "Ali";
const age: number = 30;
const active: boolean = true;

If you try to assign a value that does not match the declared type, TypeScript reports an error:

const age: number = "thirty";
// Error: Type 'string' is not assignable to type 'number'

Type Inference

When you initialize a variable with a value, TypeScript infers the type from that value:

const fullName = "Ali"; // TypeScript infers: string
const age = 30; // TypeScript infers: number
const active = true; // TypeScript infers: boolean

An inferred type works exactly like an explicit annotation. TypeScript will still catch type errors:

let count = 10;
count = "ten"; // Error: Type 'string' is not assignable to type 'number'

Basic Types

TypeScript includes all of JavaScript’s primitive types plus a few of its own:

Type Description Example
string Text values "hello", 'world'
number All numbers (integer and float) 42, 3.14
boolean True or false true, false
null Intentional absence of value null
undefined Variable declared but not assigned undefined
any Opts out of type checking anything
unknown Type-safe alternative to any anything (must narrow first)
void No return value used on functions
never Value that never occurs functions that always throw

The any Type

The any type disables type checking for a value:

let data: any = 42;
data = "hello"; // no error
data = true; // no error
data.nonExistentMethod(); // no error — but will crash at runtime!

The unknown Type

The unknown type is the type-safe alternative to any. You can assign anything to it, but you cannot use it without first checking what it is:

let data: unknown = "hello";

// data.toUpperCase(); // Error: 'data' is of type 'unknown'

if (typeof data === "string") {
  console.log(data.toUpperCase()); // "HELLO" — safe!
}

Arrays

You can type arrays in two ways:

const numbers: number[] = [1, 2, 3];
const names: Array<string> = ["Ali", "Bob", "Cat"];

Both are equivalent. The type[] syntax is more common.

TypeScript enforces that all elements match the declared type:

const scores: number[] = [90, 85, "A+"];
// Error: Type 'string' is not assignable to type 'number'

Object Types

You can describe the shape of an object inline:

const user: { name: string; age: number } = {
  name: "Ali",
  age: 30,
};

Properties are separated by semicolons (or commas — both work). Optional properties use ?:

const user: { name: string; age?: number } = {
  name: "Ali", // age is optional
};

Inline object types get long fast. In the next section we will use interfaces to name shapes we can reuse, and in Composing Types we will use type aliases and other composition tools.