Skip to content

Commit dc96d16

Browse files
Port TypeScript PR #60195: Allow reusing error type annotations
This change allows type node annotations that resolve to error types to be reused in isolated declarations mode. This prevents cascading errors when source code contains unresolved type references. The fix adds: 1. A check in tryReuseExistingNonParameterTypeNode to detect error types and allow reuse 2. Logic in serializeTypeForDeclaration to attempt reusing existing type annotations before generating new ones This matches the behavior from microsoft/TypeScript#60195 which fixes issue #60192. Co-authored-by: RyanCavanaugh <[email protected]>
1 parent 75b59bc commit dc96d16

File tree

3 files changed

+16
-40
lines changed

3 files changed

+16
-40
lines changed

internal/checker/nodebuilderimpl.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,13 @@ func (b *nodeBuilderImpl) tryReuseExistingNonParameterTypeNode(existing *ast.Typ
411411
if annotationType == nil {
412412
annotationType = b.getTypeFromTypeNode(existing, true)
413413
}
414+
if annotationType != nil && b.ch.isErrorType(annotationType) {
415+
// allow "reusing" type nodes that resolve to error types
416+
// those can't truly be reused but it prevents cascading errors in isolatedDeclarations
417+
// for source with errors there is no guarantee to emit correct code anyway
418+
// Since tryReuseExistingTypeNodeHelper is not yet implemented, directly return the existing node
419+
return existing
420+
}
414421
if annotationType != nil && b.typeNodeIsEquivalentToType(host, t, annotationType) && b.existingTypeNodeIsNotReferenceOrIsReferenceWithCompatibleTypeArgumentCount(existing, t) {
415422
result := b.tryReuseExistingTypeNodeHelper(existing)
416423
if result != nil {
@@ -2006,6 +2013,15 @@ func (b *nodeBuilderImpl) serializeTypeForDeclaration(declaration *ast.Declarati
20062013
t = b.ch.getOptionalType(t, false)
20072014
}
20082015

2016+
// Try to reuse existing type annotation if present
2017+
if declaration != nil && declaration.Type() != nil {
2018+
typeAnnotation := declaration.Type()
2019+
reused := b.tryReuseExistingTypeNode(typeAnnotation, t, declaration, addUndefinedForParameter)
2020+
if reused != nil {
2021+
return reused
2022+
}
2023+
}
2024+
20092025
restoreFlags := b.saveRestoreFlags()
20102026
if t.flags&TypeFlagsUniqueESSymbol != 0 && t.symbol == symbol && (b.ctx.enclosingDeclaration == nil || core.Some(symbol.Declarations, func(d *ast.Declaration) bool {
20112027
return ast.GetSourceFileOfNode(d) == b.ctx.enclosingFile

testdata/baselines/reference/submodule/compiler/isolatedDeclarationErrorTypes1.js.diff

Lines changed: 0 additions & 20 deletions
This file was deleted.

testdata/baselines/reference/submodule/compiler/isolatedDeclarationErrorTypes1.types.diff

Lines changed: 0 additions & 20 deletions
This file was deleted.

0 commit comments

Comments
 (0)