typescript - 使用 Create React App 和 TypeScript 在 React 应用程序中出现奇怪的解析错误
问题描述
我正在使用Create React App,最近添加了 TypeScript 支持。
这个代码库开始出现奇怪的问题,使用enum
:
import React, { useState } from 'react';
import Page from 'components/shared/Page';
import DashboardChart from 'components/shared/DashboardChart';
import { Col, Row, Select, Form } from 'antd';
const { Option } = Select;
enum Periods {
twentyFourHours = 0,
oneWeek = 1,
thirtyDays = 2,
ninetyDays = 3,
oneHundredAndEightyDay = 4,
oneYear = 5
}
我收到此错误:
Line 9:1: Parsing error: Unexpected reserved word 'enum'
7 |
8 |
> 9 | enum Periods {
| ^
10 | twentyFourHours = 0,
11 | oneWeek = 1,
12 | thirtyDays = 2,
然后我尝试添加export
之前enum
:
import React, { useState } from 'react';
import Page from 'components/shared/Page';
import DashboardChart from 'components/shared/DashboardChart';
import { Col, Row, Select, Form } from 'antd';
const { Option } = Select;
export enum Periods {
twentyFourHours = 0,
oneWeek = 1,
thirtyDays = 2,
ninetyDays = 3,
oneHundredAndEightyDay = 4,
oneYear = 5
}
这“解决”了这个问题。
如果我在该enum
行上方添加评论,我们会遇到另一个问题:
import React, { useState } from 'react';
import Page from 'components/shared/Page';
import DashboardChart from 'components/shared/DashboardChart';
import { Col, Row, Select, Form } from 'antd';
const { Option } = Select;
// export is required here to prevent a syntax error with enum
export enum Periods {
这会导致另一个问题:
Line 9:13: Parsing error: Unexpected token
7 |
8 | // export is required here to prevent a syntax error with enum
> 9 | export enum Periods {
| ^
10 | twentyFourHours = 0,
11 | oneWeek = 1,
12 | thirtyDays = 2,
这是什么法术?我尝试在 VS Code 中使用 Gremlins来发现任何奇怪的字符,但没有运气。
解决方案
推荐阅读
- c++ - 关于变量的C++问题:在字符串中添加一个整数
- c# - DotNetCore 3.1 全局错误处理,未调用中间件
- mysql - 像 MySQL 和 PostgreSQL 这样的 RDB 是否为每个索引存储数据副本?还是只是带有指向真实对象的链接的 B 树?
- c# - WPF WebBrowser 无法正确呈现外部 html 内容
- oracle - 在同一个表上使用 listagg 语句更新 Oracle 表
- r - 如何在马赛克功能期间修复“光栅 IO 期间失败”?
- python - 我尝试制作一个随机引用命令,但它不起作用?
- nginx - 使用 Parallels RAS 或 NGINX 进行 kubernetes 负载平衡
- proxy - 重写维护页面apache2
- html - CSS Flexbox我们总是需要设置显示属性吗?