From 52c6c5323db6ac21b377e9afd0e95f0f21b7b472 Mon Sep 17 00:00:00 2001 From: Jon Ambas Date: Sun, 7 Apr 2024 13:00:46 -0400 Subject: [PATCH] refactor(codegen): simplify return value hints (#17) --- src/__tests__/codegen.test.ts | 6 +++--- src/codegen.ts | 2 +- src/utils.ts | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/__tests__/codegen.test.ts b/src/__tests__/codegen.test.ts index b899ad4..c998cd6 100644 --- a/src/__tests__/codegen.test.ts +++ b/src/__tests__/codegen.test.ts @@ -37,14 +37,14 @@ describe('codegen', () => { "file": "ct.mjs", }, { - "code": "const pluginCtMap = { + "code": "type PluginCtMapType = { 'foo.100': '#fff', 'foo.200': {"base":"#000","lg":"#111"}, 'bar.100': 'red', 'bar.200': 'blue', - } as const; + } - export const ct: (alias: T) => typeof pluginCtMap[T];", + export const ct: (alias: T) => PluginCtMapType[T];", "file": "ct.d.ts", }, ], diff --git a/src/codegen.ts b/src/codegen.ts index 3326581..2985c64 100644 --- a/src/codegen.ts +++ b/src/codegen.ts @@ -32,7 +32,7 @@ export const codegen = ( const ctDtsFile: ArtifactContent = { file: 'ct.d.ts', code: `${serializeMapTypes(map)} - \nexport const ct: (alias: T) => typeof pluginCtMap[T];`, + \nexport const ct: (alias: T) => PluginCtMapType[T];`, }; cssFn.files.push(ctFile, ctDtsFile); diff --git a/src/utils.ts b/src/utils.ts index d482416..a8070bc 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -15,10 +15,10 @@ export const serializeValue = (value: any) => { }; export const serializeMapTypes = (map: Map) => { - let code = 'const pluginCtMap = {'; + let code = 'type PluginCtMapType = {'; for (const [key, value] of map.entries()) { code += `\n '${key}': ${serializeValue(value)},`; } - code += '\n} as const;'; + code += '\n}'; return code; };