diff --git a/src/parser/plugins/typescript.ts b/src/parser/plugins/typescript.ts index b56231a3..31e0f5d6 100644 --- a/src/parser/plugins/typescript.ts +++ b/src/parser/plugins/typescript.ts @@ -305,6 +305,8 @@ function tsTryParseIndexSignature(): boolean { return false; } + const oldIsType = pushTypeContext(0); + expect(tt.bracketL); parseIdentifier(); tsParseTypeAnnotation(); @@ -312,6 +314,8 @@ function tsTryParseIndexSignature(): boolean { tsTryParseTypeAnnotation(); tsParseTypeMemberSemicolon(); + + popTypeContext(oldIsType); return true; } diff --git a/src/util/getClassInfo.ts b/src/util/getClassInfo.ts index 1b756cfd..5bcce4b2 100644 --- a/src/util/getClassInfo.ts +++ b/src/util/getClassInfo.ts @@ -52,6 +52,8 @@ export default function getClassInfo( ({constructorInitializers, constructorInsertPos} = processConstructor(tokens)); } else if (tokens.matches1(tt.semi)) { tokens.nextToken(); + } else if (tokens.currentToken().isType) { + tokens.nextToken(); } else { // Either a method or a field. Skip to the identifier part. const statementStartIndex = tokens.currentIndex(); diff --git a/test/typescript-test.ts b/test/typescript-test.ts index f73bff13..b12802d7 100644 --- a/test/typescript-test.ts +++ b/test/typescript-test.ts @@ -990,4 +990,25 @@ describe("typescript transform", () => { `, ); }); + + it("allows index signatures in classes", () => { + assertTypeScriptResult( + ` + export class Foo { + f() { + } + [name: string]: any; + x = 1; + } + `, + `"use strict";${ESMODULE_PREFIX} + class Foo {constructor() { this.x = 1; } + f() { + } + + ; + } exports.Foo = Foo; + `, + ); + }); });