简体   繁体   English

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

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

I have a react application using typescript and I need to create types using literal string types with interpolation:我有一个使用 typescript 的反应应用程序,我需要使用带插值的文字字符串类型创建类型:

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

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

type HeaderClasses = HeaderClassNames<keyof ExampleClass>;

"HeaderClasses" produces: “HeaderClasses”产生:

type HeaderClasses = "theader_product_id" | "theader_product_name"

This works fine by the tsc, but when babel transpiles, it doesn't recognize the template: 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 said it started supporting this feature, but I install and reinstall babel/core and even tried babel/parser manually (where the commit was made) but nothing changes. Babel说它开始支持这个功能,但是我安装并重新安装了 babel/core,甚至手动尝试了 babel/parser(提交的地方),但没有任何改变。

Appreciate your help.感谢你的帮助。

After a lot of digging, I found out that the babel version being used was the one at the react-app-rewired and react-scripts packages.经过大量挖掘,我发现正在使用的 babel 版本是 react-app-rewired 和 react-scripts 包中的那个。 After some hours your head just doesn't work anymore.几个小时后,你的头就不再工作了。

I updated them and everything worked.我更新了它们,一切正常。

Thanks to everyone that spend a minute trying to help.感谢所有花一分钟时间提供帮助的人。

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

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