withastro/astro · error · Error

${key} expects a "${defaultType}" value!

Error message

${key} expects a "${defaultType}" value!

What it means

The `astro preferences set <key> <value>` subcommand compares the runtime type of the coerced value against the type of the default value stored in `DEFAULT_PREFERENCES` (looked up via `dlv`). If `coerce(key, value)` does not produce the expected `boolean`/`string`/`number`, the set is rejected before persisting. This keeps preference files internally consistent.

Source

Thrown at packages/astro/src/cli/preferences/index.ts:164

			console.log(msg.preferenceDefault(key, defaultValue));
			return 0;
		}
		console.log(msg.preferenceGet(key, value));
		return 0;
	} catch {}
	return 1;
}

async function setPreference(
	settings: AstroSettings,
	key: PreferenceKey,
	value: unknown,
	{ location }: SubcommandOptions,
) {
	try {
		const defaultType = typeof dlv(DEFAULT_PREFERENCES, key);
		if (typeof coerce(key, value) !== defaultType) {
			throw new Error(`${key} expects a "${defaultType}" value!`);
		}

		await settings.preferences.set(key, coerce(key, value), { location });
		console.log(msg.preferenceSet(key, value));
		return 0;
	} catch (e) {
		if (e instanceof Error) {
			console.error(msg.formatErrorMessage(collectErrorMetadata(e), true));
			return 1;
		}
		throw e;
	}
}

async function enablePreference(
	settings: AstroSettings,
	key: PreferenceKey,
	{ location }: SubcommandOptions,

View on GitHub (pinned to d081033d5f)

Solutions

  1. Inspect the expected type: `astro preferences get <key>` and read the default printed, or check `packages/astro/src/preferences/defaults.ts`.
  2. For boolean preferences use `astro preferences enable <key>` / `disable <key>` instead of `set`.
  3. Supply a literal of the correct type (e.g. `astro preferences set <key> true` for booleans, a number for numeric keys).
  4. Use `astro preferences reset <key>` to restore a known-good default if you are unsure what the value should be.

Example fix

# before
$ astro preferences set devToolbar.enabled yes
# after
$ astro preferences enable devToolbar
# or
$ astro preferences set devToolbar.enabled true
Defensive patterns

Strategy: validation

Validate before calling

import dlv from 'astro/preferences/dlv'; // illustrative
const defaultType = typeof dlv(DEFAULT_PREFERENCES, key);
if (typeof coerce(key, value) !== defaultType) {
  throw new Error(`Pass a ${defaultType} for ${key}`);
}

Type guard

function isPreferenceValue(key, value) {
  const map = { 'devToolbar.enabled': 'boolean', 'devToolbar.open': 'boolean' };
  return typeof value === (map[key] ?? 'string');
}

Prevention

When it happens

Trigger: Calling `astro preferences set devToolbar.enabled notabool` for a boolean preference (default type is `boolean`, coerced string is `string`); passing a non-numeric value to a numeric preference; passing a string to a preference whose default is an object/`undefined`.

Common situations: Assuming a preference is a string when it is actually boolean (e.g. `devToolbar.enabled`); quoting values that get coerced to strings unexpectedly; copying a value from docs without checking its declared type in `DEFAULT_PREFERENCES`.

Related errors


AI-assisted analysis of withastro/astro@d081033d5f (2026-08-12). Data as JSON: /api/errors/1ac00b4412786c65. Report an issue: GitHub.