Skip to content

Commit

Permalink
refactor(codegen): simplify return value hints (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonambas authored Apr 7, 2024
1 parent dc1734f commit 52c6c53
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 6 deletions.
6 changes: 3 additions & 3 deletions src/__tests__/codegen.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: <T extends keyof typeof pluginCtMap>(alias: T) => typeof pluginCtMap[T];",
export const ct: <T extends keyof PluginCtMapType>(alias: T) => PluginCtMapType[T];",
"file": "ct.d.ts",
},
],
Expand Down
2 changes: 1 addition & 1 deletion src/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export const codegen = (
const ctDtsFile: ArtifactContent = {
file: 'ct.d.ts',
code: `${serializeMapTypes(map)}
\nexport const ct: <T extends keyof typeof pluginCtMap>(alias: T) => typeof pluginCtMap[T];`,
\nexport const ct: <T extends keyof PluginCtMapType>(alias: T) => PluginCtMapType[T];`,
};

cssFn.files.push(ctFile, ctDtsFile);
Expand Down
4 changes: 2 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ export const serializeValue = (value: any) => {
};

export const serializeMapTypes = (map: Map<any, any>) => {
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;
};

0 comments on commit 52c6c53

Please sign in to comment.