TExcelTAD
Home › TypeScript › TS Simple Types

TypeScript Simple Types

JavaScript has a handful of primitive values: strings, numbers, booleans, big integers, symbols, null and undefined. TypeScript gives each of them a type name you can use in annotations. These simple types are the building blocks for everything else in the course.

The three everyday primitives

Type Values Example
string text, in 'single', "double" or `template` quotes "Ha Noi"
number all numbers: integers, decimals, negative, NaN, Infinity 42, 3.14, -7
boolean true or false true
ts · try it yourself
const city: string = "Da Nang";
const population: number = 1_220_000;
const isCoastal: boolean = true;

console.log(city, population, isCoastal);
console.log(typeof city, typeof population, typeof isCoastal);
Da Nang 1220000 true
string number boolean

Note: Always write the type names in lowercase. String, Number and Boolean (capitalised) refer to JavaScript's wrapper objects, which you almost never want.

TypeScript has a single number type for both integers and floating-point values, just like JavaScript. There is no separate int or float.

Type errors with primitives

Once a variable has a type, you can only assign values of that type to it. Watch what the compiler says when we break that rule:

ts · try it yourself
let score: number = 10;
score = "eleven";

let ready: boolean = true;
ready = 1;
main.ts(2,1): error TS2322: Type 'string' is not assignable to type 'number'.
main.ts(5,1): error TS2322: Type 'number' is not assignable to type 'boolean'.

TS2322 is the error you'll see most often — "type X is not assignable to type Y". Notice that the compiler reports all problems at once, not just the first one.

The type also controls which operations and methods are allowed. A number has toFixed, a string has toUpperCase — but not the other way round:

ts · try it yourself
const price: number = 19.5;
const label: string = "total";

console.log(label.toUpperCase(), price.toFixed(2));
console.log(price.toUpperCase());
main.ts(5,19): error TS2339: Property 'toUpperCase' does not exist on type 'number'.

Remove the last line and the program prints TOTAL 19.50.

bigint and symbol

Two more primitives are less common but good to know:

  • bigint holds whole numbers of any size. Write them with an n suffix. You can't mix bigint and number in arithmetic without converting.
  • symbol creates unique identifiers, often used as special object keys.
ts · try it yourself
const big: bigint = 9_007_199_254_740_993n;
const doubled: bigint = big * 2n;
console.log(doubled);

const id1: symbol = Symbol("id");
const id2: symbol = Symbol("id");
console.log(id1 === id2, id1.description);
18014398509481986n
false id

Mixing them is an error, because JavaScript would throw a TypeError at runtime:

ts · try it yourself
const big: bigint = 10n;
const small: number = 5;
console.log(big + small);
main.ts(3,13): error TS2365: Operator '+' cannot be applied to types 'bigint' and 'number'.

The fix is an explicit conversion: big + BigInt(small) gives 15n.

null and undefined

null and undefined also have their own types, named null and undefined. In strict mode (which this course uses), they are not part of other types: a string variable cannot hold null unless you say so. You'll learn how to allow missing values with union types such as string | null in the Null & Undefined lesson.

ts · try it yourself
let nickname: string = "Bin";
nickname = null;
main.ts(2,1): error TS2322: Type 'null' is not assignable to type 'string'.

Tip: This single rule — strictNullChecks, part of strict mode — prevents the famous "Cannot read properties of undefined" crash in a huge number of cases.

Summary

Type Typical use
string names, messages, IDs
number counts, prices, measurements
boolean flags and conditions
bigint very large integers (IDs, cryptography)
symbol unique keys
null, undefined absence of a value

Test yourself

Which annotation is correct for a variable that holds 3.75?

  1. float
  2. double
  3. number
  4. Decimal

Which error code means "Type X is not assignable to type Y"?

  1. TS2339
  2. TS2322
  3. TS2365
  4. TS1005

Why should you write string and not String in annotations?

  1. String is a syntax error
  2. String refers to the wrapper object type, not the primitive
  3. They are identical; it's only style
  4. String only works in classes