TypeScript Function Overloading
Function overloading allows defining multiple signatures for a single function; the compiler selects the correct implementation based on the types of the arguments passed in.
Basic Syntax
First declare multiple function signatures, then implement a single unified function.
Example
// Function overload signatures
function
add
(
a
:
number
,
b
:
number
)
:
number
;
function
add
(
a
:
string
,
b
:
string
)
:
string
;
function
add
(
a
:
any
,
b
:
any
)
:
any
{
return
a
b
;
}
console.
log
(
"Number addition: "
add
(
1
,
2
)
)
;
console.
log
(
"String addition: "
add
(
"Hello, "
,
"World"
)
)
;
Running result:
数字相加: 3
字符串相加: Hello, World
Multiple-parameter overloads
You can define different combinations of multiple parameters.
Example
// Multiple overload signatures
function
greet
(
name
:
string
)
:
string
;
function
greet
(
name
:
string
,
greeting
:
string
)
:
string
;
// Implementation
function
greet
(
name
:
any
,
greeting
?:
any
)
:
any
{
if
(
greeting
)
{
return
greeting
", "
name
"!"
;
}
return
"Hello, "
name
"!"
;
}
console.
log
(
greet
(
"Alice"
)
)
;
console.
log
(
greet
(
"Bob"
,
"Hi"
)
)
;
Running result:
Hello, Alice!
Hi, Bob!
Method overloading
Methods in classes can also be overloaded.
Example
class
Calculator
{
// Overload signatures
add
(
a
:
number
,
b
:
number
)
:
number
;
add
(
a
:
string
,
b
:
string
)
:
string
;
add
(
a
:
number
,
b
:
string
)
:
string
;
add
(
a
:
any
,
b
:
any
)
:
any
{
return
a
b
;
}
}
var
calc
=
new
Calculator
(
)
;
console.
log
(
"Number: "
calc.
add
(
1
,
2
)
)
;
console.
log
(
"String: "
calc.
add
(
"Hello"
,
"World"
)
)
;
console.
log
(
"Mixed: "
calc.
add
(
5
,
" apples"
)
)
;
Running result:
数字: 3
字符串: HelloWorld
混合: 5 apples
Constructor overloading
Constructors can be overloaded as well.
Example
class
User
{
name
:
string
;
age
:
number
;
// Constructor overloads
constructor
(
name
:
string
)
;
constructor
(
name
:
string
,
age
:
number
)
;
constructor
(
name
:
any
,
age
?:
any
)
{
this
.
name
=
name
;
this
.
age
=
age
||
0
;
}
}
var
user1
=
new
User
(
"Alice"
)
;
var
user2
=
new
User
(
"Bob"
,
25
)
;
console.
log
(
"User1: "
JSON.
stringify
(
user1
)
)
;
console.
log
(
"User2: "
JSON.
stringify
(
user2
)
)
;
Running result:
用户1: {"name":"Alice","age":0}
用户2: {"name":"Bob","age":25}
Overloads vs. union types
Using overloads instead of union types yields more precise type inference.
Example
// Recommended: use overloads
function
process
(
value
:
number
)
:
number
;
function
process
(
value
:
string
)
:
string
;
function
process
(
value
:
any
)
:
any
{
if
(
typeof
value
===
"number"
)
{
return
value
2
;
}
return
value.
toUpperCase
(
)
;
}
// TypeScript knows the return type
var
numResult
:
number
=
process
(
10
)
;
// number
var
strResult
:
string
=
process
(
"hello"
)
;
// string
console.
log
(
"Number result: "
numResult
)
;
console.
log
(
"String result: "
strResult
)
;
Running result:
数字结果: 20
字符串结果: HELLO
Notes
- Overload signatures must be placed before the implementation signature
- The implementation signature must be compatible with all overload signatures
- Overload signatures are only type declarations and generate no actual code
Summary
- **Function overloading:** Define multiple signatures; the compiler selects the matching implementation
- **Method overloading:** Also applies in classes
- **Constructor overloading:** Provides multiple ways to initialize
- **Better than union types:** More precise return types