Skip to content

Commit

Permalink
Support TypeScript mapped type 'as' clauses
Browse files Browse the repository at this point in the history
  • Loading branch information
existentialism committed Oct 2, 2020
1 parent 9808d25 commit 8bdf5d9
Show file tree
Hide file tree
Showing 11 changed files with 358 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Makefile
@@ -1,6 +1,6 @@
FLOW_COMMIT = a1f9a4c709dcebb27a5084acf47755fbae699c25
TEST262_COMMIT = 058adfed86b1d4129996faaf50a85ea55379a66a
TYPESCRIPT_COMMIT = d779a190535e52896cfe5100101173c00b6b8625
TYPESCRIPT_COMMIT = da8633212023517630de5f3620a23736b63234b1

FORCE_PUBLISH = "@babel/runtime,@babel/runtime-corejs2,@babel/runtime-corejs3,@babel/standalone"

Expand Down
10 changes: 9 additions & 1 deletion packages/babel-generator/src/generators/typescript.js
Expand Up @@ -330,7 +330,7 @@ export function TSIndexedAccessType(node) {
}

export function TSMappedType(node) {
const { readonly, typeParameter, optional } = node;
const { nameType, optional, readonly, typeParameter } = node;
this.token("{");
this.space();
if (readonly) {
Expand All @@ -345,6 +345,14 @@ export function TSMappedType(node) {
this.word("in");
this.space();
this.print(typeParameter.constraint, typeParameter);

if (nameType) {
this.space();
this.word("as");
this.space();
this.print(nameType, node);
}

this.token("]");

if (optional) {
Expand Down
@@ -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]
};
@@ -0,0 +1,3 @@
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] };
5 changes: 5 additions & 0 deletions packages/babel-parser/src/plugins/typescript/index.js
Expand Up @@ -621,6 +621,11 @@ export default (superClass: Class<Parser>): Class<Parser> =>

this.expect(tt.bracketL);
node.typeParameter = this.tsParseMappedTypeParameter();

if (this.eatContextual("as")) {
node.nameType = this.tsParseType();
}

this.expect(tt.bracketR);

if (this.match(tt.plusMin)) {
Expand Down
1 change: 1 addition & 0 deletions packages/babel-parser/src/types.js
Expand Up @@ -1339,6 +1339,7 @@ export type TsMappedType = TsTypeBase & {
typeParameter: TsTypeParameter,
optional?: true | "+" | "-",
typeAnnotation: ?TsType,
nameType: ?TsType,
};

export type TsLiteralType = TsTypeBase & {
Expand Down
@@ -0,0 +1 @@
type Foo<T> = { [K in keyof T as]: T[K] };
@@ -0,0 +1,7 @@
{
"sourceType": "module",
"plugins": [
"typescript"
],
"throws": "Unexpected token (1:32)"
}
@@ -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]
};

0 comments on commit 8bdf5d9

Please sign in to comment.