getSettings
export function getSettings<T, S extends (Subset<T> & Record<string, any>)[]>(
target: T, ...sources: S
): MergedSettings<T, S>
Description
This function merges partial objects into a target. If no sources are specified, a duplicate of the target will be returned. The sources are handled in the order they are provided, with any properties in later sources superseding those in earlier ones. If a source property is an object, it will be merged with the corresponding target property.
This function is widely used throughout the framework to add to existing settings, create new layers of settings, or customise variants.
Import
This function can be imported as such:
import {getSettings} from '@unless/component-library.utils.helpers'
Parameters
Name | Type | Description |
---|---|---|
target | T | The destination to merge into. Any properties that are redefined in the sources will take precedence over those in the destination, and any new properties will be added. Note: If a sub-property is only partially redefined in the sources, only that part will be overridden. |
...sources | (Subset<T> & Record<string, any>)[] | This refers to any object. Properties that match the structure of target should be of the same data type. The priority of sources is in the descending order into which they are provided. |
Return
MergedSettings<T, S>
- The
getSettings
function returns an object of typeMergedSettings<T, S>
, whereT
is the target object type andS
is the union of all the sources. TheMergedSettings
type is a helper type that represents the merged object of theT
andS
types, with the properties ofS
taking precedence over the corresponding properties ofT
. - The
MergedSettings
type is used to ensure that the returned object is a complete and correct representation of the merged object, with all properties accounted for and properly typed.
- The
Updated 9 months ago
What’s Next