Skip to content

Commit 57f8648

Browse files
authored
Merge pull request #16505 from Microsoft/decl-emit-parenthesize-keyof
Add parentheses around keyof in declaration emit when needed
2 parents 6370fc8 + 2d2ac67 commit 57f8648

File tree

5 files changed

+68
-0
lines changed

5 files changed

+68
-0
lines changed

src/compiler/checker.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3233,9 +3233,15 @@ namespace ts {
32333233
writer.writeStringLiteral(literalTypeToString(<LiteralType>type));
32343234
}
32353235
else if (type.flags & TypeFlags.Index) {
3236+
if (flags & TypeFormatFlags.InElementType) {
3237+
writePunctuation(writer, SyntaxKind.OpenParenToken);
3238+
}
32363239
writer.writeKeyword("keyof");
32373240
writeSpace(writer);
32383241
writeType((<IndexType>type).type, TypeFormatFlags.InElementType);
3242+
if (flags & TypeFormatFlags.InElementType) {
3243+
writePunctuation(writer, SyntaxKind.CloseParenToken);
3244+
}
32393245
}
32403246
else if (type.flags & TypeFlags.IndexedAccess) {
32413247
writeType((<IndexedAccessType>type).objectType, TypeFormatFlags.InElementType);
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//// [declarationEmitIndexTypeArray.ts]
2+
function doSomethingWithKeys<T>(...keys: (keyof T)[]) { }
3+
4+
const utilityFunctions = {
5+
doSomethingWithKeys
6+
};
7+
8+
9+
//// [declarationEmitIndexTypeArray.js]
10+
function doSomethingWithKeys() {
11+
var keys = [];
12+
for (var _i = 0; _i < arguments.length; _i++) {
13+
keys[_i] = arguments[_i];
14+
}
15+
}
16+
var utilityFunctions = {
17+
doSomethingWithKeys: doSomethingWithKeys
18+
};
19+
20+
21+
//// [declarationEmitIndexTypeArray.d.ts]
22+
declare function doSomethingWithKeys<T>(...keys: (keyof T)[]): void;
23+
declare const utilityFunctions: {
24+
doSomethingWithKeys: <T>(...keys: (keyof T)[]) => void;
25+
};
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
=== tests/cases/compiler/declarationEmitIndexTypeArray.ts ===
2+
function doSomethingWithKeys<T>(...keys: (keyof T)[]) { }
3+
>doSomethingWithKeys : Symbol(doSomethingWithKeys, Decl(declarationEmitIndexTypeArray.ts, 0, 0))
4+
>T : Symbol(T, Decl(declarationEmitIndexTypeArray.ts, 0, 29))
5+
>keys : Symbol(keys, Decl(declarationEmitIndexTypeArray.ts, 0, 32))
6+
>T : Symbol(T, Decl(declarationEmitIndexTypeArray.ts, 0, 29))
7+
8+
const utilityFunctions = {
9+
>utilityFunctions : Symbol(utilityFunctions, Decl(declarationEmitIndexTypeArray.ts, 2, 5))
10+
11+
doSomethingWithKeys
12+
>doSomethingWithKeys : Symbol(doSomethingWithKeys, Decl(declarationEmitIndexTypeArray.ts, 2, 26))
13+
14+
};
15+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
=== tests/cases/compiler/declarationEmitIndexTypeArray.ts ===
2+
function doSomethingWithKeys<T>(...keys: (keyof T)[]) { }
3+
>doSomethingWithKeys : <T>(...keys: keyof T[]) => void
4+
>T : T
5+
>keys : keyof T[]
6+
>T : T
7+
8+
const utilityFunctions = {
9+
>utilityFunctions : { doSomethingWithKeys: <T>(...keys: keyof T[]) => void; }
10+
>{ doSomethingWithKeys} : { doSomethingWithKeys: <T>(...keys: keyof T[]) => void; }
11+
12+
doSomethingWithKeys
13+
>doSomethingWithKeys : <T>(...keys: keyof T[]) => void
14+
15+
};
16+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// @declaration: true
2+
function doSomethingWithKeys<T>(...keys: (keyof T)[]) { }
3+
4+
const utilityFunctions = {
5+
doSomethingWithKeys
6+
};

0 commit comments

Comments
 (0)