Typescript - More on Functions (2)
更新時間: 2023/11/05
Function Overloads
In TypeScript, we can specify a function that can be called in different ways by writing overload signatures. To do this, write some number of function signatures (usually two or more), followed by the body of the function:
function makeDate(timestamp: number): Date;
function makeDate(m: number, d: number, y: number): Date;
function makeDate(mOrTimestamp: number, d?: number, y?: number): Date {
if (d !== undefined && y !== undefined) {
return new Date(y, mOrTimestamp, d);
} else {
return new Date(mOrTimestamp);
}
}
- First two are called
overload signatures, then third is calledimplementation Signature - The signature of the implementation is not visible from the outside. When writing an overloaded function, you should always have two or more signatures above the implementation of the function.
- The implementation signature must also be compatible with the overload signatures.
- Always prefer parameters with union types instead of overloads when possible (ex: x: any[] | string)
Declare this in a Function
const user = {
id: 123,
admin: false,
becomeAdmin: function () {
this.admin = true;
},
};
interface DB {
filterUsers(filter: (this: User) => boolean): User[];
}
const db = getDB();
const admins = db.filterUsers(function (this: User) {
return this.admin;
});
- need to use function instead of arrow function
Other Types to Know About
void: represents the return value of functions which don’t return a value, but is not the same asundefinedunknown: represents any value, similar toanybut but is safer because it’s not legal to do anything with anunknownvaluenever: represents values which are never observed. In a return type, this means that the function throws an exception or terminates execution of the program.- also appears when TypeScript determines there’s nothing left in a union.
function fn(x: string | number) { if (typeof x === "string") { // do something } else if (typeof x === "number") { // do something else } else { x; // has type 'never'! } }