🇬🇧 Limited Time — UK Only·🎓 Free Learning for 1 Month·🤖 Free AI Training Included·📚 4,000+ Lessons · 35,000+ Quizzes·🏆 GCSE Mocks · Olympiad Papers·⚡ Selected Students Only · Limited Places·🎁 Free Value Worth £2,000·🇬🇧 Limited Time — UK Only·🎓 Free Learning for 1 Month·🤖 Free AI Training Included·📚 4,000+ Lessons · 35,000+ Quizzes·🏆 GCSE Mocks · Olympiad Papers·⚡ Selected Students Only · Limited Places·🎁 Free Value Worth £2,000·🇬🇧 Limited Time — UK Only·🎓 Free Learning for 1 Month·🤖 Free AI Training Included·📚 4,000+ Lessons · 35,000+ Quizzes·🏆 GCSE Mocks · Olympiad Papers·⚡ Selected Students Only · Limited Places·🎁 Free Value Worth £2,000·
📘 TypeScript

Types for functions

You can type function parameters and return value: function add(a: number, b: number): number { return a + b; }. That way you can't accidentally pass a string. Here we write the same in plain JS.

3 min 10 XP Lesson 2 of 21
Types for functions
🌐

Appy Says…

TypeScript makes functions airtight — you declare what goes in and what comes out, and TypeScript catches any mismatch before you even run the code. No more 'undefined is not a function' surprises.

📖

How do TypeScript Functions work?

In TypeScript you annotate a function's parameter types and return type. TypeScript then verifies every caller passes the right arguments and uses the return value correctly.

  • function greet(name: string): string { return `Hello, ${name}`; }
  • Return type after ): : string, : number, : void
  • Arrow functions: const add = (a: number, b: number): number => a + b;
  • Optional params: function log(msg: string, level?: string)
  • Default params: function log(msg: string, level = 'info')
  • TypeScript infers the return type if you omit it — annotation is recommended for clarity
🎮

Think of it like a Roblox Remote with typed arguments

A Roblox RemoteEvent that fires can't guarantee the data format — you have to check it yourself. TypeScript function types are like a contract: if the caller doesn't send exactly the right types, the game (compiler) refuses to even start.

⚙️

How It Works

  • 1. Annotate parameters: (x: number, y: string)
  • 2. Annotate return: ): boolean after the closing parenthesis
  • 3. TypeScript checks every call site: wrong argument type = compile error
  • 4. Return type mismatch (returning string from a number function) = compile error
  • 5. void: function returns nothing (implicitly undefined)
  • 6. never: function never returns (always throws or infinite loop)
🌍

Real-World Examples

  • function calculateDiscount(price: number, percent: number): number
  • async function fetchUser(id: string): Promise<User>
  • function handleClick(event: React.MouseEvent<HTMLButtonElement>): void
  • const formatDate = (date: Date, locale = 'en-GB'): string => ...
💡

Key Facts

  • TypeScript infers return type from the return statement — annotation is optional but recommended for public APIs
  • Function types as variables: type Handler = (event: Event) => void;
  • Overloads: multiple function signatures for the same function name
  • never return type is used in functions that always throw or call process.exit()
⚠️

Watch Out!

Forgetting to annotate return types on async functions is a common mistake. Without it, TypeScript may infer Promise<any> instead of the specific type you intend. Always annotate async function returns: async function loadUser(id: string): Promise<User>.

📌

Remember

function name(param: Type): ReturnType. Use void when there's no return. TypeScript verifies every call site. Annotate return types explicitly for public functions.

What You Learned

  • TypeScript functions: annotate parameter types and return type; compiler checks every call
  • void = no return; never = never returns; optional params with ?
  • Unlocks: type-safe APIs, self-documenting functions, catching argument bugs at compile time

Key Facts

  • TypeScript infers return type from the return statement — annotation is optional but recommended for public APIs
  • Function types as variables: type Handler = (event: Event) => void;
  • Overloads: multiple function signatures for the same function name
  • never return type is used in functions that always throw or call process.exit()

Real-World Examples

• <code>function calculateDiscount(price: number, percent: number): number</code> • <code>async function fetchUser(id: string): Promise&lt;User&gt;</code> • <code>function handleClick(event: React.MouseEvent&lt;HTMLButtonElement&gt;): void</code> • <code>const formatDate = (date: Date, locale = 'en-GB'): string => ...</code>

Remember

function name(param: Type): ReturnType. Use void when there's no return. TypeScript verifies every call site. Annotate return types explicitly for public functions.

Quick Quiz

1 / 2

What can you type in a function?