Boost Your TypeScript Skills with 6 Powerful Type‑Checking Tricks
This article presents six practical TypeScript techniques—including never‑type exhaustive checks, discriminated unions, union‑type hints, the satisfies keyword, template‑literal permutations, and type extraction—to enhance type safety and improve developer productivity.
TypeScript, a static type‑checking language, improves code safety and developer experience. This article presents practical techniques and examples to master TypeScript.
1. Using never in switch statements
In a switch statement, the never type ensures that all possible cases are handled, preventing missed branches.
type Fruit = 'apple' | 'banana' | 'orange';
function getFruitColor(fruit: Fruit): string {
switch (fruit) {
case 'apple':
return 'red';
case 'banana':
return 'yellow';
case 'orange':
return 'orange';
default:
const exhaustiveCheck: never = fruit;
return exhaustiveCheck;
}
}If a new fruit is added to Fruit but not handled in the switch, TypeScript reports an error at the exhaustiveCheck in the default branch.
2. Replacing union types with discriminated (exclusive) types
Using discriminated types makes the type system clearer and avoids accidental mismatches.
type Square = { kind: 'square'; size: number };
type Rectangle = { kind: 'rectangle'; width: number; height: number };
type Shape = Square | Rectangle;
function getShapeArea(shape: Shape): number {
if (shape.kind === 'square') {
return shape.size * shape.size;
} else if (shape.kind === 'rectangle') {
return shape.width * shape.height;
} else {
const exhaustiveCheck: never = shape;
return exhaustiveCheck;
}
}Each variant has a unique kind property, simplifying type checks.
3. Preserving union‑type suggestions
Keeping union‑type hints in code helps developers see all possible values during development.
type Status = 'success' | 'error' | 'loading';
function handleStatus(status: Status) {
switch (status) {
case 'success':
console.log('Operation was successful');
break;
case 'error':
console.log('There was an error');
break;
case 'loading':
console.log('Loading...');
break;
default:
const exhaustiveCheck: never = status;
return exhaustiveCheck;
}
}The switch statement retains autocomplete for every Status value.
4. The satisfies keyword
Introduced in TypeScript 4.9, satisfies checks that an object conforms to a type without fixing its exact shape.
type Point = { x: number; y: number };
const point = {
x: 5,
y: 10,
z: 20
} satisfies Point;The point object satisfies Point while still allowing extra properties.
5. Template‑literal type permutations
Template‑literal types enable compile‑time string concatenation, expanding the expressive power of the type system.
type Color = 'red' | 'green' | 'blue';
type Size = 'small' | 'medium' | 'large';
type ColorSize = `${Color}-${Size}`;
const colorSize: ColorSize = 'red-small'; // ✅ valid ColorSizerepresents every possible color‑size combination.
6. Extracting types
Utility types let you pull out a portion of a complex type for reuse.
type Person = {
name: string;
age: number;
address: {
street: string;
city: string;
};
};
type Address = Person['address'];
const address: Address = {
street: '123 Main St',
city: 'New York'
}; Addressextracts the nested address type from Person.
Conclusion
The article covered several useful TypeScript techniques: never checks in switch, discriminated types, preserving union hints, the satisfies keyword, template‑literal permutations, and type extraction. These patterns improve type safety and developer productivity.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Code Mala Tang
Read source code together, write articles together, and enjoy spicy hot pot together.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
