Type assertion via "as" keyword in a new create-react-app project results in `Parsing error: Unexpected token, expected ";"`
Asked Answered
P

1

8

I created a new CRA project via yarn create react-app my-app --template typescript and I get the following error when running the development server (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 />;

I only get the error in the cra server, IDE Typescript engine sees no problem with the code. What could possibly cause this?

My 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"
  ]
}

Not sure what other useful/relevant info I could provide, even though SO forces me to write something here to let me post...

Palaeogene answered 26/12, 2020 at 1:57 Comment(4)
What do you mean by saying cra server?Trinitrophenol
I'm unable to reproduce this with a new TS CRA project using yarn create react-app my-app --template typescriptFotheringhay
@Michael Kurz Encoutered the same problem. Have you found the cause or a solution?Bandicoot
@floriannorbertbepunkt I have, but so have you :))Palaeogene
B
12

I had the same problem after updating an existing CRA app from version 3.x to 4.x. Comparing my config files with a freshly created CRA app, I found that my package.json was missing the following eslint config

"eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
Bandicoot answered 30/12, 2020 at 23:37 Comment(1)
Based on your helpful TypeScript type casting answer here, I'm hoping you might be able to help me with this similar question: https://mcmap.net/q/82878/-how-to-handle-eslint-error-during-type-casting-of-document-getelementbyid-method-in-typescript/470749 Thanks!Rainger

© 2022 - 2024 — McMap. All rights reserved.