打字稿:意外令牌`接口' w/ react js

发布于 2025-01-27 06:37:47 字数 2033 浏览 2 评论 0原文

  1. 在我在本地主机上运行dev之后,我会发现错误: x意外令牌接口。期待此,导入,异步,函数,[对于数组字面,{对于object triletal, @ for Decorator,function,class,class,null,null,true,false,number,bigint,bigint,string,string,string, | regexp,`对于模板字面的,(或标识符

这是代码的摘要:

interface SidebarProps {
      initialSelectedIcon: string
    }
    
    function Sidebar({ initialSelectedIcon }: SidebarProps) {
      const [selected, setSelected] = useState<String>(initialSelectedIcon)
      const { currentAccount, currentUser } = useContext(TwitterContext)
      const router = useRouter()
  1. 终端中没有问题

  2. 我没有问题,我将一堆JS文件转换为tsx

  3. 这是我的tsconfig.json

      {
         “ compileroptions”:{
         “目标”:“ ES6”,
         “ lib”:[“ dom”,“ dom.iterable”,“ esnext”],
         “允许J”:是的,
         “ Skiplibcheck”:是的,
         “严格”:是的,
         “ forceconsistentcasinginfilenames”:是的,
         “ noemit”:是的,
         “ eSmoduleInterop”:true,
         “模块”:“ commonjs”,
         “ Moduleresolution”:“ Node”,
         “ resolvejsonmodule”:是的,
         “孤立模块”:是的,
         “ JSX”:“ Preserve”,
         “增量”:正确
       },,
        “ inclage”:[“ next-env.d.ts”,“ **/*。ts”,“ **/*。tsx”, 
        “页面/组件/2_sidebar.tsx”],
        “排除”:[“ node_modules”]
     }
     
  4. 下面是我的软件包

     “脚本”:{
         “构建”:“ rimraf ./build&amp;&amp; tsc”,
         “ dev”:“ nodemon”,
         “ start”:“ npm运行构建&amp;&amp; node build/index.tsx”
       },,
       “关键字”:[
         “理智”
       ],,
       “依赖关系”:{
         “@Sanity/Base”:“^2.29.5”,
         “@Sanity/Core”:“^2.29.5”,
         “@sanity/default-layout”:“^2.29.5”,
         “@sanity/default-login”:“^2.29.5”,
         “@Sanity/Desk-Tool”:“^2.29.5”,
         “@sanity/eslint-config-studio”:“^2.0.0”,
         “@Sanity/Vision”:“^2.29.5”,
         “ eslint”:“^8.6.0”,
         “ prop-types”:“^15.7”,
         “反应”:“^17.0”,
         “ react-dom”:“^17.0”,
         “样式组件”:“^5.2.0”
       },,
       “ devDectiencies”:{
         “@type/node”:“^17.0.21”,
         “ nodemon”:“^2.0.15”,
         “ rimraf”:“^3.0.0”,
         “ TS节点”:“^10.4.0”,
         “打字稿”:“^4.6.2”
       }
     }
     
  1. After I npm run dev on local host I get the error:
    x Unexpected token interface. Expected this, import, async, function, [ for array literal, { for object literal, @ for decorator, function, class, null, true, false, number, bigint, string,
    | regexp, ` for template literal, (, or an identifier

This is a snippet of the code:

interface SidebarProps {
      initialSelectedIcon: string
    }
    
    function Sidebar({ initialSelectedIcon }: SidebarProps) {
      const [selected, setSelected] = useState<String>(initialSelectedIcon)
      const { currentAccount, currentUser } = useContext(TwitterContext)
      const router = useRouter()
  1. There are no problems in the Terminal

  2. I converted a bunch of js files to tsx

  3. Here is my tsconfig.json

        {
         "compilerOptions": {
         "target": "es6",
         "lib": ["dom", "dom.iterable", "esnext"],
         "allowJs": true,
         "skipLibCheck": true,
         "strict": true,
         "forceConsistentCasingInFileNames": true,
         "noEmit": true,
         "esModuleInterop": true,
         "module": "commonjs",
         "moduleResolution": "node",
         "resolveJsonModule": true,
         "isolatedModules": true,
         "jsx": "preserve",
         "incremental": true
       },
        "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", 
        "pages/components/2_Sidebar.tsx"],
        "exclude": ["node_modules"]
     }
    
  4. Below is my Package.json

       "scripts": {
         "build": "rimraf ./build && tsc",
         "dev": "nodemon",
         "start": "npm run build && node build/index.tsx"
       },
       "keywords": [
         "sanity"
       ],
       "dependencies": {
         "@sanity/base": "^2.29.5",
         "@sanity/core": "^2.29.5",
         "@sanity/default-layout": "^2.29.5",
         "@sanity/default-login": "^2.29.5",
         "@sanity/desk-tool": "^2.29.5",
         "@sanity/eslint-config-studio": "^2.0.0",
         "@sanity/vision": "^2.29.5",
         "eslint": "^8.6.0",
         "prop-types": "^15.7",
         "react": "^17.0",
         "react-dom": "^17.0",
         "styled-components": "^5.2.0"
       },
       "devDependencies": {
         "@types/node": "^17.0.21",
         "nodemon": "^2.0.15",
         "rimraf": "^3.0.0",
         "ts-node": "^10.4.0",
         "typescript": "^4.6.2"
       }
     }
    
  5. I think there might be a problem with the tsconfig.json or package.json

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文