Inference, unions and literal types
This week you learn to make the compiler work for you. We start with type inference: TypeScript's ability to deduce the type of an expression without you writing it. When you declare a variable with an initial value, the type is taken from that value. When a function returns something, the return type is computed from the return statements. Thanks to this, TypeScript code looks a lot like JavaScript, but with the safety net underneath.
let city = 'Lima'; // inferred: string
const year = 2025; // inferred: 2025 (literal type)
const tags = ['a', 'b']; // inferred: string[]
function square(n: number) {
return n * n; // return type inferred: number
}
// city = 10; // error: Type 'number' is not assignable to type 'string'
console.log(city, year, tags, square(4));Notice the difference between let and const. A let variable can change, so its type widens to the general type (string). A const never changes, so TypeScript gives it the narrowest possible type: the value itself, 2025, as a literal type. Understanding this difference opens the door to the next idea.
Literal types
A literal type is a type that accepts exactly one value. The type 'north' only accepts the text north; the type 3 only accepts the number 3. On their own they look useless, but combined they are very powerful. With the | operator (vertical bar) you create a union: a type that can be one of several. A union of literals describes a closed set of options, and the compiler rejects any value outside it, even a typo.
type Direction = 'north' | 'south' | 'east' | 'west';
type DiceRoll = 1 | 2 | 3 | 4 | 5 | 6;
function move(direction: Direction, steps: number): string {
return 'Moving ' + steps + ' steps ' + direction;
}
console.log(move('north', 3));
// move('nort', 3); // error: Argument of type '"nort"' is not assignable to parameter of type 'Direction'
const roll: DiceRoll = 4;
console.log(roll);Unions of different types
A union also combines different types. An identifier can be a number or a string; a result can be a value or null. Here a fundamental rule appears: on a union you can only use what is common to all its members. If id is string | number, you can call id.toString(), which exists on both, but not id.toUpperCase(), because numbers do not have it. To use specific operations you must first check which type it really is. That process is called narrowing and is the topic of the next lesson.
type Id = string | number;
function describe(id: Id): string {
// return id.toUpperCase(); // error: Property 'toUpperCase' does not exist on type 'number'
return 'ID: ' + id.toString();
}
console.log(describe(42));
console.log(describe('A-7'));A classic mistake is declaring with let a variable that holds a literal and expecting it to keep the narrow type. If you write let direction = 'north' and then pass it to a function expecting Direction, it fails, because TypeScript widened the type to string. You have two solutions: declare it with const, or annotate the type explicitly with let direction: Direction = 'north'. The same happens with objects: their properties are widened unless you use as const. When an error message surprises you, hover over the variable and see which type was actually inferred.
✎ Meine Notizen
Nur in diesem Browser gespeichert. Melde dich an, um es im Konto zu behalten.0/5000 Zeichen