diff --git a/internal/checker/nodebuilderimpl.go b/internal/checker/nodebuilderimpl.go index 21cc1b67b6..a6dcdb9781 100644 --- a/internal/checker/nodebuilderimpl.go +++ b/internal/checker/nodebuilderimpl.go @@ -411,6 +411,13 @@ func (b *nodeBuilderImpl) tryReuseExistingNonParameterTypeNode(existing *ast.Typ if annotationType == nil { annotationType = b.getTypeFromTypeNode(existing, true) } + if annotationType != nil && b.ch.isErrorType(annotationType) { + // allow "reusing" type nodes that resolve to error types + // those can't truly be reused but it prevents cascading errors in isolatedDeclarations + // for source with errors there is no guarantee to emit correct code anyway + // Since tryReuseExistingTypeNodeHelper is not yet implemented, directly return the existing node + return existing + } if annotationType != nil && b.typeNodeIsEquivalentToType(host, t, annotationType) && b.existingTypeNodeIsNotReferenceOrIsReferenceWithCompatibleTypeArgumentCount(existing, t) { result := b.tryReuseExistingTypeNodeHelper(existing) if result != nil { @@ -2006,6 +2013,15 @@ func (b *nodeBuilderImpl) serializeTypeForDeclaration(declaration *ast.Declarati t = b.ch.getOptionalType(t, false) } + // Try to reuse existing type annotation if present + if declaration != nil && declaration.Type() != nil { + typeAnnotation := declaration.Type() + reused := b.tryReuseExistingTypeNode(typeAnnotation, t, declaration, addUndefinedForParameter) + if reused != nil { + return reused + } + } + restoreFlags := b.saveRestoreFlags() if t.flags&TypeFlagsUniqueESSymbol != 0 && t.symbol == symbol && (b.ctx.enclosingDeclaration == nil || core.Some(symbol.Declarations, func(d *ast.Declaration) bool { return ast.GetSourceFileOfNode(d) == b.ctx.enclosingFile diff --git a/testdata/baselines/reference/submodule/compiler/isolatedDeclarationErrorTypes1.js.diff b/testdata/baselines/reference/submodule/compiler/isolatedDeclarationErrorTypes1.js.diff deleted file mode 100644 index bec6aaca54..0000000000 --- a/testdata/baselines/reference/submodule/compiler/isolatedDeclarationErrorTypes1.js.diff +++ /dev/null @@ -1,20 +0,0 @@ ---- old.isolatedDeclarationErrorTypes1.js -+++ new.isolatedDeclarationErrorTypes1.js -@@= skipped -11, +11 lines =@@ - - //// [isolatedDeclarationErrorTypes1.js] - "use strict"; --// https://github.com/microsoft/TypeScript/issues/60192 - Object.defineProperty(exports, "__esModule", { value: true }); - exports.foo3 = exports.foo2 = exports.foo1 = void 0; - const foo1 = (type) => { }; -@@= skipped -12, +11 lines =@@ - - - //// [isolatedDeclarationErrorTypes1.d.ts] --import { Unresolved } from "foo"; --export declare const foo1: (type?: Unresolved) => void; --export declare const foo2: (type?: Unresolved | undefined) => void; -+export declare const foo1: (type?: any) => void; -+export declare const foo2: (type?: any) => void; - export declare const foo3: (type: Unresolved) => void; \ No newline at end of file diff --git a/testdata/baselines/reference/submodule/compiler/isolatedDeclarationErrorTypes1.types.diff b/testdata/baselines/reference/submodule/compiler/isolatedDeclarationErrorTypes1.types.diff deleted file mode 100644 index f3d9de6296..0000000000 --- a/testdata/baselines/reference/submodule/compiler/isolatedDeclarationErrorTypes1.types.diff +++ /dev/null @@ -1,20 +0,0 @@ ---- old.isolatedDeclarationErrorTypes1.types -+++ new.isolatedDeclarationErrorTypes1.types -@@= skipped -6, +6 lines =@@ - >Unresolved : any - - export const foo1 = (type?: Unresolved): void => {}; -->foo1 : (type?: Unresolved) => void -->(type?: Unresolved): void => {} : (type?: Unresolved) => void -+>foo1 : (type?: any) => void -+>(type?: Unresolved): void => {} : (type?: any) => void - >type : any - - export const foo2 = (type?: Unresolved | undefined): void => {}; -->foo2 : (type?: Unresolved | undefined) => void -->(type?: Unresolved | undefined): void => {} : (type?: Unresolved | undefined) => void -+>foo2 : (type?: any) => void -+>(type?: Unresolved | undefined): void => {} : (type?: any) => void - >type : any - - export const foo3 = (type: Unresolved): void => {}; \ No newline at end of file