Please enable JavaScript.
Coggle requires JavaScript to display documents.
TypeScript: Handbook (Part 2) - Coggle Diagram
TypeScript: Handbook (Part 2)
Functions
Typing functions
Function type expressions
Arrow function syntax
Parameter names are required
Call signatures
Functions with properties or objects that can be called
key: value syntax
Construct signatures
Call signature preceded by the
new
keyword
An object can have both a call signature and a construct signature
Date
Generic functions
One or more type parameters after the function name and before the parameter list
Prefer non-constrained type parameters over constrained type parameters
A different type parameter can be used between values of as long as they line up
Parameters
Optional parameters
Optional parameters are not required to be specified for callbacks
Extra parameters are ignored
Suffixed with ?
Parameter would implicitly be typed
T | undefined
even if it's explicitly typed to
T
Rest parameters and arguments
Rest parameters are implicitly type
any[]
Can be explicitly typed to array and tuple types
Spreading an array as function arguments may result in an argument count mismatch
Use a
const
assertion
Overloads
Two or more overload signatures can be specified before the body of the function
Implementation signature is not visible from the outside and cannot be called
Overload signatures should be compatible with the implementation signature in parameter and return types
Prefer parameters with union types over overloads
this
Value of
this
would most often be inferred based on code flow analysis
Can be explicitly typed in the parameter list similar to other parameters
Objects types
Typing
Can be typed using an interface, type alias, or remain anonymous
There's no way to place annotations within destructuring patterns
Properties
Optional properties
Yield
T | undefined
as a type
readonly
properties
Cannot be written to before runtime
readonly
properties can be changed through a writable property of a compatible type
Not factored in for type compatibility checks
Enforces intent of not mutating a property
Index signatures
[index: T]: U
where the index type can be either
string
or
number
Return type of the numeric indexer should be compatible with the return type of the string indexer when both exist side by side
Numeric indexers are converted to strings by JavaScript
Can be marked
readonly
to prevent assigning of new keys
Should employ union types to support different return types
Interfaces and intersection types
It's possible to extend multiple types at once
An intersection type
T & U
has all the members of
T
and
U
Arrays
T[]
is shorthand for
Array<T>
readonly T[]
is shorthand for
ReadOnlyArray<T>
A regular array can be assigned to a
ReadOnly
array type but not vice versa
Tuples
Length of tuple and types of each position are known at compile time
Equivalent to arrays with known properties and a numeric
length
property
Optional properties
Can only come at the end
Affects length
Union of two
number
literals
Rest elements
Have to be an array- or tuple-typed
Can appear anywhere inside the tuple
Has no
length
property
readonly
tuples
Array literal with
const
assertions are inferred as
readonly
tuples
Classes
Members
Fields
readonly
fields
Prevents assignment outside of initialisation and the constructor
Fields without initialisers are implicitly typed as
any
Optional fields
Postfix ?
Parameters properties
Constructor parameters prefixed with visibility modifiers or
readonly
Creates a field with the same name and value
Constructors
Typing
No type parameters
Uses the type parameters from the class declaration
No return type
Always returns the class instance type
Can be overloaded
A
super()
call is required before accessing members through the
this
reference
Methods
this
reference
Unqualified names refer to identifiers in the enclosing scope
Other class members can only be accessed through the
this
reference
Accessors
Methods with
get
or
set
keyword
Both accessors should have the same visibility
Getter return type and setter parameter annotation should match
Index signatures
Same as object types but less useful
Inheritance
implements
Checks compatibility of a class with one or many interfaces
Does not influence the type of the class or its members
extends
Derived classes have all members of their base classes
Base class constructor runs before derived class initialisations
Overriding
Both base class fields and methods can be overridden
super
keyword can be used to access base class methods but not fields
Base class contract should adhered to when overriding
Abstract classes and members
Abstract classes cannot be directly instantiated
Abstract methods and fields can only exist inside abstract classes
Parameters of constructor type should be typed as construct signatures
Prevents attempts at instantiating abstract classes
Extending built-in types
Limitations
Methods on derived type instances may be
undefined
instanceof
may be broken between derived type instances
Workaround
Manually adjusting the prototype immediately after
super()
calls
Descendants of derived classes may in turn need to set the prototype
IE10 and below
Manually copying methods from the prototype to the instance (
this
)
Member visibility
public
Default
protected
Visible inside own class and derived classes
Derived classes may expose protected members of super class as public
Field with no modifier
Protected members can only be accessed through an instance of the same class or a subclass
Different from Java
private
Private identifier names cannot be reused in derived classes
Private members of a different instance of the same class are accessible
Similar to Java
Enforced at build-time only
Static members
Static members are inherited
Can be modified with visibility modifiers
Certain properties of
Function
cannot be used as static member identifiers
Classes are functions
No static classes
Static members cannot access type parameters of generic classes
Type erasure
this
reference and types
Method declaration
this
reference is bound dynamically
Typed
this
parameter
Only checked at compile time
One instance of method per class definition
Derived classes can access method using the
super
syntax
Arrow functions
this
reference is bound lexically
Derived classes cannot access method using the
super
syntax
Each instance has its own version of the method
this
type
Dynamically refers to the type of the current class
Useful for operating on instances of the same class
Type guards
this is T
return types on class and interface methods can be used to narrow objects to type T
Useful for lazy validation of fields
Basics
Can be declared as a class declaration or a class expression
Compared structurally
Classes and interfaces with a similar structure are type-compatible
A class that extends another class without explicitly inheriting is compatible with the other class
Modules
Definition
A file that contains top-level
import
or
export
statements
Script contents are available in the global scope (and in modules)
Type imports
import type
Type imports are removed from output code
Namespaces
ES modules are preferred over namespaces
CommonJS support
import val = require('')
syntax can be used when importing default exports with
export =