Skip to content

Commit e1867a4

Browse files
committed
fix(complex-types): early calculate condition types
1 parent 5bfca17 commit e1867a4

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,21 @@ export class Printer {
281281
}
282282

283283
private printConditionalTypeNode(node: ts.ConditionalTypeNode): string {
284+
// Try to evaluate the conditional type using the type checker
285+
const conditionalType = this.checker.getTypeAtLocation(node);
286+
287+
if (
288+
conditionalType &&
289+
!(conditionalType.flags & ts.TypeFlags.Conditional)
290+
) {
291+
const resolvedTypeString = this.typeToString(conditionalType);
292+
293+
if (resolvedTypeString !== node.getText()) {
294+
return resolvedTypeString;
295+
}
296+
}
297+
298+
// If we can't resolve it or it's still conditional, fall back to the original logic
284299
const checkType = this.print(node.checkType);
285300
const extendsType = this.print(node.extendsType);
286301
const trueType = this.print(node.trueType);

packages/complex-types/test/__snapshots__/fixtures-compiled.test.ts.snap

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,10 @@ var _sfc_main = /* @__PURE__ */ defineComponent({
124124
},
125125
error: { required: true },
126126
emptyInterface: { required: true },
127-
conditionalNumber: { required: true },
127+
conditionalNumber: {
128+
type: Number,
129+
required: true
130+
},
128131
interface: {
129132
type: Object,
130133
required: true

packages/complex-types/test/__snapshots__/fixtures.test.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ error: Error
5454
} & {
5555
emptyInterface: {
5656
}
57-
conditionalNumber: number extends boolean ? string : number
57+
conditionalNumber: number
5858
interface: {
5959
aaa: 1
6060
}

0 commit comments

Comments
 (0)