Skip to content

Commit dfc8cb2

Browse files
committed
chore: wip
1 parent 3420eb0 commit dfc8cb2

File tree

2 files changed

+49
-22
lines changed

2 files changed

+49
-22
lines changed

fixtures/output/variable.d.ts

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,34 @@ export declare const someObject: {
1111
anotherOne: () => unknown;
1212
someArray: Array<1 | 2 | 3>;
1313
someNestedArray: Array<Array<1 | 2 | 3> | Array<4 | 5 | 6 | 7 | 8 | 9 | 10>>;
14-
someNestedArray2: Array<Array<1 | 2 | 3> | Array<4 | 5 | 6 | 7 | 8 | 9 | 10> | 'dummy value'>;
15-
someNestedArray3: Array<Array<1 | 2 | 3> | Array<4 | 5 | 6 | 7 | 8 | 9 | 10> | 'dummy value' | Array<11 | 12 | 13>>;
14+
someNestedArray2: Array<
15+
Array<1 | 2 | 3>
16+
Array<4 | 5 | 6 | 7 | 8 | 9 | 10>
17+
'dummy value'
18+
>;
19+
someNestedArray3: Array<
20+
Array<1 | 2 | 3>
21+
Array<4 | 5 | 6 | 7 | 8 | 9 | 10>
22+
'dummy value'
23+
Array<11 | 12 | 13>
24+
>;
1625
someOtherNestedArray: Array<
17-
Array<'some text' | 2 | unknown | (() => void) | unknown> |
26+
Array<'some text' | 2 | unknown | (() => void) | unknown>
1827
Array<4 | 5 | 6 | 7 | 8 | 9 | 10>
1928
>;
2029
someComplexArray: Array<
2130
Array<
22-
{
23-
key: 'value'
24-
}
25-
> |
31+
{
32+
key: 'value'
33+
}
34+
>
2635
Array<
27-
{
28-
key2: 'value2'
29-
} |
30-
'test' |
31-
1000
32-
> |
36+
{
37+
key2: 'value2'
38+
}
39+
'test'
40+
1000
41+
>
3342
Array<'some string' | unknown | unknown>
3443
>;
3544
someObject: {
@@ -46,11 +55,11 @@ export declare const someObject: {
4655
};
4756
someNestedObjectArray: Array<
4857
{
49-
key: 'value'
50-
} |
58+
key: 'value'
59+
}
5160
{
52-
key2: 'value2'
53-
}
61+
key2: 'value2'
62+
}
5463
>;
5564
someOtherObject: unknown;
5665
someInlineCall2: unknown;
@@ -63,12 +72,17 @@ export declare const defaultHeaders: {
6372
declare const dtsConfig: DtsGenerationConfig;
6473
export declare const complexArrays: {
6574
matrix: Array<
66-
Array<1 | 2 | Array<3 | 4 | Array<5 | 6>>> |
67-
Array<'a' | 'b' | Array<'c' | 'd'>> |
75+
Array<1 | 2 | Array<3 | 4 | Array<5 | 6>>>
76+
Array<'a' | 'b' | Array<'c' | 'd'>>
6877
Array<true | Array<false | Array<true>>>
6978
>;
7079
tuples: Array<readonly [1, 'string', true] | readonly ['literal', 42, false]>;
71-
mixedArrays: Array<unknown | unknown | ((...args: any[]) => unknown) | ((...args: any[]) => unknown)>
80+
mixedArrays: Array<
81+
unknown
82+
unknown
83+
((...args: any[]) => unknown)
84+
((...args: any[]) => unknown)
85+
>
7286
};
7387
export declare const complexObject: {
7488
handlers: {

src/extract.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -619,11 +619,24 @@ function inferArrayType(value: string, state?: ProcessingState, indentLevel = 0)
619619
const needsMultiline = types.some(type =>
620620
type.includes('\n')
621621
|| type.includes('{')
622-
|| type.length > 40,
622+
|| type.length > 40
623+
|| types.join(' | ').length > 60,
623624
)
624625

625626
if (needsMultiline) {
626-
return `Array<\n${elementIndent}${types.join(` |\n${elementIndent}`)}\n${baseIndent}>`
627+
const formattedTypes = types.map((type) => {
628+
// Indent nested types that have newlines
629+
if (type.includes('\n')) {
630+
return type.replace(/\n/g, `\n${elementIndent}`)
631+
}
632+
return type
633+
})
634+
635+
return [
636+
'Array<',
637+
...formattedTypes.map(type => `${elementIndent}${type}`),
638+
`${baseIndent}>`,
639+
].join('\n')
627640
}
628641

629642
return `Array<${types.join(' | ')}>`

0 commit comments

Comments
 (0)