@omnifyjp/ts 5.8.32 → 5.8.34

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -15,7 +15,18 @@ export declare function schemaToEnum(schema: SchemaDefinition, options: Generato
15
15
  export declare function generateEnums(schemas: Record<string, SchemaDefinition>, options: GeneratorOptions): TSEnum[];
16
16
  /** Generate enums from plugin enums (customTypes.enums in schemas.json). */
17
17
  export declare function generatePluginEnums(pluginEnums: Record<string, string[]>, _options: GeneratorOptions): TSEnum[];
18
- /** Format a TypeScript enum with helpers (Values array, type guard, label getter). */
18
+ /** Format a TypeScript enum with helpers (Values array, type guard, label getter).
19
+ *
20
+ * Emits the `as const` object pattern instead of `export enum X { ... }`.
21
+ * Reasoning: TS 5.5+ ships with `erasableSyntaxOnly` enabled by default in
22
+ * Vite 7 starters and recommended by the TS team for new projects, which
23
+ * disallows enums (they emit runtime code that type-only-stripping tools
24
+ * like esbuild + deno + bun can't erase). The const-object pattern
25
+ * preserves identical call-site ergonomics — `X.User` still resolves to
26
+ * the string value, the union type still narrows correctly, label /
27
+ * value-array helpers all work — but compiles under every strict-mode
28
+ * preset. Pilot #103 Issue 1.
29
+ */
19
30
  export declare function formatEnum(enumDef: TSEnum): string;
20
31
  /** Format a TypeScript type alias with helpers. */
21
32
  export declare function formatTypeAlias(alias: TSTypeAlias): string;
@@ -97,16 +97,33 @@ function lowerFirst(str) {
97
97
  function escapeString(str) {
98
98
  return str.replace(/\\/g, '\\\\').replace(/'/g, "\\'");
99
99
  }
100
- /** Format a TypeScript enum with helpers (Values array, type guard, label getter). */
100
+ /** Format a TypeScript enum with helpers (Values array, type guard, label getter).
101
+ *
102
+ * Emits the `as const` object pattern instead of `export enum X { ... }`.
103
+ * Reasoning: TS 5.5+ ships with `erasableSyntaxOnly` enabled by default in
104
+ * Vite 7 starters and recommended by the TS team for new projects, which
105
+ * disallows enums (they emit runtime code that type-only-stripping tools
106
+ * like esbuild + deno + bun can't erase). The const-object pattern
107
+ * preserves identical call-site ergonomics — `X.User` still resolves to
108
+ * the string value, the union type still narrows correctly, label /
109
+ * value-array helpers all work — but compiles under every strict-mode
110
+ * preset. Pilot #103 Issue 1.
111
+ */
101
112
  export function formatEnum(enumDef) {
102
113
  const { name, values, comment } = enumDef;
103
114
  const parts = [];
104
115
  if (comment) {
105
116
  parts.push(`/**\n * ${comment}\n */\n`);
106
117
  }
107
- // Enum definition
108
- const enumValues = values.map(v => ` ${v.name} = '${v.value}',`).join('\n');
109
- parts.push(`export enum ${name} {\n${enumValues}\n}\n\n`);
118
+ // Const object — works under erasableSyntaxOnly. Identical access at
119
+ // call sites: `X.User === 'user'` (literal string equality).
120
+ const enumValues = values.map(v => ` ${v.name}: '${v.value}',`).join('\n');
121
+ parts.push(`export const ${name} = {\n${enumValues}\n} as const;\n\n`);
122
+ // Type alias — string union derived from the const object's values. A
123
+ // function parameter typed `${name}` accepts `X.User` AND the literal
124
+ // 'user' (same shape as enums had). The `keyof typeof` extraction is
125
+ // the canonical TS pattern for this.
126
+ parts.push(`export type ${name} = typeof ${name}[keyof typeof ${name}];\n\n`);
110
127
  // Values array
111
128
  parts.push(`/** All ${name} values */\n`);
112
129
  parts.push(`export const ${name}Values = Object.values(${name}) as ${name}[];\n\n`);
package/dist/generator.js CHANGED
@@ -106,10 +106,15 @@ function generateBaseInterfaceFile(schemaName, schemas, options) {
106
106
  // would be empty noise.
107
107
  const formShape = buildFormShape(schema, schemas, options);
108
108
  if (formShape.fields.length > 0) {
109
- // The builders depend on `Locale` (from common.ts) and the shared
110
- // `buildI18nPayload` / `emptyLocaleMap` helpers (from payload-helpers.ts).
111
- // Inject the imports near the top of the file.
112
- insertPayloadImports(parts);
109
+ // The builders only reference `Locale` / `buildI18nPayload` /
110
+ // `emptyLocaleMap` for translatable fields. When the schema has no
111
+ // translatable field, those imports would be unused trip TS6133
112
+ // (declared but never read) under noUnusedLocals. Pilot #103 Issue 2
113
+ // — emit imports conditionally on actual i18n usage.
114
+ const hasTranslatableField = formShape.fields.some(f => f.translatable);
115
+ if (hasTranslatableField) {
116
+ insertPayloadImports(parts);
117
+ }
113
118
  parts.push(formatPayloadBuilderSection(formShape, options.defaultLocale));
114
119
  }
115
120
  return {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@omnifyjp/ts",
3
- "version": "5.8.32",
3
+ "version": "5.8.34",
4
4
  "description": "TypeScript model type generator from Omnify schemas.json",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",