Skip to content

Commit

Permalink
fix: handle questionToken for MappedType
Browse files Browse the repository at this point in the history
  • Loading branch information
so1ve committed Oct 31, 2023
1 parent fdc2230 commit d70ed8f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/core/printer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@ export class Printer {
parts.push(
[
member.name.getText(),
member.questionToken ? "?" : "",
member.questionToken?.getText(),
": ",
stringBaseType,
].join(""),
]
.filter(Boolean)
.join(""),
);
}
}
Expand All @@ -52,14 +54,19 @@ export class Printer {
const type = this.checker.getTypeAtLocation(node);
const properties = type.getProperties();
const parts = ["{"];

let questionToken = "";
if (ts.isMappedTypeNode(node)) {
questionToken = node.questionToken?.getText() ?? "";
}
for (const property of properties) {
const valueType = this.checker.getTypeOfSymbol(property);
const stringValueType = this.checker.typeToString(
this.getBaseType(valueType),
);
parts.push(
`${this.checker.symbolToString(property)}: ${stringValueType}`,
`${this.checker.symbolToString(
property,
)}${questionToken}: ${stringValueType}`,
);
}

Expand Down
2 changes: 2 additions & 0 deletions test/__fixtures__/basic.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ defineProps<
genericCondition: I extends "a" ? "a" : 1;
} & SomeInterface & {
[MappedString in "1-1" | "2-1"]: `${MappedString}MappedType`;
} & {
[MappedString in "1-1" | "2-1"]?: `${MappedString}OptionalMappedType`;
}
>();
</script>
3 changes: 3 additions & 0 deletions test/__snapshots__/fixtures.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ typeLiteral: TypeLiteral
} & {
\\"1-1\\": string
\\"2-1\\": string
} & {
\\"1-1\\"?: \\"1-1OptionalMappedType\\" | undefined
\\"2-1\\"?: \\"2-1OptionalMappedType\\" | undefined
}
>();
</script>
Expand Down

0 comments on commit d70ed8f

Please sign in to comment.