Skip to content

Commit bfee39d

Browse files
committed
chore: wip
1 parent 46c9738 commit bfee39d

2 files changed

Lines changed: 33 additions & 20 deletions

File tree

fixtures/output/variable.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,5 @@ export declare const defaultHeaders: {
4747
declare const dtsConfig: DtsGenerationConfig;
4848
export declare const complexArrays: {
4949
matrix: Array<Array<1 | 2 | Array<3 | 4 | Array<5 | 6>>> | Array<'a' | 'b' | Array<'c' | 'd'>> | Array<true | Array<false | Array<true>>>>;
50-
tuples: readonly [1, 'string', true]
50+
tuples: Array<readonly [1, 'string', true] | readonly ['literal', 42, false]>
5151
};

src/extract.ts

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -388,13 +388,24 @@ function inferArrayType(value: string, state?: ProcessingState): string {
388388
if (!content)
389389
return 'unknown[]'
390390

391-
// Handle 'as const' arrays
391+
// Check if all elements are const tuples
392+
const elements = splitArrayElements(content, state)
393+
const allConstTuples = elements.every(el => el.trim().endsWith('as const'))
394+
395+
if (allConstTuples) {
396+
const tuples = elements.map((el) => {
397+
const tupleContent = el.slice(0, el.indexOf('as const')).trim()
398+
return inferConstArrayType(tupleContent, state)
399+
})
400+
return `Array<${tuples.join(' | ')}>`
401+
}
402+
403+
// Handle individual const assertions
392404
if (content.includes('as const')) {
393-
const beforeConst = content.split('as const')[0].trim()
405+
const beforeConst = content.slice(0, content.indexOf('as const')).trim()
394406
return inferConstArrayType(beforeConst, state)
395407
}
396408

397-
const elements = splitArrayElements(content, state)
398409
const types = elements.map((element) => {
399410
const trimmed = element.trim()
400411

@@ -449,7 +460,7 @@ function inferComplexObjectType(value: string, state?: ProcessingState): string
449460
function inferConstArrayType(value: string, state?: ProcessingState): string {
450461
debugLog(state, 'infer-const', `Inferring const array type for: ${value}`)
451462

452-
// Handle nested array with const assertion
463+
// Handle array literals
453464
if (value.startsWith('[')) {
454465
const content = value.slice(1, -1).trim()
455466
const elements = splitArrayElements(content, state)
@@ -1085,17 +1096,22 @@ function processProperty(key: string, value: string, state?: ProcessingState): {
10851096
const cleanKey = key.replace(/^['"`]|['"`]$/g, '')
10861097
const cleanValue = value.trim()
10871098

1088-
// Handle 'as const' arrays
1089-
if (cleanValue.endsWith('as const')) {
1090-
const constValue = cleanValue.slice(0, -8).trim()
1091-
return {
1092-
key: cleanKey,
1093-
value: inferConstArrayType(constValue, state),
1099+
// Handle arrays with const assertions
1100+
if (cleanValue.startsWith('[')) {
1101+
// Multiple const tuples
1102+
if (cleanValue.includes('as const')) {
1103+
const arrayElements = splitArrayElements(cleanValue.slice(1, -1), state)
1104+
const isAllConst = arrayElements.every(el => el.trim().endsWith('as const'))
1105+
1106+
if (isAllConst) {
1107+
const tuples = arrayElements.map((el) => {
1108+
const tupleContent = el.slice(0, el.indexOf('as const')).trim()
1109+
return inferConstArrayType(tupleContent, state)
1110+
})
1111+
return { key: cleanKey, value: `Array<${tuples.join(' | ')}>` }
1112+
}
10941113
}
1095-
}
10961114

1097-
// Handle arrays
1098-
if (cleanValue.startsWith('[')) {
10991115
return {
11001116
key: cleanKey,
11011117
value: inferArrayType(cleanValue, state),
@@ -1119,15 +1135,12 @@ function processProperty(key: string, value: string, state?: ProcessingState): {
11191135
}
11201136

11211137
// Handle literals
1122-
if (/^['"`].*['"`]$/.test(cleanValue)) {
1138+
if (/^['"`].*['"`]$/.test(cleanValue))
11231139
return { key: cleanKey, value: cleanValue }
1124-
}
1125-
if (!Number.isNaN(Number(cleanValue))) {
1140+
if (!Number.isNaN(Number(cleanValue)))
11261141
return { key: cleanKey, value: cleanValue }
1127-
}
1128-
if (cleanValue === 'true' || cleanValue === 'false') {
1142+
if (cleanValue === 'true' || cleanValue === 'false')
11291143
return { key: cleanKey, value: cleanValue }
1130-
}
11311144

11321145
// Handle other expressions
11331146
return { key: cleanKey, value: 'unknown' }

0 commit comments

Comments
 (0)