api / const

genericTrap

Creates the fallback trap for values that do not match a specialized trap.

Source: src/api/traps/generic-trap.ts

Creates the fallback trap for values that do not match a specialized trap.

Signature

export const genericTrap = <T>(input: MaybeSignalValue<T>): GenericTrap<T> => {
  return {
    get string() {
      return derive(() => {
        const val = value(input);
        const str = (
          val === undefined || val === null ? "" : val.toString()
        ) as T extends null | undefined ? undefined : string;
        return str;
      });
    },
    or: <OV>(
      orValue: MaybeSignalValue<OV>,
    ): DerivedSignal<OV | NonNullable<T>> =>
      derive(() => value(input) || value(orValue)),
  };
};

Type Parameters

  • The type of the trapped value

Parameters

  • input: A signalified or plain value to trap

Returns

A generic trap with string coercion and fallback selection