Repository: prettier/prettier (JavaScript)
Date: 2020-01-05 05:00:50+00:00
Task type: Bug Fix

Issue:
Format error syntax with language typescript
BEFORE RUN FORMAT
`type A = { readonly [key in keyof B] };`
AFTER RUN FORMAT
`type A = { readonly [key in keyof B]:  };`

Format error syntax with language typescript
BEFORE RUN FORMAT
`type A = { readonly [key in keyof B] };`
AFTER RUN FORMAT
`type A = { readonly [key in keyof B]:  };`



Issue discussion:
Some useful information's about this ticket 🔢 

https://github.com/prettier/prettier/blob/master/src/language-js/printer-estree.js#L3336

```diff
-              ": ",
+              n.typeAnnotation ? ": " : "",
```

https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/typescript-estree/src/ts-estree/ts-estree.ts#L1382-L1388
```ts
export interface TSMappedType extends BaseNode {
  type: AST_NODE_TYPES.TSMappedType;
  typeParameter: TSTypeParameterDeclaration;
  readonly?: boolean | '-' | '+';
  optional?: boolean | '-' | '+';
  typeAnnotation?: TypeNode;
}
```
Some useful information's about this ticket 🔢 

https://github.com/prettier/prettier/blob/master/src/language-js/printer-estree.js#L3336

```diff
-              ": ",
+              n.typeAnnotation ? ": " : "",
```

https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/typescript-estree/src/ts-estree/ts-estree.ts#L1382-L1388
```ts
export interface TSMappedType extends BaseNode {
  type: AST_NODE_TYPES.TSMappedType;
  typeParameter: TSTypeParameterDeclaration;
  readonly?: boolean | '-' | '+';
  optional?: boolean | '-' | '+';
  typeAnnotation?: TypeNode;
}
```

Solution patch:
diff --git a/changelog_unreleased/typescript/pr-7174.md b/changelog_unreleased/typescript/pr-7174.md
new file mode 100644
index 000000000000..225335abca92
--- /dev/null
+++ b/changelog_unreleased/typescript/pr-7174.md
@@ -0,0 +1,13 @@
+#### Fix printing of mapped types with the template type omitted ([#7221](https://github.com/prettier/prettier/pull/7221) by [@cola119](https://github.com/cola119))
+
+<!-- prettier-ignore -->
+```ts
+// Input
+type A = { [key in B] };
+
+// Prettier stable
+type A = { [key in B]: };
+
+// Prettier master
+type A = { [key in B] };
+```
diff --git a/src/language-js/printer-estree.js b/src/language-js/printer-estree.js
index 3f16b2f9fe54..29a4ab1421b4 100644
--- a/src/language-js/printer-estree.js
+++ b/src/language-js/printer-estree.js
@@ -3333,7 +3333,7 @@ function printPathNoParens(path, options, print, args) {
               n.optional
                 ? getTypeScriptMappedTypeModifier(n.optional, "?")
                 : "",
-              ": ",
+              n.typeAnnotation ? ": " : "",
               path.call(print, "typeAnnotation"),
               ifBreak(semi, "")
             ])


Test patch:
diff --git a/tests/typescript_mapped_type/__snapshots__/jsfmt.spec.js.snap b/tests/typescript_mapped_type/__snapshots__/jsfmt.spec.js.snap
new file mode 100644
index 000000000000..aa88ebac33e5
--- /dev/null
+++ b/tests/typescript_mapped_type/__snapshots__/jsfmt.spec.js.snap
@@ -0,0 +1,19 @@
+// Jest Snapshot v1, https://goo.gl/fbAQLP
+
+exports[`mapped-type.ts 1`] = `
+====================================options=====================================
+parsers: ["typescript"]
+printWidth: 80
+                                                                                | printWidth
+=====================================input======================================
+type Keys = 'option1' | 'option2';
+type A = { [K in Keys] };
+type B = { [K in Keys]+? };
+
+=====================================output=====================================
+type Keys = "option1" | "option2";
+type A = { [K in Keys] };
+type B = { [K in Keys]+? };
+
+================================================================================
+`;
diff --git a/tests/typescript_mapped_type/jsfmt.spec.js b/tests/typescript_mapped_type/jsfmt.spec.js
new file mode 100644
index 000000000000..2ea3bb6eb2e4
--- /dev/null
+++ b/tests/typescript_mapped_type/jsfmt.spec.js
@@ -0,0 +1,1 @@
+run_spec(__dirname, ["typescript"]);
diff --git a/tests/typescript_mapped_type/mapped-type.ts b/tests/typescript_mapped_type/mapped-type.ts
new file mode 100644
index 000000000000..83e408571f64
--- /dev/null
+++ b/tests/typescript_mapped_type/mapped-type.ts
@@ -0,0 +1,3 @@
+type Keys = 'option1' | 'option2';
+type A = { [K in Keys] };
+type B = { [K in Keys]+? };