diff --git a/tsc/internal/checker/nodebuilderimpl.go b/tsc/internal/checker/nodebuilderimpl.go index 2d2d01e83affa..f93844470efe6 100644 --- a/tsc/internal/checker/nodebuilderimpl.go +++ b/tsc/internal/checker/nodebuilderimpl.go @@ -757,19 +757,25 @@ func (b *NodeBuilderImpl) symbolToTypeNode(symbol *ast.Symbol, mask ast.SymbolFl } + restoreFlags := b.saveRestoreFlags() + if isTypeOf { + // Type queries permit arguments only after the complete entity name. + b.ctx.flags &^= nodebuilder.FlagsWriteTypeParametersInQualifiedName + } entityName := b.createAccessFromSymbolChain(chain, len(chain)-1, 0, typeArguments) + restoreFlags() if ast.IsIndexedAccessTypeNode(entityName) { return entityName // Indexed accesses can never be `typeof` } if ast.IsEntityName(entityName) { if isTypeOf { - return b.f.NewTypeQueryNode(entityName, nil) + return b.f.NewTypeQueryNode(entityName, typeArguments) } return b.f.NewTypeReferenceNode(entityName, typeArguments) } if isTypeOf && ast.IsExpressionWithTypeArguments(entityName) { expr := entityName.AsExpressionWithTypeArguments() - return b.f.NewTypeQueryNode(b.f.DeepCloneNode(expr.Expression), expr.TypeArguments) + return b.f.NewTypeQueryNode(printer.CreateEntityNameFromExpression(b.f, expr.Expression), expr.TypeArguments) } return entityName } diff --git a/tsc/internal/fourslash/tests/codeFixMissingTypeAnnotationOnExports34-object-spread_test.go b/tsc/internal/fourslash/tests/codeFixMissingTypeAnnotationOnExports34-object-spread_test.go index c264dbb96c43c..d68bc39e305a3 100644 --- a/tsc/internal/fourslash/tests/codeFixMissingTypeAnnotationOnExports34-object-spread_test.go +++ b/tsc/internal/fourslash/tests/codeFixMissingTypeAnnotationOnExports34-object-spread_test.go @@ -74,3 +74,30 @@ export const All: typeof All_Part1_1 & typeof Start & typeof All_Part3 & typeof Index: 1, }) } + +func TestCodeFixMissingTypeAnnotationOnQualifiedObjectSpread(t *testing.T) { + t.Parallel() + defer testutil.RecoverAndFail(t, "Panic on fourslash test") + const content = `// @isolatedDeclarations: true +// @declaration: true +// @Filename: /code.ts +declare namespace N { + namespace Nested { + const values: { x: number }; + } +} +export const all = { ...N.Nested.values };` + f, done := fourslash.NewFourslash(t, nil /*capabilities*/, content) + defer done() + f.VerifyCodeFix(t, fourslash.VerifyCodeFixOptions{ + Description: "Add annotation of type 'typeof N.Nested.values'", + NewFileContent: `declare namespace N { + namespace Nested { + const values: { x: number }; + } +} +export const all: typeof N.Nested.values = { + ...N.Nested.values +};`, + }) +} diff --git a/tsc/internal/ls/codeactions_fixmissingtypeannotation.go b/tsc/internal/ls/codeactions_fixmissingtypeannotation.go index 783f2207f78aa..7318ce1830bf9 100644 --- a/tsc/internal/ls/codeactions_fixmissingtypeannotation.go +++ b/tsc/internal/ls/codeactions_fixmissingtypeannotation.go @@ -935,7 +935,7 @@ func (f *isolatedDeclarationsFixer) getExtraFlags(node *ast.Node, t *checker.Typ // createTypeOfFromEntityNameExpression creates a `typeof X` type query node. func (f *isolatedDeclarationsFixer) createTypeOfFromEntityNameExpression(node *ast.Node) *ast.TypeNode { return f.changeTracker.NodeFactory.NewTypeQueryNode( - f.changeTracker.NodeFactory.DeepCloneNode(node), nil, + printer.CreateEntityNameFromExpression(f.changeTracker.NodeFactory, node), nil, ) } diff --git a/tsc/internal/printer/factory.go b/tsc/internal/printer/factory.go index dbc9242abe400..596cad98c612f 100644 --- a/tsc/internal/printer/factory.go +++ b/tsc/internal/printer/factory.go @@ -281,6 +281,17 @@ func (f *NodeFactory) InlineExpressions(expressions []*ast.Expression) *ast.Expr // Utilities // +func CreateEntityNameFromExpression(f *ast.NodeFactory, node *ast.Expression) *ast.EntityName { + if ast.IsIdentifier(node) { + return f.DeepCloneNode(node) + } + debug.Assert(ast.IsPropertyAccessExpression(node) && ast.IsIdentifier(node.Name())) + return f.NewQualifiedName( + CreateEntityNameFromExpression(f, node.Expression()), + f.DeepCloneNode(node.Name()), + ) +} + func (f *NodeFactory) CreateExpressionFromEntityName(node *ast.Node) *ast.Expression { if ast.IsQualifiedName(node) { left := f.CreateExpressionFromEntityName(node.AsQualifiedName().Left) diff --git a/tsc/internal/printer/printer.go b/tsc/internal/printer/printer.go index a4cb6e3591cba..e1a70968e4403 100644 --- a/tsc/internal/printer/printer.go +++ b/tsc/internal/printer/printer.go @@ -1229,10 +1229,6 @@ func (p *Printer) emitEntityName(node *ast.EntityName) { p.emitIdentifierReference(node.AsIdentifier()) case ast.KindQualifiedName: p.emitQualifiedName(node.AsQualifiedName()) - case ast.KindPropertyAccessExpression: - // TypeQuery nodes may have PropertyAccessExpression as exprName (e.g. typeof foo.x). - // TS's emitter handles this via generic emit(); we dispatch to expression emitter here. - p.emitExpression(node, ast.OperatorPrecedenceDisallowComma) default: panic(fmt.Sprintf("unexpected EntityName: %v", node.Kind)) }