Skip to content

getOptionEnum

Import from @varavel/vdl-plugin-sdk/utils/options.

function getOptionEnum<T>(
   options, 
   key, 
   allowedValues, 
   defaultValue): T;

Returns a string option constrained to a known set of allowed values.

Missing, blank, and unsupported values fall back to the provided default.

Type Parameters

Type Parameter
T extends string

Parameters

Parameter Type Description
options Record\<string, string> | undefined Plugin options record.
key string Option name to read.
allowedValues readonly T[] Supported string values for the option.
defaultValue T Value returned when the option is missing or invalid.

Returns

T

A valid enum-like string from allowedValues.

Example

getOptionEnum({ format: "esm" }, "format", ["cjs", "esm"] as const, "cjs");
// returns "esm"