Rule: When possible, use the type parameter itself rather than constraining it
Rule: Always use as few type parameters as possible
Rule: If a type parameter only appears in one location, strongly reconsider if you actually need it
When writing a function type for a callback, never write an optional parameter unless you intend to call the function without passing that argument
Signatures
Call Signatures
In JavaScript, functions can have properties in addition to being callable. However, the function type expression syntax doesn’t allow for declaring properties. If we want to describe something callable with properties, we can write a call signature in an object type:
Construct Signatures
JavaScript functions can also be invoked with the new
operator. TypeScript refers to these as constructors because they usually create a new object. You can write a construct signature by adding the new
keyword in front of a call signature:
The parenthesis is for argument, it is actually a function.
Function Overloads
- Always prefer parameters with union types instead of overloads when possible