···163extern dec errors(target: unknown, ...errors: unknown[]);
164165/**
0000000000000000000000000166 * Marks a namespace as external, preventing it from emitting JSON output.
167 * This decorator can only be applied to namespaces.
168 * Useful for importing definitions from other lexicons without re-emitting them.
···163extern dec errors(target: unknown, ...errors: unknown[]);
164165/**
166+ * Specifies a default value for a scalar or union definition.
167+ * Only valid on standalone scalar or union defs (not @inline).
168+ * The value must match the underlying type (string, integer, or boolean).
169+ * For unions with token refs, you can pass a model reference directly.
170+ *
171+ * @param value - The default value (literal or model reference for tokens)
172+ *
173+ * @example Scalar with default
174+ * ```typespec
175+ * @default("standard")
176+ * scalar Mode extends string;
177+ * ```
178+ *
179+ * @example Union with token default
180+ * ```typespec
181+ * @default(Inperson)
182+ * union EventMode { Hybrid, Inperson, Virtual, string }
183+ *
184+ * @token
185+ * model Inperson {}
186+ * ```
187+ */
188+extern dec `default`(target: unknown, value: unknown);
189+190+/**
191 * Marks a namespace as external, preventing it from emitting JSON output.
192 * This decorator can only be applied to namespaces.
193 * Useful for importing definitions from other lexicons without re-emitting them.
+17
packages/emitter/src/decorators.ts
···25const maxBytesKey = Symbol("maxBytes");
26const minBytesKey = Symbol("minBytes");
27const externalKey = Symbol("external");
02829/**
30 * @maxBytes decorator for maximum length of bytes type
···294295export function isReadOnly(program: Program, target: Type): boolean {
296 return program.stateSet(readOnlyKey).has(target);
0000000000000000297}
298299/**
···25const maxBytesKey = Symbol("maxBytes");
26const minBytesKey = Symbol("minBytes");
27const externalKey = Symbol("external");
28+const defaultKey = Symbol("default");
2930/**
31 * @maxBytes decorator for maximum length of bytes type
···295296export function isReadOnly(program: Program, target: Type): boolean {
297 return program.stateSet(readOnlyKey).has(target);
298+}
299+300+/**
301+ * @default decorator for setting default values on scalars and unions
302+ * The value can be a literal (string, number, boolean) or a model reference for tokens
303+ */
304+export function $default(context: DecoratorContext, target: Type, value: any) {
305+ // Just store the raw value - let the emitter handle unwrapping and validation
306+ context.program.stateMap(defaultKey).set(target, value);
307+}
308+309+export function getDefault(
310+ program: Program,
311+ target: Type,
312+): any | undefined {
313+ return program.stateMap(defaultKey).get(target);
314}
315316/**
···1+import "@typelex/emitter";
2+3+namespace com.example.scalarDefaults {
4+ /** Test default decorator on scalars */
5+ model Main {
6+ /** Uses string scalar with default */
7+ mode?: Mode;
8+9+ /** Uses integer scalar with default */
10+ limit?: Limit;
11+12+ /** Uses boolean scalar with default */
13+ enabled?: Enabled;
14+ }
15+16+ /** A string type with a default value */
17+ @default("standard")
18+ @maxLength(50)
19+ scalar Mode extends string;
20+21+ /** An integer type with a default value */
22+ @default(50)
23+ @minValue(1)
24+ @maxValue(100)
25+ scalar Limit extends integer;
26+27+ /** A boolean type with a default value */
28+ @default(true)
29+ scalar Enabled extends boolean;
30+}