More on Functions
Function Type Expressions
The simplest way to describe a function is with a function type expression.
1 | |
we can use a type alias to name a function type:
1 | |
Call Signatures 调用签名
if we want to describe something callable with properties, we can write a call signature in an object type:
1 | |
Construct Signatures
You can write a construct signature by adding the new keyword in front of a call signature.
1 | |
Some objects, like JavaScript’s Date object, can be called with or without new You can combine call and construct signatrues in the same type arbitarily.
1 | |
Generic Functions 通用函数
In TypeScript, generics are used when we want to describe a correspondence between two values. We do this by declaring a type parameter in function signature.
1 | |
When we need a length property that’s a number. We constrain the type parameter to that type by writing an extends clause:
1 | |
Optional Parameters in Callbacks
1 | |