Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support TypeScript 4.1 via Babel #9473

Merged
merged 5 commits into from
Oct 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 40 additions & 0 deletions changelog_unreleased/typescript/pr-9473.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#### Support TypeScript 4.1 via Babel (#9473 by @sosukesuzuki)

`--parser=babel-ts` is required.

##### Key Remapping In Mapped Types

```ts
// Input
type MappedTypeWithNewKeys<T> = {
[K in keyof T as NewKeyType]: T[K]
};

// Prettier master
SyntaxError: Unexpected token, expected "]" (2:17)
1 | type MappedTypeWithNewKeys<T> = {
> 2 | [K in keyof T as NewKeyType]: T[K]
| ^
3 | };

// Prettier stable
type MappedTypeWithNewKeys<T> = {
[K in keyof T as NewKeyType]: T[K]
};
```

##### Template Literal Types

```ts
// Input
type HelloWorld = `Hello, ${keyof World}`

// Prettier master
SyntaxError: Unexpected token, expected "}" (1:35)
> 1 | type HelloWorld = `Hello, ${keyof World}`
| ^

// Prettier stable
type HelloWorld = `Hello, ${keyof World}`;

```
1 change: 1 addition & 0 deletions cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@
"Kassens",
"Kasturi",
"Kearney",
"keyof",
"Khatri",
"l’objectif",
"lcov",
Expand Down
8 changes: 8 additions & 0 deletions src/language-js/printer-estree.js
Original file line number Diff line number Diff line change
Expand Up @@ -2943,6 +2943,14 @@ function printPathNoParens(path, options, print, args) {
if (n.constraint) {
parts.push(" in ", path.call(print, "constraint"));
}
if (parent.nameType) {
parts.push(
" as ",
path.callParent((path) => {
return path.call(print, "nameType");
})
);
}
parts.push("]");
return concat(parts);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`key-remapping.ts format 1`] = `
====================================options=====================================
parsers: ["babel-ts", "typescript"]
printWidth: 80
| printWidth
=====================================input======================================
type MappedTypeWithNewKeys<T> = {
[K in keyof T as NewKeyType]: T[K]
};

type RemoveKindField<T> = {
[K in keyof T as Exclude<K, "kind">]: T[K]
};

type PickByValueType<T, U> = {
[K in keyof T as T[K] extends U ? K : never]: T[K]
};

=====================================output=====================================
type MappedTypeWithNewKeys<T> = {
[K in keyof T as NewKeyType]: T[K];
};

type RemoveKindField<T> = {
[K in keyof T as Exclude<K, "kind">]: T[K];
};

type PickByValueType<T, U> = {
[K in keyof T as T[K] extends U ? K : never]: T[K];
};

================================================================================
`;
3 changes: 3 additions & 0 deletions tests/typescript/key-remapping-in-mapped-types/jsfmt.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
run_spec(__dirname, ["babel-ts", "typescript"], {
errors: { typescript: true },
});
11 changes: 11 additions & 0 deletions tests/typescript/key-remapping-in-mapped-types/key-remapping.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
type MappedTypeWithNewKeys<T> = {
[K in keyof T as NewKeyType]: T[K]
};

type RemoveKindField<T> = {
[K in keyof T as Exclude<K, "kind">]: T[K]
};

type PickByValueType<T, U> = {
[K in keyof T as T[K] extends U ? K : never]: T[K]
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`template-literal-types.ts format 1`] = `
====================================options=====================================
parsers: ["babel-ts", "typescript"]
printWidth: 80
| printWidth
=====================================input======================================
let x: \`foo-\${infer bar}\`;
type HelloWorld = \`\${Hello}, \${World}\`
type SeussFish = \`\${Quantity | Color} fish\`;
declare function setAlignment(value: \`\${VerticalAlignment}-\${HorizontalAlignment}\`): void;
type PropEventSource<T> = {
on(eventName: \`\${string & keyof T}Changed\`, callback: () => void): void;
};
type PropEventSource<T> = {
on<K extends string & keyof T>
(eventName: \`\${K}Changed\`, callback: (newValue: T[K]) => void ): void;
};

=====================================output=====================================
let x: \`foo-\${infer bar}\`;
type HelloWorld = \`\${Hello}, \${World}\`;
type SeussFish = \`\${Quantity | Color} fish\`;
declare function setAlignment(
value: \`\${VerticalAlignment}-\${HorizontalAlignment}\`
): void;
type PropEventSource<T> = {
on(eventName: \`\${string & keyof T}Changed\`, callback: () => void): void;
};
type PropEventSource<T> = {
on<K extends string & keyof T>(
eventName: \`\${K}Changed\`,
callback: (newValue: T[K]) => void
): void;
};

================================================================================
`;
3 changes: 3 additions & 0 deletions tests/typescript/template-literal-types/jsfmt.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
run_spec(__dirname, ["babel-ts", "typescript"], {
errors: { typescript: true },
});
11 changes: 11 additions & 0 deletions tests/typescript/template-literal-types/template-literal-types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
let x: `foo-${infer bar}`;
type HelloWorld = `${Hello}, ${World}`
type SeussFish = `${Quantity | Color} fish`;
declare function setAlignment(value: `${VerticalAlignment}-${HorizontalAlignment}`): void;
type PropEventSource<T> = {
on(eventName: `${string & keyof T}Changed`, callback: () => void): void;
};
type PropEventSource<T> = {
on<K extends string & keyof T>
(eventName: `${K}Changed`, callback: (newValue: T[K]) => void ): void;
};