在新的 create-react-app 项目中通过“as”关键字键入断言会导致“解析错误:意外的令牌,预期的“;”`
Posted
技术标签:
【中文标题】在新的 create-react-app 项目中通过“as”关键字键入断言会导致“解析错误:意外的令牌,预期的“;”`【英文标题】:Type assertion via "as" keyword in a new create-react-app project results in `Parsing error: Unexpected token, expected ";"` 【发布时间】:2021-04-03 18:41:45 【问题描述】:我通过yarn create react-app my-app --template typescript
创建了一个新的CRA 项目,运行开发服务器时出现以下错误(yarn start
):
src/App.tsx
Line 5:24: Parsing error: Unexpected token, expected ";"
3 | import "./App.css";
4 |
> 5 | const _window = window as any;
| ^
6 |
7 | const App: FC<> = () =>
8 | return <div />;
我只在 cra 服务器中得到错误,IDE Typescript 引擎认为代码没有问题。什么可能导致这种情况?
我的 package.json:
"name": "xxx",
"version": "0.1.0",
"private": true,
"dependencies":
"@emotion/react": "^11.1.4",
"@testing-library/jest-dom": "^5.11.4",
"@testing-library/react": "^11.1.0",
"@testing-library/user-event": "^12.1.10",
"@types/jest": "^26.0.15",
"@types/node": "^12.0.0",
"@types/react": "^16.9.53",
"@types/react-dom": "^16.9.8",
"@types/react-redux": "^7.1.14",
"react": "^17.0.1",
"react-dom": "^17.0.1",
"react-redux": "^7.2.2",
"react-scripts": "4.0.1",
"redux": "^4.0.5",
"redux-thunk": "^2.3.0",
"ts-action": "^11.0.0",
"ts-action-immer": "^3.0.3",
"typescript": "^4.1.0",
"web-vitals": "^0.2.4"
,
"devDependencies":
"@typescript-eslint/eslint-plugin": "^4.9.1",
"@typescript-eslint/parser": "^4.9.1",
"babel-eslint": "^10.1.0",
"eslint": "^7.15.0",
"eslint-config-airbnb": "^18.2.1",
"eslint-config-react-app": "^6.0.0",
"eslint-plugin-import": "^2.22.1",
"eslint-plugin-jsx-a11y": "^6.4.1",
"eslint-plugin-react": "^7.21.5",
"eslint-plugin-react-hooks": "^4.2.0",
"prettier": "^2.2.1"
,
"scripts":
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
,
"browserslist":
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
tsconfig.json:
"compilerOptions":
"target": "es5",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noFallthroughCasesInSwitch": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx",
"types": ["@emotion/react"]
,
"include": [
"src"
]
不确定我可以提供哪些其他有用/相关的信息,尽管 SO 迫使我在这里写一些东西让我发布...
【问题讨论】:
cra server 是什么意思? 我无法使用yarn create react-app my-app --template typescript
使用新的 TS CRA 项目重现此问题
@Michael Kurz 遇到了同样的问题。找到原因或解决办法了吗?
@floriannorbertbepunkt 我有,但你也有 :))
【参考方案1】:
在将现有 CRA 应用程序从 3.x 版更新到 4.x 版后,我遇到了同样的问题。将我的配置文件与新创建的 CRA 应用程序进行比较,我发现我的 package.json 缺少以下 eslint 配置
"eslintConfig":
"extends": [
"react-app",
"react-app/jest"
]
,
【讨论】:
根据您在此处提供的 TypeScript 类型转换答案,我希望您能帮助我解决类似的问题:***.com/q/67522592/470749 谢谢!以上是关于在新的 create-react-app 项目中通过“as”关键字键入断言会导致“解析错误:意外的令牌,预期的“;”`的主要内容,如果未能解决你的问题,请参考以下文章
基于create-react-app脚手架,按需加载antd组件以及less样式
在新门户中通过 powershell 创建 azure 存储容器
@SpringBootApplication - ComponentScan 在新的 Eclipse 项目中不能按预期工作