diff --git a/src/compiler/declarationEmitter.ts b/src/compiler/declarationEmitter.ts index 0bba375a2cb..7d2f61b6d6f 100644 --- a/src/compiler/declarationEmitter.ts +++ b/src/compiler/declarationEmitter.ts @@ -416,7 +416,9 @@ namespace ts { case SyntaxKind.TypeOperator: return emitTypeOperator(type); case SyntaxKind.IndexedAccessType: - return emitPropertyAccessType(type); + return emitIndexedAccessType(type); + case SyntaxKind.MappedType: + return emitMappedType(type); case SyntaxKind.FunctionType: case SyntaxKind.ConstructorType: return emitSignatureDeclarationWithJsDocComments(type); @@ -516,13 +518,39 @@ namespace ts { emitType(type.type); } - function emitPropertyAccessType(node: IndexedAccessTypeNode) { + function emitIndexedAccessType(node: IndexedAccessTypeNode) { emitType(node.objectType); write("["); emitType(node.indexType); write("]"); } + function emitMappedType(node: MappedTypeNode) { + const prevEnclosingDeclaration = enclosingDeclaration; + enclosingDeclaration = node; + write("{"); + writeLine(); + increaseIndent(); + if (node.readonlyToken) { + write("readonly "); + } + write("["); + writeEntityName(node.typeParameter.name); + write(" in "); + emitType(node.typeParameter.constraint); + write("]"); + if (node.questionToken) { + write("?"); + } + write(": "); + emitType(node.type); + write(";"); + writeLine(); + decreaseIndent(); + write("}"); + enclosingDeclaration = prevEnclosingDeclaration; + } + function emitTypeLiteral(type: TypeLiteralNode) { write("{"); if (type.members.length) { diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index fa0d44a695f..a18146bdaa1 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -606,7 +606,9 @@ const _super = (function (geti, seti) { case SyntaxKind.TypeOperator: return emitTypeOperator(node); case SyntaxKind.IndexedAccessType: - return emitPropertyAccessType(node); + return emitIndexedAccessType(node); + case SyntaxKind.MappedType: + return emitMappedType(node); case SyntaxKind.LiteralType: return emitLiteralType(node); @@ -1109,13 +1111,36 @@ const _super = (function (geti, seti) { emit(node.type); } - function emitPropertyAccessType(node: IndexedAccessTypeNode) { + function emitIndexedAccessType(node: IndexedAccessTypeNode) { emit(node.objectType); write("["); emit(node.indexType); write("]"); } + function emitMappedType(node: MappedTypeNode) { + write("{"); + writeLine(); + increaseIndent(); + if (node.readonlyToken) { + write("readonly "); + } + write("["); + emit(node.typeParameter.name); + write(" in "); + emit(node.typeParameter.constraint); + write("]"); + if (node.questionToken) { + write("?"); + } + write(": "); + emit(node.type); + write(";"); + writeLine(); + decreaseIndent(); + write("}"); + } + function emitLiteralType(node: LiteralTypeNode) { emitExpression(node.literal); }