[typescript] parsing template literal as type (#9748)
This commit is contained in:
committed by
Nicolò Ribaudo
parent
444daf9224
commit
2867bbf195
@@ -660,6 +660,19 @@ export default (superClass: Class<Parser>): Class<Parser> =>
|
||||
return this.finishNode(node, "TSLiteralType");
|
||||
}
|
||||
|
||||
tsParseTemplateLiteralType(): N.TsType {
|
||||
const node: N.TsLiteralType = this.startNode();
|
||||
const templateNode = this.parseTemplate(false);
|
||||
if (templateNode.expressions.length > 0) {
|
||||
throw this.raise(
|
||||
templateNode.expressions[0].start,
|
||||
"Template literal types cannot have any substitution",
|
||||
);
|
||||
}
|
||||
node.literal = templateNode;
|
||||
return this.finishNode(node, "TSLiteralType");
|
||||
}
|
||||
|
||||
tsParseNonArrayType(): N.TsType {
|
||||
switch (this.state.type) {
|
||||
case tt.name:
|
||||
@@ -712,6 +725,8 @@ export default (superClass: Class<Parser>): Class<Parser> =>
|
||||
return this.tsParseTupleType();
|
||||
case tt.parenL:
|
||||
return this.tsParseParenthesizedType();
|
||||
case tt.backQuote:
|
||||
return this.tsParseTemplateLiteralType();
|
||||
}
|
||||
|
||||
throw this.unexpected();
|
||||
|
||||
@@ -1308,7 +1308,7 @@ export type TsMappedType = TsTypeBase & {
|
||||
|
||||
export type TsLiteralType = TsTypeBase & {
|
||||
type: "TSLiteralType",
|
||||
literal: NumericLiteral | StringLiteral | BooleanLiteral,
|
||||
literal: NumericLiteral | StringLiteral | BooleanLiteral | TemplateLiteral,
|
||||
};
|
||||
|
||||
export type TsImportType = TsTypeBase & {
|
||||
|
||||
Reference in New Issue
Block a user