api / const
op
Creates an operation object for composing logical and mathematical operations.
Creates an operation object for composing logical and mathematical operations.
This function evaluates the input and dispatches to the appropriate operation implementation based on the evaluated type:
- Number values → numberOp (provides math operations: add, sub, mul, div, etc.)
- String or Array values → stringAndArrayOp (provides length-based operations)
- Other types → genericOp (provides logical operations: or, and, equals, etc.)
Signature
export const op = <T>(input: MaybeSignalValue<T> | (() => T)): Operation<T> => {
const evaluator: () => T =
typeof input === "function"
? (input as () => T)
: (): T => value(input as MaybeSignalValue<T>);
const val = evaluator();
return (
typeof val === "number"
? numberOp(input as MaybeSignalValue<number> | (() => number))
: typeof val === "string" || Array.isArray(val)
? stringAndArrayOp(
input as
| MaybeSignalValue<string | unknown[]>
| (() => string | unknown[])
)
: genericOp(input)
) as Operation<T>;
};
Type Parameters
- The type of value the operation works with
Parameters
input: A signal, plain value, or value-producing function
Returns
A type-specific operation object with chainable methods
Remarks
- The operation type is determined by the runtime type of the evaluated value
- If input is a function, it is called to get the value
- Type changes in the input signal are not reflected in the operation type
- Methods return new operation objects for chaining
- Final results are obtained via getters such as
truthy,falsy, andresult
Examples
const count = signal(5);
const operation = op(count);
const doubled = operation.add(5).result; // DerivedSignal<number>
const text = signal("hello");
const textOp = op(text);
const isLong = textOp.lengthGT(5).truthy; // DerivedSignal<boolean>
const value = signal(10);
const check = op(value).isBetween(5, 15).truthy; // DerivedSignal<boolean>
See Also
- Operation - For the operation type union
- MaybeSignalValue - For the input type