Skip to content

Commit 070dc24

Browse files
committed
fix(generator): emit valid default declarations
1 parent bcca75a commit 070dc24

2 files changed

Lines changed: 27 additions & 2 deletions

File tree

packages/dtsx/src/extractor/scanner.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1930,7 +1930,8 @@ function scanDeclarationsInternal(_source: string, _filename: string, _keepComme
19301930
const exportPrefix = isExported
19311931
? (isDefault ? 'export default function ' : 'export declare function ')
19321932
: 'declare function '
1933-
const text = `${exportPrefix}${name || 'default'}${generics}${dtsParams}: ${returnType};`
1933+
const declarationName = name || (isDefault ? '' : 'default')
1934+
const text = `${exportPrefix}${declarationName}${generics}${dtsParams}: ${returnType};`
19341935
const comments = extractLeadingComments(declStart)
19351936

19361937
// Record index if this function had a body (implementation signature for overloads)
@@ -3278,7 +3279,8 @@ function scanDeclarationsInternal(_source: string, _filename: string, _keepComme
32783279
let helperName = '__dtsx_default_export__'
32793280
let suffix = 1
32803281
while (source.includes(helperName)) helperName = `__dtsx_default_export_${suffix++}__`
3281-
const inferredType = inferNarrowType(expression, false)
3282+
const assertedType = extractAssertion(expression)
3283+
const inferredType = assertedType || inferNarrowType(expression, false)
32823284
const type = inferredType === 'unknown' && isEntityNameText(expression)
32833285
? `typeof ${expression}`
32843286
: inferredType

packages/dtsx/test/generation-paths.test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,29 @@ describe('declaration generation paths', () => {
273273
expect(result).not.toContain('export default {')
274274
})
275275

276+
it('uses the contract from asserted default-export expressions', () => {
277+
const source = `
278+
type ColorSpecifier = string | null
279+
declare function hcl(): unknown
280+
export default hcl() as (start: ColorSpecifier, end: ColorSpecifier) => (t: number) => string
281+
`
282+
283+
const result = processSourceIsolated(source, 'asserted-default.ts', false)
284+
285+
expect(result).toContain('declare const __dtsx_default_export__: (start: ColorSpecifier, end: ColorSpecifier) => (t: number) => string;')
286+
expect(result).toContain('export default __dtsx_default_export__;')
287+
expect(result).not.toContain('hcl() as')
288+
})
289+
290+
it('emits valid anonymous default function declarations', () => {
291+
const source = 'export default function (name: string): string { return name }'
292+
293+
const result = processSourceIsolated(source, 'anonymous-default.ts', false)
294+
295+
expect(result).toContain('export default function (name: string): string;')
296+
expect(result).not.toContain('function default')
297+
})
298+
276299
it('keeps result caches separate across import orders', () => {
277300
clearResultCache()
278301
const source = `

0 commit comments

Comments
 (0)