Configuration Objects vs Long Parameter Lists
A function with many positional parameters is easy to call wrongly. See how options objects, defaults and types make calls clear, and when positional wins.

A function with five or six positional parameters is easy to write and hard to call correctly. Nobody remembers whether the third argument is the timeout or the retry count, and connect("db", 5432, true, false, 30) tells the reader nothing. A configuration object, usually written as one parameter that destructures into named fields, makes each call say what it means. This post compares the two on Node.js 24 and TypeScript 5.9: how to design options objects with defaults, what to do about required fields, how to validate and extend them safely, and when plain positional parameters are still the better choice.
The problem with positional parameters
Consider a function that grew one parameter at a time.
function createServer(host, port, secure, timeoutMs, retries, logger) {
return { host, port, secure, timeoutMs, retries, hasLogger: Boolean(logger) };
}
console.log(createServer("localhost", 8080, false, 5000, 3, null));
console.log(createServer("localhost", 8080, 5000, false, 3, null));{
host: 'localhost',
port: 8080,
secure: false,
timeoutMs: 5000,
retries: 3,
hasLogger: false
}
{
host: 'localhost',
port: 8080,
secure: 5000,
timeoutMs: false,
retries: 3,
hasLogger: false
}The second call swapped two arguments and nothing complained. JavaScript accepted a timeout of false and a security flag of 5000. The call site gives no hint, and a reviewer must open the function to check each position. Every new parameter also forces callers who do not care about it to pass placeholders such as null to reach later ones.
One object, named fields
Replace the list with a single object, and destructure it with defaults.
function createServer({
host = "localhost",
port = 8080,
secure = false,
timeoutMs = 5000,
retries = 3,
logger = null,
} = {}) {
return { host, port, secure, timeoutMs, retries, hasLogger: Boolean(logger) };
}
console.log(createServer({ port: 9000, secure: true }));
console.log(createServer());{
host: 'localhost',
port: 9000,
secure: true,
timeoutMs: 5000,
retries: 3,
hasLogger: false
}
{
host: 'localhost',
port: 8080,
secure: false,
timeoutMs: 5000,
retries: 3,
hasLogger: false
}Callers name what they set and skip everything else. Order no longer matters, a new option can be added without touching existing calls, and the call site documents itself: { port: 9000, secure: true } is clear without opening the function. The trailing = {} makes the whole argument optional, so createServer() works.
Defaults: where to put them
Defaults in the destructuring pattern are short but have a limitation: they apply only when a value is undefined. Passing null or 0 keeps that value.
function connect({ retries = 3 } = {}) {
return retries;
}
console.log(connect({ retries: undefined }));
console.log(connect({ retries: null }));
console.log(connect({ retries: 0 }));3
null
0undefined falls back to the default, which is usually what you want, since an unset variable passed along gives undefined. null and 0 are kept, because they are real values. If null should also mean "use the default", use ?? explicitly: const n = retries ?? 3. Be deliberate, since a retry count of 0 is a meaningful setting that a || default would silently replace.
For larger sets of options, keep the defaults in one named object and merge, so the defaults are visible and reusable.
const DEFAULTS = Object.freeze({ host: "localhost", port: 8080, timeoutMs: 5000 });
function createClient(options = {}) {
return { ...DEFAULTS, ...options };
}
console.log(createClient({ port: 9000 }));
console.log(createClient({ timeoutMs: undefined }));{ host: 'localhost', port: 9000, timeoutMs: 5000 }
{ host: 'localhost', port: 8080, timeoutMs: undefined }The second result shows a trap of spread merging: an explicit undefined overwrites the default, which is the opposite of what destructuring defaults do. If callers pass options through from other places, undefined values are common, so filter them or use destructuring defaults.
Required and optional together
Most functions have one or two required values and a tail of optional settings. Keep the required ones positional and put the optional ones in an object.
function request(url, { method = "GET", timeoutMs = 5000, headers = {} } = {}) {
return `${method} ${url} timeout=${timeoutMs} headers=${Object.keys(headers).length}`;
}
console.log(request("/users"));
console.log(request("/users", { method: "POST", headers: { "x-id": "1" } }));GET /users timeout=5000 headers=0
POST /users timeout=5000 headers=1This is the shape of fetch(url, options), and it works because the required value is obvious and the rest are tuning. The rule of thumb: positional for what the function is about, an object for how it should behave.
TypeScript makes it safer
In TypeScript, an options interface documents every field, marks which are optional and catches typos at compile time.
interface ServerOptions {
host?: string;
port?: number;
secure?: boolean;
timeoutMs?: number;
}
function createServer({ host = "localhost", port = 8080, secure = false, timeoutMs = 5000 }: ServerOptions = {}) {
return `${secure ? "https" : "http"}://${host}:${port} (${timeoutMs}ms)`;
}
console.log(createServer({ port: 9000, secure: true }));
// createServer({ prot: 9000 }); // error: 'prot' does not exist in type 'ServerOptions'https://localhost:9000 (5000ms)The commented line shows the compiler rejecting a misspelled option, which a plain JavaScript call would accept and ignore, leaving you with the default port and a confusing bug. For options where fields depend on each other, use a union so invalid combinations do not type check.
type Auth =
| { kind: "none" }
| { kind: "token"; token: string }
| { kind: "basic"; user: string; password: string };
function describe(auth: Auth): string {
switch (auth.kind) {
case "none":
return "anonymous";
case "token":
return `token ending ${auth.token.slice(-4)}`;
case "basic":
return `user ${auth.user}`;
}
}
console.log(describe({ kind: "token", token: "abcd1234" }));
console.log(describe({ kind: "basic", user: "ada", password: "x" }));token ending 1234
user adaA caller cannot pass a token together with a password, because no member of the union has both. Constraints that were once documentation become compile-time checks.
Validating at run time
TypeScript is gone at run time, and plain JavaScript callers have no types at all. For options that arrive from configuration files or user input, validate them where the function starts.
function createPool({ size = 10, idleMs = 30000, ...unknown } = {}) {
const extra = Object.keys(unknown);
if (extra.length > 0) throw new TypeError(`unknown option(s): ${extra.join(", ")}`);
if (!Number.isInteger(size) || size < 1) throw new RangeError("size must be a positive integer");
return { size, idleMs };
}
console.log(createPool({ size: 4 }));
for (const bad of [{ szie: 4 }, { size: 0 }]) {
try {
createPool(bad);
} catch (err) {
console.log(err.name, err.message);
}
}{ size: 4, idleMs: 30000 }
TypeError unknown option(s): szie
RangeError size must be a positive integerCollecting leftover keys with a rest element, ...unknown, catches typos in plain JavaScript: szie is reported and not silently ignored. This is the guard that types give you for free in TypeScript.
Evolving the options
Adding a field is backward compatible: existing callers do not pass it, and the default applies. Removing or renaming one is not. When you rename, accept both for a release and warn about the old name, so callers have time to move.
function createCache({ ttlMs, ttl, maxItems = 100 } = {}) {
if (ttl !== undefined) {
console.warn("option 'ttl' is deprecated, use 'ttlMs'");
}
return { ttlMs: ttlMs ?? ttl ?? 60000, maxItems };
}
console.log(createCache({ ttl: 5000 }));{ ttlMs: 5000, maxItems: 100 }The warning goes to standard error, so it does not appear in the standard output shown above. Because options are named, this kind of gradual change is possible, while a positional signature offers no way to rename a position.
When positional parameters are better
An options object is not always the right call. For one or two parameters whose meaning is obvious from the function name, positional is shorter and clearer.
Math.max(a, b),clamp(value, min, max)andadd(x, y)read perfectly well as they are.Callbacks passed to
mapandreducerely on fixed positions that every reader knows.Hot paths that run millions of times can avoid the small cost of allocating an options object, though you should measure before choosing on performance alone.
The signal to switch is when a call needs a comment to explain its arguments, when two parameters of the same type sit next to each other, or when booleans appear in the list. A function called with true, false has an options object waiting to be written.
Testing
Test that omitted options fall back to the defaults, that explicit falsy values are respected and that unknown options are rejected.
import assert from "node:assert/strict";
import { test } from "node:test";
test("explicit zero is kept and undefined uses the default", () => {
assert.equal(createServer({ retries: 0 }).retries, 0);
assert.equal(createServer({ retries: undefined }).retries, 3);
assert.equal(createServer().retries, 3);
});✔ explicit zero is kept and undefined uses the default (0.8ms)
ℹ tests 1
ℹ pass 1
ℹ fail 0Documenting options
An options object is part of your function's public interface, so document each field where people will look for it. In plain JavaScript, a JSDoc @typedef for the options and a @param that refers to it gives editors completion and hover help. In TypeScript the interface itself is the documentation, and a doc comment above each field appears in the editor when a caller types the property name. State the default and the unit in the comment: timeoutMs already says milliseconds in its name, and a comment adds that zero means "no timeout" if that is true.
Passing options through layers
Functions that wrap other functions often forward their options. Forward only what the inner function understands, or you will pass unknown keys downward and trigger its validation. Pull out the options you own with a rest element, and pass the remainder along, so a new option in the inner function works without changing the wrapper.
function inner({ retries = 3, timeoutMs = 1000 } = {}) {
return `retries=${retries} timeoutMs=${timeoutMs}`;
}
function outer({ label = "job", ...innerOptions } = {}) {
return `${label}: ${inner(innerOptions)}`;
}
console.log(outer({ label: "sync", timeoutMs: 250 }));sync: retries=3 timeoutMs=250The wrapper handles label itself and hands everything else to inner, so adding retries to a call just works.


