Support TypeScript mapped type 'as' clauses (#12129)
This commit is contained in:
parent
3fd963fdc8
commit
6830c90ac9
@ -333,7 +333,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) {
|
||||
@ -348,6 +348,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) {
|
||||
|
||||
11
packages/babel-generator/test/fixtures/typescript/types-mapped-as/input.js
vendored
Normal file
11
packages/babel-generator/test/fixtures/typescript/types-mapped-as/input.js
vendored
Normal file
@ -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]
|
||||
};
|
||||
3
packages/babel-generator/test/fixtures/typescript/types-mapped-as/output.js
vendored
Normal file
3
packages/babel-generator/test/fixtures/typescript/types-mapped-as/output.js
vendored
Normal file
@ -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] };
|
||||
@ -620,6 +620,8 @@ export default (superClass: Class<Parser>): Class<Parser> =>
|
||||
|
||||
this.expect(tt.bracketL);
|
||||
node.typeParameter = this.tsParseMappedTypeParameter();
|
||||
node.nameType = this.eatContextual("as") ? this.tsParseType() : null;
|
||||
|
||||
this.expect(tt.bracketR);
|
||||
|
||||
if (this.match(tt.plusMin)) {
|
||||
|
||||
@ -1340,6 +1340,7 @@ export type TsMappedType = TsTypeBase & {
|
||||
typeParameter: TsTypeParameter,
|
||||
optional?: true | "+" | "-",
|
||||
typeAnnotation: ?TsType,
|
||||
nameType: ?TsType,
|
||||
};
|
||||
|
||||
export type TsLiteralType = TsTypeBase & {
|
||||
|
||||
1
packages/babel-parser/test/fixtures/typescript/types/mapped-as-invalid/input.ts
vendored
Normal file
1
packages/babel-parser/test/fixtures/typescript/types/mapped-as-invalid/input.ts
vendored
Normal file
@ -0,0 +1 @@
|
||||
type Foo<T> = { [K in keyof T as]: T[K] };
|
||||
7
packages/babel-parser/test/fixtures/typescript/types/mapped-as-invalid/options.json
vendored
Normal file
7
packages/babel-parser/test/fixtures/typescript/types/mapped-as-invalid/options.json
vendored
Normal file
@ -0,0 +1,7 @@
|
||||
{
|
||||
"sourceType": "module",
|
||||
"plugins": [
|
||||
"typescript"
|
||||
],
|
||||
"throws": "Unexpected token (1:32)"
|
||||
}
|
||||
11
packages/babel-parser/test/fixtures/typescript/types/mapped-as/input.ts
vendored
Normal file
11
packages/babel-parser/test/fixtures/typescript/types/mapped-as/input.ts
vendored
Normal file
@ -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]
|
||||
};
|
||||
307
packages/babel-parser/test/fixtures/typescript/types/mapped-as/output.json
vendored
Normal file
307
packages/babel-parser/test/fixtures/typescript/types/mapped-as/output.json
vendored
Normal file
@ -0,0 +1,307 @@
|
||||
{
|
||||
"type": "File",
|
||||
"start":0,"end":238,"loc":{"start":{"line":1,"column":0},"end":{"line":11,"column":2}},
|
||||
"program": {
|
||||
"type": "Program",
|
||||
"start":0,"end":238,"loc":{"start":{"line":1,"column":0},"end":{"line":11,"column":2}},
|
||||
"sourceType": "module",
|
||||
"interpreter": null,
|
||||
"body": [
|
||||
{
|
||||
"type": "TSTypeAliasDeclaration",
|
||||
"start":0,"end":73,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":2}},
|
||||
"id": {
|
||||
"type": "Identifier",
|
||||
"start":5,"end":26,"loc":{"start":{"line":1,"column":5},"end":{"line":1,"column":26},"identifierName":"MappedTypeWithNewKeys"},
|
||||
"name": "MappedTypeWithNewKeys"
|
||||
},
|
||||
"typeParameters": {
|
||||
"type": "TSTypeParameterDeclaration",
|
||||
"start":26,"end":29,"loc":{"start":{"line":1,"column":26},"end":{"line":1,"column":29}},
|
||||
"params": [
|
||||
{
|
||||
"type": "TSTypeParameter",
|
||||
"start":27,"end":28,"loc":{"start":{"line":1,"column":27},"end":{"line":1,"column":28}},
|
||||
"name": "T"
|
||||
}
|
||||
]
|
||||
},
|
||||
"typeAnnotation": {
|
||||
"type": "TSMappedType",
|
||||
"start":32,"end":72,"loc":{"start":{"line":1,"column":32},"end":{"line":3,"column":1}},
|
||||
"typeParameter": {
|
||||
"type": "TSTypeParameter",
|
||||
"start":37,"end":49,"loc":{"start":{"line":2,"column":3},"end":{"line":2,"column":15}},
|
||||
"name": "K",
|
||||
"constraint": {
|
||||
"type": "TSTypeOperator",
|
||||
"start":42,"end":49,"loc":{"start":{"line":2,"column":8},"end":{"line":2,"column":15}},
|
||||
"operator": "keyof",
|
||||
"typeAnnotation": {
|
||||
"type": "TSTypeReference",
|
||||
"start":48,"end":49,"loc":{"start":{"line":2,"column":14},"end":{"line":2,"column":15}},
|
||||
"typeName": {
|
||||
"type": "Identifier",
|
||||
"start":48,"end":49,"loc":{"start":{"line":2,"column":14},"end":{"line":2,"column":15},"identifierName":"T"},
|
||||
"name": "T"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"nameType": {
|
||||
"type": "TSTypeReference",
|
||||
"start":53,"end":63,"loc":{"start":{"line":2,"column":19},"end":{"line":2,"column":29}},
|
||||
"typeName": {
|
||||
"type": "Identifier",
|
||||
"start":53,"end":63,"loc":{"start":{"line":2,"column":19},"end":{"line":2,"column":29},"identifierName":"NewKeyType"},
|
||||
"name": "NewKeyType"
|
||||
}
|
||||
},
|
||||
"typeAnnotation": {
|
||||
"type": "TSIndexedAccessType",
|
||||
"start":66,"end":70,"loc":{"start":{"line":2,"column":32},"end":{"line":2,"column":36}},
|
||||
"objectType": {
|
||||
"type": "TSTypeReference",
|
||||
"start":66,"end":67,"loc":{"start":{"line":2,"column":32},"end":{"line":2,"column":33}},
|
||||
"typeName": {
|
||||
"type": "Identifier",
|
||||
"start":66,"end":67,"loc":{"start":{"line":2,"column":32},"end":{"line":2,"column":33},"identifierName":"T"},
|
||||
"name": "T"
|
||||
}
|
||||
},
|
||||
"indexType": {
|
||||
"type": "TSTypeReference",
|
||||
"start":68,"end":69,"loc":{"start":{"line":2,"column":34},"end":{"line":2,"column":35}},
|
||||
"typeName": {
|
||||
"type": "Identifier",
|
||||
"start":68,"end":69,"loc":{"start":{"line":2,"column":34},"end":{"line":2,"column":35},"identifierName":"K"},
|
||||
"name": "K"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "TSTypeAliasDeclaration",
|
||||
"start":75,"end":150,"loc":{"start":{"line":5,"column":0},"end":{"line":7,"column":2}},
|
||||
"id": {
|
||||
"type": "Identifier",
|
||||
"start":80,"end":95,"loc":{"start":{"line":5,"column":5},"end":{"line":5,"column":20},"identifierName":"RemoveKindField"},
|
||||
"name": "RemoveKindField"
|
||||
},
|
||||
"typeParameters": {
|
||||
"type": "TSTypeParameterDeclaration",
|
||||
"start":95,"end":98,"loc":{"start":{"line":5,"column":20},"end":{"line":5,"column":23}},
|
||||
"params": [
|
||||
{
|
||||
"type": "TSTypeParameter",
|
||||
"start":96,"end":97,"loc":{"start":{"line":5,"column":21},"end":{"line":5,"column":22}},
|
||||
"name": "T"
|
||||
}
|
||||
]
|
||||
},
|
||||
"typeAnnotation": {
|
||||
"type": "TSMappedType",
|
||||
"start":101,"end":149,"loc":{"start":{"line":5,"column":26},"end":{"line":7,"column":1}},
|
||||
"typeParameter": {
|
||||
"type": "TSTypeParameter",
|
||||
"start":106,"end":118,"loc":{"start":{"line":6,"column":3},"end":{"line":6,"column":15}},
|
||||
"name": "K",
|
||||
"constraint": {
|
||||
"type": "TSTypeOperator",
|
||||
"start":111,"end":118,"loc":{"start":{"line":6,"column":8},"end":{"line":6,"column":15}},
|
||||
"operator": "keyof",
|
||||
"typeAnnotation": {
|
||||
"type": "TSTypeReference",
|
||||
"start":117,"end":118,"loc":{"start":{"line":6,"column":14},"end":{"line":6,"column":15}},
|
||||
"typeName": {
|
||||
"type": "Identifier",
|
||||
"start":117,"end":118,"loc":{"start":{"line":6,"column":14},"end":{"line":6,"column":15},"identifierName":"T"},
|
||||
"name": "T"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"nameType": {
|
||||
"type": "TSTypeReference",
|
||||
"start":122,"end":140,"loc":{"start":{"line":6,"column":19},"end":{"line":6,"column":37}},
|
||||
"typeName": {
|
||||
"type": "Identifier",
|
||||
"start":122,"end":129,"loc":{"start":{"line":6,"column":19},"end":{"line":6,"column":26},"identifierName":"Exclude"},
|
||||
"name": "Exclude"
|
||||
},
|
||||
"typeParameters": {
|
||||
"type": "TSTypeParameterInstantiation",
|
||||
"start":129,"end":140,"loc":{"start":{"line":6,"column":26},"end":{"line":6,"column":37}},
|
||||
"params": [
|
||||
{
|
||||
"type": "TSTypeReference",
|
||||
"start":130,"end":131,"loc":{"start":{"line":6,"column":27},"end":{"line":6,"column":28}},
|
||||
"typeName": {
|
||||
"type": "Identifier",
|
||||
"start":130,"end":131,"loc":{"start":{"line":6,"column":27},"end":{"line":6,"column":28},"identifierName":"K"},
|
||||
"name": "K"
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "TSLiteralType",
|
||||
"start":133,"end":139,"loc":{"start":{"line":6,"column":30},"end":{"line":6,"column":36}},
|
||||
"literal": {
|
||||
"type": "StringLiteral",
|
||||
"start":133,"end":139,"loc":{"start":{"line":6,"column":30},"end":{"line":6,"column":36}},
|
||||
"extra": {
|
||||
"rawValue": "kind",
|
||||
"raw": "\"kind\""
|
||||
},
|
||||
"value": "kind"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"typeAnnotation": {
|
||||
"type": "TSIndexedAccessType",
|
||||
"start":143,"end":147,"loc":{"start":{"line":6,"column":40},"end":{"line":6,"column":44}},
|
||||
"objectType": {
|
||||
"type": "TSTypeReference",
|
||||
"start":143,"end":144,"loc":{"start":{"line":6,"column":40},"end":{"line":6,"column":41}},
|
||||
"typeName": {
|
||||
"type": "Identifier",
|
||||
"start":143,"end":144,"loc":{"start":{"line":6,"column":40},"end":{"line":6,"column":41},"identifierName":"T"},
|
||||
"name": "T"
|
||||
}
|
||||
},
|
||||
"indexType": {
|
||||
"type": "TSTypeReference",
|
||||
"start":145,"end":146,"loc":{"start":{"line":6,"column":42},"end":{"line":6,"column":43}},
|
||||
"typeName": {
|
||||
"type": "Identifier",
|
||||
"start":145,"end":146,"loc":{"start":{"line":6,"column":42},"end":{"line":6,"column":43},"identifierName":"K"},
|
||||
"name": "K"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "TSTypeAliasDeclaration",
|
||||
"start":152,"end":238,"loc":{"start":{"line":9,"column":0},"end":{"line":11,"column":2}},
|
||||
"id": {
|
||||
"type": "Identifier",
|
||||
"start":157,"end":172,"loc":{"start":{"line":9,"column":5},"end":{"line":9,"column":20},"identifierName":"PickByValueType"},
|
||||
"name": "PickByValueType"
|
||||
},
|
||||
"typeParameters": {
|
||||
"type": "TSTypeParameterDeclaration",
|
||||
"start":172,"end":178,"loc":{"start":{"line":9,"column":20},"end":{"line":9,"column":26}},
|
||||
"params": [
|
||||
{
|
||||
"type": "TSTypeParameter",
|
||||
"start":173,"end":174,"loc":{"start":{"line":9,"column":21},"end":{"line":9,"column":22}},
|
||||
"name": "T"
|
||||
},
|
||||
{
|
||||
"type": "TSTypeParameter",
|
||||
"start":176,"end":177,"loc":{"start":{"line":9,"column":24},"end":{"line":9,"column":25}},
|
||||
"name": "U"
|
||||
}
|
||||
]
|
||||
},
|
||||
"typeAnnotation": {
|
||||
"type": "TSMappedType",
|
||||
"start":181,"end":237,"loc":{"start":{"line":9,"column":29},"end":{"line":11,"column":1}},
|
||||
"typeParameter": {
|
||||
"type": "TSTypeParameter",
|
||||
"start":186,"end":198,"loc":{"start":{"line":10,"column":3},"end":{"line":10,"column":15}},
|
||||
"name": "K",
|
||||
"constraint": {
|
||||
"type": "TSTypeOperator",
|
||||
"start":191,"end":198,"loc":{"start":{"line":10,"column":8},"end":{"line":10,"column":15}},
|
||||
"operator": "keyof",
|
||||
"typeAnnotation": {
|
||||
"type": "TSTypeReference",
|
||||
"start":197,"end":198,"loc":{"start":{"line":10,"column":14},"end":{"line":10,"column":15}},
|
||||
"typeName": {
|
||||
"type": "Identifier",
|
||||
"start":197,"end":198,"loc":{"start":{"line":10,"column":14},"end":{"line":10,"column":15},"identifierName":"T"},
|
||||
"name": "T"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"nameType": {
|
||||
"type": "TSConditionalType",
|
||||
"start":202,"end":228,"loc":{"start":{"line":10,"column":19},"end":{"line":10,"column":45}},
|
||||
"checkType": {
|
||||
"type": "TSIndexedAccessType",
|
||||
"start":202,"end":206,"loc":{"start":{"line":10,"column":19},"end":{"line":10,"column":23}},
|
||||
"objectType": {
|
||||
"type": "TSTypeReference",
|
||||
"start":202,"end":203,"loc":{"start":{"line":10,"column":19},"end":{"line":10,"column":20}},
|
||||
"typeName": {
|
||||
"type": "Identifier",
|
||||
"start":202,"end":203,"loc":{"start":{"line":10,"column":19},"end":{"line":10,"column":20},"identifierName":"T"},
|
||||
"name": "T"
|
||||
}
|
||||
},
|
||||
"indexType": {
|
||||
"type": "TSTypeReference",
|
||||
"start":204,"end":205,"loc":{"start":{"line":10,"column":21},"end":{"line":10,"column":22}},
|
||||
"typeName": {
|
||||
"type": "Identifier",
|
||||
"start":204,"end":205,"loc":{"start":{"line":10,"column":21},"end":{"line":10,"column":22},"identifierName":"K"},
|
||||
"name": "K"
|
||||
}
|
||||
}
|
||||
},
|
||||
"extendsType": {
|
||||
"type": "TSTypeReference",
|
||||
"start":215,"end":216,"loc":{"start":{"line":10,"column":32},"end":{"line":10,"column":33}},
|
||||
"typeName": {
|
||||
"type": "Identifier",
|
||||
"start":215,"end":216,"loc":{"start":{"line":10,"column":32},"end":{"line":10,"column":33},"identifierName":"U"},
|
||||
"name": "U"
|
||||
}
|
||||
},
|
||||
"trueType": {
|
||||
"type": "TSTypeReference",
|
||||
"start":219,"end":220,"loc":{"start":{"line":10,"column":36},"end":{"line":10,"column":37}},
|
||||
"typeName": {
|
||||
"type": "Identifier",
|
||||
"start":219,"end":220,"loc":{"start":{"line":10,"column":36},"end":{"line":10,"column":37},"identifierName":"K"},
|
||||
"name": "K"
|
||||
}
|
||||
},
|
||||
"falseType": {
|
||||
"type": "TSNeverKeyword",
|
||||
"start":223,"end":228,"loc":{"start":{"line":10,"column":40},"end":{"line":10,"column":45}}
|
||||
}
|
||||
},
|
||||
"typeAnnotation": {
|
||||
"type": "TSIndexedAccessType",
|
||||
"start":231,"end":235,"loc":{"start":{"line":10,"column":48},"end":{"line":10,"column":52}},
|
||||
"objectType": {
|
||||
"type": "TSTypeReference",
|
||||
"start":231,"end":232,"loc":{"start":{"line":10,"column":48},"end":{"line":10,"column":49}},
|
||||
"typeName": {
|
||||
"type": "Identifier",
|
||||
"start":231,"end":232,"loc":{"start":{"line":10,"column":48},"end":{"line":10,"column":49},"identifierName":"T"},
|
||||
"name": "T"
|
||||
}
|
||||
},
|
||||
"indexType": {
|
||||
"type": "TSTypeReference",
|
||||
"start":233,"end":234,"loc":{"start":{"line":10,"column":50},"end":{"line":10,"column":51}},
|
||||
"typeName": {
|
||||
"type": "Identifier",
|
||||
"start":233,"end":234,"loc":{"start":{"line":10,"column":50},"end":{"line":10,"column":51},"identifierName":"K"},
|
||||
"name": "K"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
],
|
||||
"directives": []
|
||||
}
|
||||
}
|
||||
@ -33,6 +33,7 @@
|
||||
"start":18,"end":24,"loc":{"start":{"line":1,"column":18},"end":{"line":1,"column":24}}
|
||||
}
|
||||
},
|
||||
"nameType": null,
|
||||
"typeAnnotation": {
|
||||
"type": "TSNumberKeyword",
|
||||
"start":27,"end":33,"loc":{"start":{"line":1,"column":27},"end":{"line":1,"column":33}}
|
||||
@ -72,6 +73,7 @@
|
||||
"start":65,"end":71,"loc":{"start":{"line":2,"column":27},"end":{"line":2,"column":33}}
|
||||
}
|
||||
},
|
||||
"nameType": null,
|
||||
"optional": true,
|
||||
"typeAnnotation": {
|
||||
"type": "TSNumberKeyword",
|
||||
@ -112,6 +114,7 @@
|
||||
"start":114,"end":120,"loc":{"start":{"line":3,"column":28},"end":{"line":3,"column":34}}
|
||||
}
|
||||
},
|
||||
"nameType": null,
|
||||
"optional": "+",
|
||||
"typeAnnotation": {
|
||||
"type": "TSNumberKeyword",
|
||||
@ -152,6 +155,7 @@
|
||||
"start":164,"end":170,"loc":{"start":{"line":4,"column":28},"end":{"line":4,"column":34}}
|
||||
}
|
||||
},
|
||||
"nameType": null,
|
||||
"optional": "-",
|
||||
"typeAnnotation": {
|
||||
"type": "TSNumberKeyword",
|
||||
|
||||
@ -306,12 +306,13 @@ defineType("TSIndexedAccessType", {
|
||||
|
||||
defineType("TSMappedType", {
|
||||
aliases: ["TSType"],
|
||||
visitor: ["typeParameter", "typeAnnotation"],
|
||||
visitor: ["typeParameter", "typeAnnotation", "nameType"],
|
||||
fields: {
|
||||
readonly: validateOptional(bool),
|
||||
typeParameter: validateType("TSTypeParameter"),
|
||||
optional: validateOptional(bool),
|
||||
typeAnnotation: validateOptionalType("TSType"),
|
||||
nameType: validateOptionalType("TSType"),
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user