HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to TypeScriptBasic TypesUnion & Literal TypesInterfacesTyped FunctionsArrays & TuplesGenerics & Enums
REFERENCE
📖 Overview
🖥️ Basics & Output
🏷️ Types
📐 Interface & Type Alias
🧩 Function
📋 Array & Object Types
🚀 Next
TypeScript›Typed Functions

Typed Functions

Writing safe functions by specifying parameter and return types; void.

Simple enough for a 5-year-old to understand

Parameter and return type

Example code
1
function square(x: number): number {
x takes a number, returns a number.
Functionx takes a number, returns a number.
2
return x * x;
Result.
FunctionResult.
3
}
End.
FunctionEnd.
4
console.log(square(5));
25
Output / Input25
FunctionOutput / Input
💡 Hint: square("5") → ERROR: argument must be of type number. Type safety prevents errors.

🎯 Exercise & Practice

Test what you've learned — answer, write code, check instantly. (1 left)

1.Write a function named addUp that takes two numbers and returns a number; print addUp(8, 9).

🎯 Expected output: 17

Loading editor…

About this topic

Functions in TypeScript are JavaScript functions with parameter and return type annotations added. Parameter types are mandatory — if you don't declare them, they become implicit any and you will get an error in strict mode. The return type, however, is usually inferred.

The true value of function types comes into play when passing the function ITSELF as a value. When you declare what parameters a callback takes and what it returns, passing a function with the wrong signature becomes a compilation error.

Explicitly writing the return type is usually a good choice: if you rely on inference, a change you make inside the function silently alters the contract and the error surfaces on the caller's side. When you write it explicitly, the error surfaces right where you made the change.

What you will learn

  • Declaring parameter and return types
  • Optional and default parameters
  • Defining function types
  • Callback signatures
  • The special behavior of the void return type
  • Overload signatures

When to use?

You need to declare the signature in every function that takes a callback; this is what you encounter most frequently in array methods and event handlers.

Overloading is for cases where the same function returns different types depending on the number of arguments. It's rarely needed — in most cases, a union type or generics provide a cleaner solution.

Common mistakes

Wrong

function greet(name?: string, greeting: string) {
  return greeting + " " + name;
}

An optional parameter cannot come BEFORE a required parameter; the compiler gives "A required parameter cannot follow an optional parameter". Optionals are always placed at the end.

Correct

function greet(greeting: string, name?: string) {
  return greeting + " " + (name ?? "");
}

Wrong

function sum(a: number, b: number) {
  return a + b;
}
const nums = ["1", "2"];
console.log(sum(...nums.map(Number)));

TypeScript cannot know the length of an array passed with the spread operator; it gives the error "A spread argument must either have a tuple type or be passed to a rest parameter". You need to use a tuple type or pass the arguments explicitly.

Correct

function sum(a: number, b: number) {
  return a + b;
}
const nums: [number, number] = [1, 2];
console.log(sum(...nums));

Wrong

const handlers: Array<() => void> = [];
handlers.push(() => "text");
const value: string = handlers[0]();

The void return type ACCEPTS a function that returns a value — this is a deliberate flexibility, because it's assumed the return value will be ignored. But if you try to use the return value, the assignment errors out because the type is void.

Correct

const handlers: Array<() => string> = [];
handlers.push(() => "text");
const value: string = handlers[0]();

Frequently asked questions

Should I write the return type?

Yes, on public API functions. If you rely on inference, a change in the function body silently alters the contract and the error surfaces at the caller. When you write it explicitly, the error surfaces on the exact line where you made the change — making it much easier to find.

Why does the void return type accept a function that returns a value?

Because it is assumed that the return value will be ignored, which makes it possible to pass array methods as callbacks. It looks strange but is very useful in practice; the problem only arises when you try to use the return value.

When is function overloading necessary?

Rarely. It's needed when the return type of a function DEPENDS on the type of its arguments. In most cases, union types or generics are cleaner and easier to maintain; with overloading, you have to ensure consistency between the implementation signature and the external signatures yourself.

Summary

In TypeScript, parameter types are mandatory, return types are usually inferred but it's preferred to write them explicitly in public APIs. Optional parameters go at the end, and the void return type intentionally accepts a function that returns a value.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code