Skip to content

Commit

Permalink
Add support for Record<T> in tspd
Browse files Browse the repository at this point in the history
  • Loading branch information
timotheeguerin committed Oct 14, 2024
1 parent ab6d951 commit d36cd77
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,10 @@ export function generateSignatures(
} else if (isReflectionType(type)) {
return getValueOfReflectionType(type);
} else {
// If its exactly the record type use Record<string, T> instead of the model name.
if (type.indexer && type.name === "Record" && type.namespace?.name === "TypeSpec") {
return `Record<string, ${getValueTSType(type.indexer.value)}>`;
}
if (type.name) {
return useLocalType(type);
} else {
Expand All @@ -248,6 +252,9 @@ export function generateSignatures(
const properties = [...model.properties.values()].map((x) => {
return `readonly ${x.name}${x.optional ? "?" : ""}: ${getValueTSType(x.type)}`;
});
if (model.indexer?.value) {
properties.unshift(`readonly [key: string]: ${getValueTSType(model.indexer.value)}`);
}

return `{ ${properties.join(", ")} }`;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,11 @@ export type Decorators = {
[`valueof "abc" | "def" | string`, `"abc" | "def" | string`],
[`valueof string[]`, `readonly string[]`],
[`valueof ("abc" | "def")[]`, `readonly ("abc" | "def")[]`],
[`valueof Record<int32>`, `Record<string, number>`],
[
`valueof {...Record<int32>, other: string}`,
`{ readonly [key: string]: number; readonly other: string }`,
],
[`valueof {name: string, age?: int32}`, `{ readonly name: string; readonly age?: number }`],
])("%s => %s", async (ref, expected) => {
await expectSignatures({
Expand Down

0 comments on commit d36cd77

Please sign in to comment.