Helper Types
AtLeastOne<T extends Record<string, any>>
Makes sure that at least one key of your Record is set.
ts
type MyType = AtLeastOne<{
zipcode?: string
city?: string
}>MakeOptional<T extends object, K extends keyof T>
Will make all the keys K of the object T optional.
ts
type Customer = {
name: string
phone: string
}
type CustomerWithOptionalPhone = MakeOptional<Customer, "phone">DeepKeys<T extends object>
Creates a String Union of all the Objects keys and will use dot-notation for nested Keys recursively.
ts
type MyKeys = {
Foo: string,
Bar: {
Schning: string
}
}
type AnyKeys = DeepKeys<MyKeys>
// AnyKeys = "Foo" | "Bar.Schning"INFO
This powers Spire's Image and Icon typehints.
UnionToIntersection<U>
Creates an Intersection out of a Union.
ts
export const manufacturers = [
"Beyerdynamic",
"AKG",
"Universal Audio"
] as const
// manufacturers = Readonly<["Beyerdynamic", "AKG", "Universal Audio"]>
type Intersection = UnionToIntersection<typeof manufacturers>
// Intersection = Readonly<["Beyerdynamic", "AKG", "Universal Audio"] | undefined>
// The Magic starts by combining a Partial
type PartialIntersection = Partial<Intersection>
// PartialIntersection = Readonly<[("Beyerdynamic" | undefined)?, ("AKG" | undefined)?, ("Universal Audio" | undefined)?]| undefined>ActionRequestType<T extends Action>
Extract the Request-Type from an Action.
ts
const { create } = createCustomerAction()
type Request = ActionRequestType<typeof create>
// Request = { id: string, last_name: string, ... }ActionResponseType<T extends Action>
Extract the Response-Type from an Action.
ts
const { create } = createCustomerAction()
type Response = ActionResponseType<typeof create>
// Response = { id: string, last_name: string, ... }ActionHasPagination<T extends Action>
Check if a Request implements Pagination.
ts
const { get } = allCustomersAction()
type HasPagination = ActionHasPagination<typeof get>
// HasPagination = false (if no pagination) or true (if pagination)