Type Inference

Type Inference
πŸ‘¨β€πŸ’Ό TypeScript is smart about inferring types. You don't always need to write them explicitlyβ€”especially for return types.
function add(a: number, b: number) {
	return a + b // TypeScript knows this returns number
}
But TypeScript cannot infer parameter types (in most cases). You must specify them:
// ❌ Error: Parameter 'a' implicitly has 'any' type
function add(a, b) {
	return a + b
}

// βœ… Correct
function add(a: number, b: number) {
	return a + b
}
🐨 Open
index.ts
and:
  1. Observe what TypeScript infers for each function
  2. Hover over function names to see the inferred return types
  3. Add an explicit return type where it catches a bug
This exercise uses: - throw new Error(message) - stops execution and raises an error - % (modulo operator) - returns the remainder after division (e.g., 7 % 2 returns 1)
πŸ’° In VS Code/Cursor, hover over a function name to see its full type signature.

Please set the playground first

Loading "Type Inference"
Loading "Type Inference"
Login to get access to the exclusive discord channel.
Loading Discord Posts