Skip to content

Commit 09075fd

Browse files
committed
fix(complex-types): correctly handle arrays
1 parent 5306f06 commit 09075fd

File tree

8 files changed

+957
-509
lines changed

8 files changed

+957
-509
lines changed

package.json

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"private": true,
33
"version": "1.0.0-beta.3",
4-
"packageManager": "pnpm@10.13.1",
4+
"packageManager": "pnpm@10.14.0",
55
"type": "module",
66
"scripts": {
77
"build": "pnpm -r run build",
@@ -14,19 +14,19 @@
1414
},
1515
"devDependencies": {
1616
"@antfu/ni": "^25.0.0",
17-
"@so1ve/eslint-config": "^3.7.0",
18-
"@so1ve/prettier-config": "^3.7.0",
19-
"@types/node": "^24.0.13",
17+
"@so1ve/eslint-config": "^3.8.0",
18+
"@so1ve/prettier-config": "^3.8.0",
19+
"@types/node": "^24.1.0",
2020
"@typescript/native-preview": "7.0.0-dev.20250711.1",
2121
"@vue-macros/test-utils": "^2.0.0",
22-
"bumpp": "^10.2.0",
23-
"eslint": "^9.30.1",
22+
"bumpp": "^10.2.2",
23+
"eslint": "^9.32.0",
2424
"prettier": "^3.6.2",
25-
"tsdown": "^0.12.9",
26-
"typescript": "^5.8.3",
27-
"vite": "^7.0.4",
25+
"tsdown": "^0.13.2",
26+
"typescript": "^5.9.2",
27+
"vite": "^7.0.6",
2828
"vite-tsconfig-paths": "^5.1.4",
2929
"vitest": "^3.2.4",
30-
"vue": "^3.5.17"
30+
"vue": "^3.5.18"
3131
}
3232
}

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

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,15 @@ export class Printer {
3131
: !!(type.flags & ts.TypeFlags.Undefined);
3232
}
3333

34+
private printConditionTypeNode(type: ts.ConditionalType) {
35+
const decl = type.root.node;
36+
const { trueType, falseType } = decl;
37+
const trueTypeNode = this.checker.getTypeAtLocation(trueType);
38+
const falseTypeNode = this.checker.getTypeAtLocation(falseType);
39+
40+
return `${this.printType(trueTypeNode)} | ${this.printType(falseTypeNode)}`;
41+
}
42+
3443
private printPrimitiveType(type: ts.Type): string {
3544
if (type.flags & ts.TypeFlags.BooleanLiteral) {
3645
return "boolean";
@@ -57,9 +66,8 @@ export class Printer {
5766
} else if (type.isIntersection()) {
5867
return this.printUnionOrIntersection(type, " & ", inner);
5968
}
60-
// WIP
61-
if (this.typeToString(type).endsWith("[]")) {
62-
return this.typeToString(type);
69+
if (this.checker.isArrayType(type)) {
70+
return "Array";
6371
} else if (type.flags & ts.TypeFlags.Object) {
6472
const decl = type.getSymbol()?.declarations?.[0];
6573
if (decl && ts.isFunctionTypeNode(decl)) {
@@ -81,7 +89,7 @@ export class Printer {
8189
for (const prop of properties) {
8290
const propType = this.checker.getTypeOfSymbol(prop);
8391
props[prop.getName()] = {
84-
value: this.printType(this.checker.getTypeOfSymbol(prop), true),
92+
value: this.printType(propType, true),
8593
isOptional: this.containsUndefined(propType),
8694
};
8795
}
@@ -94,20 +102,20 @@ export class Printer {
94102

95103
return Object.keys(props).length > 0 ? `{\n${parts.join("\n")}\n}` : "";
96104
} else if (
97-
type.isLiteral()
98-
|| type.flags & ts.TypeFlags.BooleanLiteral
99-
|| type.flags & ts.TypeFlags.String
100-
|| type.flags & ts.TypeFlags.Number
101-
|| type.flags & ts.TypeFlags.BigInt
102-
|| type.flags & ts.TypeFlags.Any
103-
|| type.flags & ts.TypeFlags.Unknown
104-
|| type.flags & ts.TypeFlags.Null
105+
type.isLiteral() ||
106+
type.flags & ts.TypeFlags.BooleanLiteral ||
107+
type.flags & ts.TypeFlags.String ||
108+
type.flags & ts.TypeFlags.Number ||
109+
type.flags & ts.TypeFlags.BigInt ||
110+
type.flags & ts.TypeFlags.Any ||
111+
type.flags & ts.TypeFlags.Unknown ||
112+
type.flags & ts.TypeFlags.Null
105113
) {
106114
return this.printPrimitiveType(type);
107115
} else if (type.flags & ts.TypeFlags.Undefined) {
108116
return "";
109117
} else if (type.flags & ts.TypeFlags.Conditional) {
110-
return `${this.printType((type as any).resolvedTrueType)} | ${this.printType((type as any).resolvedFalseType)}`;
118+
return this.printConditionTypeNode(type as ts.ConditionalType);
111119
} else if (type.isTypeParameter()) {
112120
const symbol = type.getSymbol();
113121
const decl = symbol?.declarations?.[0];

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ defineProps<
1818
stringUnion: "a" | "b";
1919
genericExtendsString: T;
2020
array: string[];
21+
customTypeArray: SomeEnum[];
2122
genericArray: U;
2223
genericCondition: I extends "a" ? "a" : 1;
2324
function: () => void;

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@ var _sfc_main = /* @__PURE__ */ defineComponent({
120120
type: Array,
121121
required: true
122122
},
123+
customTypeArray: {
124+
type: Array,
125+
required: true
126+
},
123127
genericArray: {
124128
type: Array,
125129
required: true
@@ -247,7 +251,14 @@ var _sfc_main = /* @__PURE__ */ defineComponent({
247251
type: String,
248252
required: false
249253
},
250-
value: { required: false },
254+
value: {
255+
type: [
256+
String,
257+
Number,
258+
Array
259+
],
260+
required: false
261+
},
251262
innerHTML: {
252263
type: String,
253264
required: false

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,9 @@ defineProps<
5353
"optionalString"?: string,
5454
"stringUnion": string,
5555
"genericExtendsString": string,
56-
"array": string[],
57-
"genericArray": (string | number)[],
56+
"array": Array,
57+
"customTypeArray": Array,
58+
"genericArray": Array,
5859
"genericCondition": string | number,
5960
"function": Function,
6061
"error": object,
@@ -96,10 +97,10 @@ defineProps<{
9697
"formtarget"?: string,
9798
"name"?: string,
9899
"type"?: string,
99-
"value"?: string | number | readonly string[],
100+
"value"?: string | number | Array,
100101
"innerHTML"?: string,
101102
"class": any,
102-
"style"?: null | string | boolean | object | StyleValue[],
103+
"style"?: null | string | boolean | object | Array,
103104
"accesskey"?: string,
104105
"contenteditable"?: boolean | string,
105106
"contextmenu"?: string,

packages/language/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
"watch": "tsdown --watch"
3939
},
4040
"dependencies": {
41-
"@volar/typescript": "~2.4.18",
41+
"@volar/typescript": "~2.4.22",
4242
"@vue.ts/shared": "workspace:*",
4343
"@vue/language-core": "3.0.1"
4444
}

0 commit comments

Comments
 (0)