or a type alias(unions, primitive,tuple, intersection, etc)
Extends
Interfaces only
Union
Intersection
Tuples
Array of fixed size that can have different known types
Any vs unknown
Use unknown as it blocks fields unless narrowed, as any lets you do untypesafe things
Never
Never is the empty set and will never have a value
Functions
Generics
Advanced
This works because conditional types distribute over type unions. Given any type of the form T extends U ? X : Y when a union type is substituted for T the type expands to distribute the condition to each branch of that union type:
interface Name {
name: “string”
};
interface Age {
age: number
};
type Person = Name & Age;
type Reponse = [string, number]
function maxHeightWidth(
ratio: number,
maxWidth: number,
maxHeight: number
): [width: number, height: number]
function timeout(ms: number): Promise<never> {
return new Promise((_, reject) =>
setTimeout(() => reject(new Error("Timeout elapsed")), ms)
)
}
//useful because will only be return type of fetchStock as T | never = T
const stock = await Promise.race([
fetchStock(tickerSymbol),
timeout(3000)
])
function firstElement<Type>(arr: Type[]): Type | undefined {
return arr[0];
}
type NonNullable<T> = T extends null | undefined ? never : T;