Skip to content

@kaelyx/ts-kit / array / sum

Function: sum() ​

sum(array, options?) ​

sum(array, options?): number

Defined in: array/sum/sum.ts:26

Adds all the numbers in an array.

Parameters ​

array ​

readonly number[]

The numbers to add. An empty array gives initialValue.

options? ​

SumNumberOptions

Optional. See SumNumberOptions.

Returns ​

number

initialValue plus the total of the numbers.

Example ​

ts
sum([1, 2, 3])                      // 6
sum([])                             // 0
sum([1, 2, 3], { initialValue: 10 }) // 16

sum<T>(array, options) ​

sum<T>(array, options): number

Defined in: array/sum/sum.ts:43

Adds one number value from each object in an array.

Type Parameters ​

T ​

T extends object

Parameters ​

array ​

readonly T[]

The objects. An empty array gives initialValue.

options ​

SumObjectOptions<T>

See SumObjectOptions. key must be a key whose value is a number.

Returns ​

number

initialValue plus the total of the values at key.

Throws ​

If missingBehaviour is 'THROW' and an object has no value at key.

Example ​

ts
sum([{ price: 2 }, { price: 3 }], { key: 'price' }) // 5
sum([{ price: 2 }, {}], { key: 'price', missingBehaviour: SumOptionsMissingBehaviour.zero }) // 2

Types ​

SumKey<T> type ​

SumKey<T> = { [K in keyof T]-?: [T[K]] extends [number | undefined] ? [T[K]] extends [undefined] ? never : K : never }[keyof T]

Defined in: array/sum/sum.types.ts:10

The keys of T that have a value of type number. An optional key is included. See missingBehaviour in SumObjectOptions for what happens when its value is missing.

Type Parameters ​

T ​

T


SumOptionsMissingBehaviour type ​

SumOptionsMissingBehaviour = ValueOf<typeof SumOptionsMissingBehaviour>

Defined in: array/sum/sum.types.ts:19

What sum does when an object has no value at key.


SumNumberOptions interface ​

Defined in: array/sum/sum.types.ts:30

Options for sum when the array holds numbers.

Properties ​

initialValue? ​

readonly optional initialValue?: number

Defined in: array/sum/sum.types.ts:32

The value to start the total from.

Default ​
ts
0

key? ​

readonly optional key?: undefined

Defined in: array/sum/sum.types.ts:34

Not used for an array of numbers.


missingBehaviour? ​

readonly optional missingBehaviour?: undefined

Defined in: array/sum/sum.types.ts:36

Not used for an array of numbers.


SumObjectOptions<T> interface ​

Defined in: array/sum/sum.types.ts:40

Options for sum when the array holds objects.

Type Parameters ​

T ​

T

Properties ​

initialValue? ​

readonly optional initialValue?: number

Defined in: array/sum/sum.types.ts:42

The value to start the total from.

Default ​
ts
0

key ​

readonly key: SumKey<T>

Defined in: array/sum/sum.types.ts:44

The key of the number to add from each object. See SumKey.


missingBehaviour? ​

readonly optional missingBehaviour?: "THROW" | "ZERO"

Defined in: array/sum/sum.types.ts:46

What the function does when the value at key is missing or undefined.

Default ​
ts
SumOptionsMissingBehaviour.zero