Types
Variable types cannot change afterwards.
Type being inferred from the value:
let str = "This is a string"; // stringA variable declared with no type will by default be of any type.
let str; // anyBuilt-in primitive types includes:
booleanstringnumber: Representing integers and floats.undefinednullanyunknownnevervoidbigintsymbol
Common built-in JS Objects:
DateErrorArray or []MapSetRegexpPromise
Type Literals:
Object:
{ field: number }Function:
(arg: number) => numberArrays:
Array<number> or number[]Tuple:
[number, number]
any
anyVery flexible and can accept any type.
Despite it's flexibility, it is discouraged to use it, since it may break your code's type enforcement and thus the use of Typescript.
If you don't know the type, preffer using unkonwn.
Variable of any type may also receive different types values at any point.
unknown
unknownRepresents any value and while it is similar to any type, it is much safer because it's not legal to do anything with an unknown value.
This is useful when describing function types because you can describe functions that accept any value without having any values in your function body.
never
neverIt is a return type, meaning that it never returns a value.
This should mean that the function either throws an exception or terminates execution of the program.
void
voidRepresents the return value of functions which don't return a value.
Tuples
Are not a "type" per say, but it is a way to use the [] type, enforcing a size and adding values change constrains.
type
Used for type definitions, usually to reduce repetition.
It can be amost the same as
interface, but it cannot be extendend.They may not be extended but
typescan be merged.
interface
Can be extended by declaring it multiple times, or merged with other
interfaces.Used to describe object shapes.
Can have function definitions. (Great to describe classes or contracts)
You can merge different interface into a type:
Extend it with extends:
It is also possible to extend via merging when the interfaces have the same name:
You can ensure a class conforms to an interface via implements.
Are a feature which allows for describing a value which could be one of a set of possible named constants.
It is NOT a type-level addition, but something added to the language and runtime.
Function Type
A way to declare the Function structure types (parameters and return).
You can generically say a variable is a function.
Or you can explicitly define it's params and return types.
Union Type
Describes a type which is one of many options. (These options are may be types or values)
Template Union Types
A template string can be used to combine and manipulate text inside the type system.
Intersection Types
A way to merge/extend types. (&), as seen above.
Partial
Constructs a type with all properties of Type set to optional. This utility will return a type that represents all subsets of a given type. (So that you dont have to use ?:)
Required
Constructs a type consisting of all properties of Type set to required.
Pick
Constructs a type by picking the set of properties Keys (string literal or union of string literals) from Type.
Omit
Constructs a type by picking all properties from Type and then removing Keys (String literal or union of string literals).
Conditional Types
Acts as if statements inside the type system. Created via generics, and then commonly used to reduce the number of options in a Type Union.
Generic Types
Are types that will hold a generic type, so that this type can change at runtime.
Will be the name inside <>, this name can be anything. (Conventionally it is used as T or R)
You can specify one or more generic types.
You may specify a default type for the generic if nothing was informed. (Otherwise Typescript will require you to specify a type)
You can constrain what types are accepted into the generic parameter with the extends keyword.
Mapped Types
Acts like a map statement for the type system, allowing an input type to change the structure of the new type.
Type Assertions
When you have information about the type of a value that Typescript can't know about.
Sometimes this rule can be too conservative and will disallow more complex coercions that might be valid.
If this happens you can use two assertions, first to any or unkonwn then to the desired type.
Last updated