繁体   English   中英

无法让 babel 在 typescript 上使用插值编译字符串文字类型

[英]Can't get babel to compile string literal type with interpolation on typescript

我有一个使用 typescript 的反应应用程序,我需要使用带插值的文字字符串类型创建类型:

class ExampleClass {
    product_id!: number;
    product_name!: string;
}

export type HeaderClassNames<N extends string> = `theader_${Lowercase<N>}`;

type HeaderClasses = HeaderClassNames<keyof ExampleClass>;

“HeaderClasses”产生:

type HeaderClasses = "theader_product_id" | "theader_product_name"

tsc 可以正常工作,但是当 babel 转译时,它无法识别模板:

Unexpected token (8:72)

   6 | 
   7 | export type TableHeaderConfig<Keys extends string> = Record<Keys, TableHeaderColumn>;
>  8 | export type HeaderClassNames<N extends string> = `theader_${Lowercase<N>}`;
     |                                                                         ^
   9 | export type ColumnClassNames<N extends string> = `column_${Lowercase<N>}`;
  10 | 

Babel说它开始支持这个功能,但是我安装并重新安装了 babel/core,甚至手动尝试了 babel/parser(提交的地方),但没有任何改变。

感谢你的帮助。

经过大量挖掘,我发现正在使用的 babel 版本是 react-app-rewired 和 react-scripts 包中的那个。 几个小时后,你的头就不再工作了。

我更新了它们,一切正常。

感谢所有花一分钟时间提供帮助的人。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM