代码之家  ›  专栏  ›  技术社区  ›  Robert Hovhannisyan

Const声明需要初始化值

  •  1
  • Robert Hovhannisyan  · 技术社区  · 4 年前

    我想将TypeScript添加到我的项目中,向数组中添加类型时出现以下错误:

    SyntaxError:
    my/path/of/file:
    Const declarations require an initialization value (2:28)
    
      1 | //keycodes of restricted characters for all our amount inputs    
      2 | export const restrictedChars: number[] = [ 43, 45, 69, 107, 109, 187, 188, 189, 190 ];         
    

    代码如下:

    //keycodes of restricted characters for all our amount inputs
    export const restrictedChars: number[] = [ 43, 45, 69, 107, 109, 187, 188, 189, 190 ];
    

    我试图声明数组,然后导出,但没有成功。此外,我还尝试为该数组创建一个接口,如一些解决此类问题的解决方案中所述,但没有太大帮助。

    const path = require("path");
    const webpack = require("webpack");
    
    module.exports = {
      entry: "./src/index.tsx",
      mode: "development",
      module: {
        rules: [
          {
            test: /\.(js|jsx|ts|tsx)$/,
            exclude: /(node_modules|bower_components)/,
            loader: "babel-loader",
            options: { presets: ["@babel/env"] }
          },
          {
            test: /\.css$/,
            use: ["style-loader", "css-loader"]
          }
        ]
      },
      resolve: { extensions: ["*", ".js", ".jsx", ".ts", ".tsx"] },
      output: {
        path: path.resolve(__dirname, "dist/"),
        publicPath: "/dist/",
        filename: "bundle.js"
      },
      devServer: {
        contentBase: path.join(__dirname, "public/"),
        port: 3000,
        publicPath: "http://localhost:3000/dist/",
        historyApiFallback: true,
        hot: true
      },
      plugins: [new webpack.HotModuleReplacementPlugin()]
    };
    

    包.json

    {
      "name": "WhatToCook",
      "version": "1.0.0",
      "description": "",
      "main": "./src/index.tsx",
      "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1",
        "start": "webpack-dev-server --mode development --open --hot"
      },
      "keywords": [],
      "author": "",
      "license": "ISC",
      "devDependencies": {
        "@babel/cli": "^7.12.13",
        "@babel/core": "^7.12.13",
        "@babel/plugin-proposal-class-properties": "^7.12.13",
        "@babel/preset-env": "^7.12.13",
        "@babel/preset-react": "^7.12.13",
        "@types/react-dom": "^17.0.3",
        "@types/react-redux": "^7.1.16",
        "@types/react-router-dom": "^5.1.7",
        "babel-loader": "^8.2.2",
        "css-loader": "^1.0.1",
        "style-loader": "^0.23.1",
        "webpack": "^4.46.0",
        "webpack-cli": "^3.3.12",
        "webpack-dev-server": "^3.11.2"
      },
      "dependencies": {
        "lodash": "^4.17.20",
        "react": "^17.0.1",
        "react-addons-update": "^15.6.3",
        "react-dom": "^17.0.1",
        "react-hot-loader": "^4.13.0",
        "react-jss": "^10.5.1",
        "react-redux": "^7.2.2",
        "react-router-dom": "^5.2.0",
        "redux": "^4.0.5",
        "redux-devtools-extension": "^2.13.8",
        "redux-thunk": "^2.3.0"
      }
    }
    
    2 回复  |  直到 4 年前
        1
  •  2
  •   Oleksandr K    4 年前

    尝试在webpack配置中将ts loader放在js规则之前

          {
            test: /\.(ts|tsx)$/,
            loader: 'ts-loader',
            // include: [path.resolve(__dirname, 'yourAppPath')],
            exclude: [/node_modules/, /bower_components/],
          },
          {
            test: /\.(js|jsx)$/,
    ...
    
        2
  •  1
  •   T.J. Crowder    4 年前

    听起来好像这个代码没有被TypeScript处理。如果JavaScript引擎直接处理它,就会出现这样的错误:

    /* export */ const restrictedChars: number[] = [ 43, 45, 69, 107, 109, 187, 188, 189, 190 ];

    确保这是在一个 .ts .tsx 文件,不是 .js .jsx 文件,并且您的项目设置为让TypeScript编译 将文件转换为JavaScript。


    restrictedChars . TypeScript将正确推断类型 number[] 来自初始值设定项。(TypeScript在推断类型方面非常聪明。)

    推荐文章