惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
The Blog of Author Tim Ferriss
IT之家
IT之家
Engineering at Meta
Engineering at Meta
WordPress大学
WordPress大学
博客园 - 三生石上(FineUI控件)
博客园 - 聂微东
C
Check Point Blog
T
Tailwind CSS Blog
博客园 - Franky
H
Help Net Security
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Google DeepMind News
Google DeepMind News
博客园 - 叶小钗
J
Java Code Geeks
腾讯CDC
罗磊的独立博客
爱范儿
爱范儿
阮一峰的网络日志
阮一峰的网络日志
Martin Fowler
Martin Fowler
酷 壳 – CoolShell
酷 壳 – CoolShell
I
InfoQ
B
Blog
V
Visual Studio Blog
F
Fortinet All Blogs

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Introduction to TypeScript. Function Types
Julia Shlykova · 2026-06-19 · via DEV Community

Julia Shlykova

Basic syntax

Since everything in TypeScript has to have type, functions are also wrapped in the type system: inputs must have types, output must have one, let's look at the example:

function add(x: number, y: number): number {
  return x + y;
}

console.log(add(1, 3)); //4

Actually, we don't have to explicitly assign type for an ouput:

function add(x: number, y: number) {
  return x + y;
}

console.log(add(1, 3)); //4

The compiler won't see any problem with it, since it implicitly set number type for an output. The following code is also valid:

function add(x: number, y: number) {
  if (x === 0) {
    return "zero!"
  }
  return x + y;
}

console.log(add(1, 3));

Here, the compiler defines output type of the function as either number or "zero!".

Arrow function


Special return type

  • void - function return type that is used to indicate that function returns nothing.
function action(): void {
  // your code here
}

  • never - function return type indicating that the function throws an exception or terminates execution of the program.

Optional parameter ?:

function makeProfile(name: string, age: number, url?: string) {

}

let profile = makeProfile("Mary", 81);

A parameter url is optional, but if it exists, it must have string type.


Default parameter

Default parameter is an optional parameter that doesn't need special character:

function makeProfile(
  name: string, 
  age: number, 
  url: string = "www.example.com"
) {
  return `${name} is ${age} years old and visits ${url}.`
}

let profile = makeProfile("Mary", 81);

console.log(profile); //Mary is 81 years old and visits www.example.com.


Callback

As we know in JavaScript functions are treated as all values, they can be an argument in other functions. So, how would we define callback type?

function makeProfile(
  name: string, 
  age: number, 
  url: string = "www.example.com"
) {
  return `${name} is ${age} years old and visits ${url}.`;
}

function makePage(
  func: (n: string, a: number, u?: string) => string,
  param1: string,
  param2: number
) {
  func(param1, param2);
}

let profile = makePage(makeProfile, "Mary", 81);

parameters names in callback don't matter, but the same types do.

Multiple callbacks

What if we have several callbacks with the same parameters types? How is better to define them?

function mathOperation1(x: number, y: number): number {
  return x + y;
}

function mathOperation2(x: number, y: number): number {
  return x - y;
}

function applyMath(
  funcs: ((a: number, b: number) => number)[], 
  x: number, 
  y: number
){

}

applyMath(
  [mathOperation1, mathOperation2], 
  1, 
2
);

((a: number, b: number) => number)[] type tells the compiler that it has to be an array of functions with two parameters of number types.


Rest parameter

function sum(...numbers: number[]) {
// code
}

sum(1,2,3);
sum();
sum(1,2,3,4,5,6);

arguments of this function will be stored inside numbers array.