Sub topic 1
Two worlds: types and values
TypeScript programs use two related but different worlds:
- The value space contains things that exist when JavaScript runs, such as variables, objects, functions, and numbers.
- The type space contains descriptions that TypeScript checks before the program runs, such as
string, interfaces, and type aliases.
Many TypeScript features become easier once you know which world an expression belongs to.
Prerequisites
You should know basic TypeScript syntax, including interfaces, type aliases, union types, JavaScript objects, arrays, functions, and basic generics.
Values exist at runtime
A variable declared with const creates a runtime value:
const userName = "Ada";
console.log(userName);When TypeScript compiles this code to JavaScript, userName still exists. The JavaScript program can read it and print it.
An interface does not work this way:
interface User {
id: number;
name: string;
}User describes the shape of an object for the type checker. It does not create a constructor, object, or runtime variable. The interface is erased from the emitted JavaScript.
The same distinction applies to type aliases:
type UserId = number;UserId is useful while checking the program, but there is no UserId value at runtime.
`typeof` has two meanings
JavaScript already has a runtime typeof operator. It evaluates a value and returns a string:
const port = 8080;
console.log(typeof port); // "number"This typeof runs in JavaScript. Its result is a value, specifically the string "number".
TypeScript also allows typeof in a type position. There, it does not run code. Instead, it reads the type of an existing value:
const port = 8080;
type Port = typeof port; // numberThe two uses look similar but perform different jobs:
| Expression | Position | Meaning |
|---|---|---|
| `typeof port` | Value position | Runs at runtime and returns `"number"` |
| `typeof port` | Type position | Produces the type `number` |
A type position appears where TypeScript expects a type, such as after type, :, or as. A value position appears in executable expressions.
Deriving types from values
Type-level typeof is useful when a value is the source of truth:
const config = {
host: "localhost",
port: 8080,
};
type Config = typeof config;Config now describes the structure inferred from config. It is equivalent to a type with host as a string and port as a number.
This avoids repeating the same structure:
function connect(options: Config): void {
console.log(`Connecting to ${options.host}:${options.port}`);
}
connect(config);The object remains a runtime value, while Config is only a compile-time name for its inferred type.
You can also apply type operators to the derived type. For example, keyof produces a union of property names:
type ConfigKey = keyof typeof config; // "host" | "port"Read this from right to left:
configis a runtime value.typeof configgets its type.keyofgets the keys of that type.
The operators do not inspect the object while the program runs. They only help TypeScript check code.
Avoiding the wrong space
This code is invalid:
interface User {
id: number;
name: string;
}
// const copy = User; // Error: User only exists in the type spaceIf you need a runtime object, create one separately:
const user = {
id: 1,
name: "Ada",
};
type UserShape = typeof user;Keeping these spaces separate explains many compiler errors. A name can be valid as a type but invalid as a value, or valid as a value but usable as a type only through typeof.
Exercise
What does typeof config mean in type Config = typeof config, and why is it different from typeof config in console.log(typeof config)?
Show answer
In the type position, it derives a compile-time type from the value named config. In the runtime expression, it evaluates config and returns a string such as "object".