diff --git a/tsc/internal/transformers/declarations/transform.go b/tsc/internal/transformers/declarations/transform.go index 8b5b449ec87f5..9b25fbe2a9501 100644 --- a/tsc/internal/transformers/declarations/transform.go +++ b/tsc/internal/transformers/declarations/transform.go @@ -672,6 +672,14 @@ func (tx *DeclarationTransformer) visitDeclarationSubtree(input *ast.Node) *ast. tx.EmitContext().AddEmitFlags(result, printer.EFSingleLine) } } + case ast.KindParenthesizedType: + result = tx.transformParenthesizedType(input.AsParenthesizedTypeNode()) + case ast.KindTypeOperator: + result = tx.transformTypeOperator(input.AsTypeOperatorNode()) + case ast.KindArrayType: + result = tx.transformArrayType(input.AsArrayTypeNode()) + case ast.KindIndexedAccessType: + result = tx.transformIndexedAccessType(input.AsIndexedAccessTypeNode()) case ast.KindJSDocTypeExpression: result = tx.transformJSDocTypeExpression(input.AsJSDocTypeExpression()) case ast.KindJSDocTypeLiteral: @@ -807,6 +815,39 @@ func (tx *DeclarationTransformer) transformConditionalTypeNode(input *ast.Condit ) } +func (tx *DeclarationTransformer) transformParenthesizedType(input *ast.ParenthesizedTypeNode) *ast.Node { + t := tx.Visitor().Visit(input.Type) + if t == nil { + return nil + } + return tx.Factory().UpdateParenthesizedTypeNode(input, t) +} + +func (tx *DeclarationTransformer) transformTypeOperator(input *ast.TypeOperatorNode) *ast.Node { + t := tx.Visitor().Visit(input.Type) + if t == nil { + return nil + } + return tx.Factory().UpdateTypeOperatorNode(input, input.Operator, t) +} + +func (tx *DeclarationTransformer) transformArrayType(input *ast.ArrayTypeNode) *ast.Node { + elem := tx.Visitor().Visit(input.ElementType) + if elem == nil { + return nil + } + return tx.Factory().UpdateArrayTypeNode(input, elem) +} + +func (tx *DeclarationTransformer) transformIndexedAccessType(input *ast.IndexedAccessTypeNode) *ast.Node { + objectType := tx.Visitor().Visit(input.ObjectType) + indexType := tx.Visitor().Visit(input.IndexType) + if objectType == nil || indexType == nil { + return nil + } + return tx.Factory().UpdateIndexedAccessTypeNode(input, objectType, indexType) +} + func (tx *DeclarationTransformer) transformTypeReference(input *ast.TypeReferenceNode) *ast.Node { tx.checkEntityNameVisibility(input.TypeName, tx.enclosingDeclaration) return tx.Visitor().VisitEachChild(input.AsNode()) diff --git a/tsc/testdata/baselines/reference/compiler/declarationEmitStripInternalParenthesizedType.js b/tsc/testdata/baselines/reference/compiler/declarationEmitStripInternalParenthesizedType.js new file mode 100644 index 0000000000000..921d7383e472f --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/declarationEmitStripInternalParenthesizedType.js @@ -0,0 +1,70 @@ +//// [tests/cases/compiler/declarationEmitStripInternalParenthesizedType.ts] //// + +//// [declarationEmitStripInternalParenthesizedType.ts] +interface Foo {} + +export type T = ( + /** @internal */ + Foo +); + +export const x: ( + /** @internal */ + Foo +) = {} as any; + +export type T2 = readonly ( + /** @internal */ + Foo +)[]; + +interface Bar { + x: string; +} + +export type T3 = ( + /** @internal */ + Bar +)["x"]; + +export type T4 = Bar[( + /** @internal */ + "x" +)]; + + +//// [declarationEmitStripInternalParenthesizedType.js] +export const x = {}; + + +//// [declarationEmitStripInternalParenthesizedType.d.ts] +export type T = any; +export declare const x; +export type T2 = any; +interface Bar { + x: string; +} +export type T3 = any; +export type T4 = any; +export {}; + + +//// [DtsFileErrors] + + +declarationEmitStripInternalParenthesizedType.d.ts(2,22): error TS7005: Variable 'x' implicitly has an 'any' type. + + +==== declarationEmitStripInternalParenthesizedType.d.ts (1 errors) ==== + export type T = any; + export declare const x; + ~ +!!! error TS7005: Variable 'x' implicitly has an 'any' type. + export type T2 = any; + interface Bar { + x: string; + } + export type T3 = any; + export type T4 = any; + export {}; + \ No newline at end of file diff --git a/tsc/testdata/baselines/reference/compiler/declarationEmitStripInternalParenthesizedType.symbols b/tsc/testdata/baselines/reference/compiler/declarationEmitStripInternalParenthesizedType.symbols new file mode 100644 index 0000000000000..69dfc5aa3da36 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/declarationEmitStripInternalParenthesizedType.symbols @@ -0,0 +1,57 @@ +//// [tests/cases/compiler/declarationEmitStripInternalParenthesizedType.ts] //// + +=== declarationEmitStripInternalParenthesizedType.ts === +interface Foo {} +>Foo : Symbol(Foo, Decl(declarationEmitStripInternalParenthesizedType.ts, 0, 0)) + +export type T = ( +>T : Symbol(T, Decl(declarationEmitStripInternalParenthesizedType.ts, 0, 16)) + + /** @internal */ + Foo +>Foo : Symbol(Foo, Decl(declarationEmitStripInternalParenthesizedType.ts, 0, 0)) + +); + +export const x: ( +>x : Symbol(x, Decl(declarationEmitStripInternalParenthesizedType.ts, 7, 12)) + + /** @internal */ + Foo +>Foo : Symbol(Foo, Decl(declarationEmitStripInternalParenthesizedType.ts, 0, 0)) + +) = {} as any; + +export type T2 = readonly ( +>T2 : Symbol(T2, Decl(declarationEmitStripInternalParenthesizedType.ts, 10, 14)) + + /** @internal */ + Foo +>Foo : Symbol(Foo, Decl(declarationEmitStripInternalParenthesizedType.ts, 0, 0)) + +)[]; + +interface Bar { +>Bar : Symbol(Bar, Decl(declarationEmitStripInternalParenthesizedType.ts, 15, 4)) + + x: string; +>x : Symbol(Bar.x, Decl(declarationEmitStripInternalParenthesizedType.ts, 17, 15)) +} + +export type T3 = ( +>T3 : Symbol(T3, Decl(declarationEmitStripInternalParenthesizedType.ts, 19, 1)) + + /** @internal */ + Bar +>Bar : Symbol(Bar, Decl(declarationEmitStripInternalParenthesizedType.ts, 15, 4)) + +)["x"]; + +export type T4 = Bar[( +>T4 : Symbol(T4, Decl(declarationEmitStripInternalParenthesizedType.ts, 24, 7)) +>Bar : Symbol(Bar, Decl(declarationEmitStripInternalParenthesizedType.ts, 15, 4)) + + /** @internal */ + "x" +)]; + diff --git a/tsc/testdata/baselines/reference/compiler/declarationEmitStripInternalParenthesizedType.types b/tsc/testdata/baselines/reference/compiler/declarationEmitStripInternalParenthesizedType.types new file mode 100644 index 0000000000000..c09fa2f324a0f --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/declarationEmitStripInternalParenthesizedType.types @@ -0,0 +1,47 @@ +//// [tests/cases/compiler/declarationEmitStripInternalParenthesizedType.ts] //// + +=== declarationEmitStripInternalParenthesizedType.ts === +interface Foo {} + +export type T = ( +>T : Foo + + /** @internal */ + Foo +); + +export const x: ( +>x : Foo + + /** @internal */ + Foo +) = {} as any; +>{} as any : any +>{} : {} + +export type T2 = readonly ( +>T2 : T2 + + /** @internal */ + Foo +)[]; + +interface Bar { + x: string; +>x : string +} + +export type T3 = ( +>T3 : string + + /** @internal */ + Bar +)["x"]; + +export type T4 = Bar[( +>T4 : string + + /** @internal */ + "x" +)]; + diff --git a/tsc/testdata/tests/cases/compiler/declarationEmitStripInternalParenthesizedType.ts b/tsc/testdata/tests/cases/compiler/declarationEmitStripInternalParenthesizedType.ts new file mode 100644 index 0000000000000..e915140811ff0 --- /dev/null +++ b/tsc/testdata/tests/cases/compiler/declarationEmitStripInternalParenthesizedType.ts @@ -0,0 +1,33 @@ +// @declaration: true +// @stripInternal: true + +interface Foo {} + +export type T = ( + /** @internal */ + Foo +); + +export const x: ( + /** @internal */ + Foo +) = {} as any; + +export type T2 = readonly ( + /** @internal */ + Foo +)[]; + +interface Bar { + x: string; +} + +export type T3 = ( + /** @internal */ + Bar +)["x"]; + +export type T4 = Bar[( + /** @internal */ + "x" +)];