TypeScript: Primitives, Literals and the Special Types
A quick dive into TypeScript's primitives, literals and special types.
Listen to Article
TypeScript: Primitives, Literals and the Special Types
Primitives
JavaScript has seven runtime primitive types, and TypeScript has corresponding types for each of them:
string: Textual data, such as"hello",'hello'or'hello ${name}'.number: Floating-point numbers (all numbers in JavaScript are 64-bit IEEE-754 floating-point values), including integers, deminals,NaN, andInfinity.bigint: Whole numbers of arbitrary size, created withBigInt(...)or integer literals ending withn(such as9007199254740991n). Abigintand anumberare distinct types and cannot be added or compared directly with===without conversion.boolean: Logical valuestrueandfalse.symbol: Unique values created by callingSymbol()orSymbol.for(...), often used as unique object property keys.undefined: Represents a variable that has been declared but not assigned a value, or a missing property/return value.null: Represents the intentional absence of any object value.
null and undefined in Strict Mode
When strictNullChecks is enabled (which is part of strict: true), null and undefined are not automatically included in other types like string or number. If a variable can hold a string or null, you write string | null. Without strictNullChecks null and undefined can be assigned to any type, which hides potential runtime errors.
Lowercase Primitives vs. Uppercase Constructors
In TypeScript, string, number, boolean, symbol, and bigint refer to primitive types. String, Number, Boolean, Symbol, and BigInt refer to the built-in JavaScript constructor/wrapper types, which include boxed object instances. Primitive types are the standard types to use for values.
Literals
A literal type is a type that represents one specific value rather than an entire set of values:
- String literals:
"GET","admin","light". - Number literals:
0,1,200,-1. - Boolean literals:
true,false. - BigInt literals:
0n,10n.
On its own, a literal type like "success" means that a value can only be that exact string. Literals become especially useful in union types, where you restrict a value to a specific set of choices.
Example:
Type widening and as const
When you write let mode = "dark", TypeScript widens the inferred type from "dark" to string because mode can be reassigned to another string later.
When you write const mode = "dark", mode cannot be reassigned, so TypeScript infers the literal type "dark".
For objects and arrays, const prevents reassigning the variable itself, but its properties or elements can still be mutated and widened. Adding as const tells TypeScript to treat the expression as deeply readOnly and keep literal types instead of widening them:
Template literal types
Template literal types build new string literal types from existing string literal types, unions, or primitives using backtick syntax.
Example:
Top types: any and unknown
In type theory, a top type is the supertype of all types - meaning every type can be assigned to it. TypeScript has two top types: any and unknown.
any
any disables type checking for the value that it is attached to. Any type can be assigned to any, and any can be assigned to almost any type (except never). When you access properties, call methods, or pass any any value into functions, TypeScript does not check whether those operations are valid. If the actual runtime value does not support what you do with it, a runtime error occurs.
Example:
unknown
unknown is the type-safe counterpart to any. Like any, any value can be assigned to unknown. Unlike any, you cannot directly do anything with an unknown value - you cannot access properties on it, call it as a function, or assign it to a more specific type without first narrowing it or validating it.
Example:
Choosing unknown or never instead of any
- Use
unknownwhen you have a value whose type is not known yet at compile time - such as parsed JSON, a caught error incatch (error), or data from an external API - and you want the compiler to force you to check or validate it before using it. - Use
never(notany) when a type or code path represents something impossible or a function that never returns - not to hold an arbitrary runtime value.
Bottom type: never
In type theory, a bottom type is a subtype of every type and represent the empty set - a type that can never have any runtime value. TypeScript's bottom type is never.
neveris assignable to every type.- No type (not even
string,number,anyorunknown) is assignable tonever.
never shows up in several key situations:
- Functions that never return: A function that always throws an exception or enters an infinite loop never completes normally, so its return type is
never.
Example:
- Exhaustiveness checking: When narrowing a union type (such as a discriminated union) in a
switchorif/elsechain, handling every member leavesneverin the remaining branch. If you pass that remaining value to a helper expectingnever, TypeScript will report a compile error if a new member is later added to the union and not handled.
Example:
- Impossible states and type filtering: TypeScript can narrow a variable to
neverif conditions contradict each other (for example, checking if astringis also anumber). In conditional types, returningneverremoves a branch when distributing over a union (which is how utility types likeExclude<T, U>filter out types).
void
void is primarily used as the return type of a function to indicate that the function does not return a meaningful value (or that callers should not rely on a return value).
- A function with a
voidreturn type can omit areturnstatement or explicitlyreturn undefined;. - Under
strictNullChecks,voidis not the same asundefined- you generally cannot assign avoidvalue to a variable typed asundefined(though a function returningundefinedis assignable to a function type returningvoid). - A callback type with a
voidreturn type, such as() => void, can accept a function that returns another type (for example,Array.prototype.pushreturns anumber, yetitems.forEach(item => items.push(item))is valid because the callback's return value is simply ignored).
Stay Connected
Get notified whenever we publish new articles, insights, guides and product updates.