Skip to content

Commit 0441ff5

Browse files
committed
fix(complex-types): handle optional properties correctly
1 parent ef7f19a commit 0441ff5

File tree

4 files changed

+16
-12
lines changed

4 files changed

+16
-12
lines changed

packages/complex-types/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
"@nuxt/kit": "catalog:",
7171
"@vue.ts/language": "workspace:*",
7272
"@vue.ts/shared": "workspace:*",
73+
"@vue/shared": "^3.5.22",
7374
"defu": "^6.1.4",
7475
"magic-string": "catalog:",
7576
"unplugin": "catalog:"

packages/complex-types/src/core/printer.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,8 @@ export class Printer {
2525
].join(separator);
2626
}
2727

28-
private containsUndefined(type: ts.Type): boolean {
29-
return type.isUnion()
30-
? type.types.some((t) => t.flags & ts.TypeFlags.Undefined)
31-
: !!(type.flags & ts.TypeFlags.Undefined);
28+
private isSymbolOptional(symbol: ts.Symbol): boolean {
29+
return !!(symbol.flags & ts.SymbolFlags.Optional);
3230
}
3331

3432
private printConditionType(type: ts.ConditionalType, inner: boolean): string {
@@ -88,9 +86,16 @@ export class Printer {
8886
> = {};
8987
for (const prop of properties) {
9088
const propType = this.checker.getTypeOfSymbol(prop);
89+
const node = prop.declarations?.[0];
90+
const hasQuestionToken = !!(
91+
node &&
92+
ts.isPropertySignature(node) &&
93+
node.questionToken
94+
);
95+
9196
props[prop.getName()] = {
9297
value: this.printType(propType, true),
93-
isOptional: this.containsUndefined(propType),
98+
isOptional: this.isSymbolOptional(prop),
9499
};
95100
}
96101

packages/complex-types/test/__fixtures__/defineProps/basic.vue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ defineProps<
2929
symbol: symbol;
3030
'quotes"': string;
3131
enum: SomeEnum;
32+
anyQuestion?: any;
3233
} & SomeInterface & {
3334
[MappedString in "1-1" | "1-2"]: `${MappedString}MappedType`;
3435
} & {

pnpm-lock.yaml

Lines changed: 4 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)