From 43d47de74dad468be328af32c6a2776f55b10bbc Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders Date: Tue, 13 Jun 2017 13:59:38 -0700 Subject: [PATCH 1/2] Add parentheses around keyof in declaration emit When needed. Use InElementType flag to determine this. --- src/compiler/checker.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index b7dd84f7716..338dd72d52d 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -3233,9 +3233,15 @@ namespace ts { writer.writeStringLiteral(literalTypeToString(type)); } else if (type.flags & TypeFlags.Index) { + if (flags & TypeFormatFlags.InElementType) { + writePunctuation(writer, SyntaxKind.OpenParenToken); + } writer.writeKeyword("keyof"); writeSpace(writer); writeType((type).type, TypeFormatFlags.InElementType); + if (flags & TypeFormatFlags.InElementType) { + writePunctuation(writer, SyntaxKind.CloseParenToken); + } } else if (type.flags & TypeFlags.IndexedAccess) { writeType((type).objectType, TypeFormatFlags.InElementType); From 2d2ac6794f133843ec0f601655c0550850a14dee Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders Date: Tue, 13 Jun 2017 14:00:26 -0700 Subject: [PATCH 2/2] Test:correct parens around keyof in decl emit --- .../declarationEmitIndexTypeArray.js | 25 +++++++++++++++++++ .../declarationEmitIndexTypeArray.symbols | 15 +++++++++++ .../declarationEmitIndexTypeArray.types | 16 ++++++++++++ .../compiler/declarationEmitIndexTypeArray.ts | 6 +++++ 4 files changed, 62 insertions(+) create mode 100644 tests/baselines/reference/declarationEmitIndexTypeArray.js create mode 100644 tests/baselines/reference/declarationEmitIndexTypeArray.symbols create mode 100644 tests/baselines/reference/declarationEmitIndexTypeArray.types create mode 100644 tests/cases/compiler/declarationEmitIndexTypeArray.ts diff --git a/tests/baselines/reference/declarationEmitIndexTypeArray.js b/tests/baselines/reference/declarationEmitIndexTypeArray.js new file mode 100644 index 00000000000..a84080fb5c4 --- /dev/null +++ b/tests/baselines/reference/declarationEmitIndexTypeArray.js @@ -0,0 +1,25 @@ +//// [declarationEmitIndexTypeArray.ts] +function doSomethingWithKeys(...keys: (keyof T)[]) { } + +const utilityFunctions = { + doSomethingWithKeys +}; + + +//// [declarationEmitIndexTypeArray.js] +function doSomethingWithKeys() { + var keys = []; + for (var _i = 0; _i < arguments.length; _i++) { + keys[_i] = arguments[_i]; + } +} +var utilityFunctions = { + doSomethingWithKeys: doSomethingWithKeys +}; + + +//// [declarationEmitIndexTypeArray.d.ts] +declare function doSomethingWithKeys(...keys: (keyof T)[]): void; +declare const utilityFunctions: { + doSomethingWithKeys: (...keys: (keyof T)[]) => void; +}; diff --git a/tests/baselines/reference/declarationEmitIndexTypeArray.symbols b/tests/baselines/reference/declarationEmitIndexTypeArray.symbols new file mode 100644 index 00000000000..1f6cb302881 --- /dev/null +++ b/tests/baselines/reference/declarationEmitIndexTypeArray.symbols @@ -0,0 +1,15 @@ +=== tests/cases/compiler/declarationEmitIndexTypeArray.ts === +function doSomethingWithKeys(...keys: (keyof T)[]) { } +>doSomethingWithKeys : Symbol(doSomethingWithKeys, Decl(declarationEmitIndexTypeArray.ts, 0, 0)) +>T : Symbol(T, Decl(declarationEmitIndexTypeArray.ts, 0, 29)) +>keys : Symbol(keys, Decl(declarationEmitIndexTypeArray.ts, 0, 32)) +>T : Symbol(T, Decl(declarationEmitIndexTypeArray.ts, 0, 29)) + +const utilityFunctions = { +>utilityFunctions : Symbol(utilityFunctions, Decl(declarationEmitIndexTypeArray.ts, 2, 5)) + + doSomethingWithKeys +>doSomethingWithKeys : Symbol(doSomethingWithKeys, Decl(declarationEmitIndexTypeArray.ts, 2, 26)) + +}; + diff --git a/tests/baselines/reference/declarationEmitIndexTypeArray.types b/tests/baselines/reference/declarationEmitIndexTypeArray.types new file mode 100644 index 00000000000..67f8905639e --- /dev/null +++ b/tests/baselines/reference/declarationEmitIndexTypeArray.types @@ -0,0 +1,16 @@ +=== tests/cases/compiler/declarationEmitIndexTypeArray.ts === +function doSomethingWithKeys(...keys: (keyof T)[]) { } +>doSomethingWithKeys : (...keys: keyof T[]) => void +>T : T +>keys : keyof T[] +>T : T + +const utilityFunctions = { +>utilityFunctions : { doSomethingWithKeys: (...keys: keyof T[]) => void; } +>{ doSomethingWithKeys} : { doSomethingWithKeys: (...keys: keyof T[]) => void; } + + doSomethingWithKeys +>doSomethingWithKeys : (...keys: keyof T[]) => void + +}; + diff --git a/tests/cases/compiler/declarationEmitIndexTypeArray.ts b/tests/cases/compiler/declarationEmitIndexTypeArray.ts new file mode 100644 index 00000000000..be80413aa63 --- /dev/null +++ b/tests/cases/compiler/declarationEmitIndexTypeArray.ts @@ -0,0 +1,6 @@ +// @declaration: true +function doSomethingWithKeys(...keys: (keyof T)[]) { } + +const utilityFunctions = { + doSomethingWithKeys +};