首页 > 解决方案 > 在 vscode 中的 redux reducer 中出现 Typescript 注释错误,即使我没有在 Typescript 中编写任何内容

问题描述

在我的 reducer 文件中,这种类型的所有声明 const newValue: action.payload.value;都带有红色下划线,并且 VSCODE 会抛出一个编译器错误,上面写着:“类型注释只能在 TypeScript 文件中使用。”

但是,我根本没有在我的 create-react-app 项目中指示或配置任何应该检查打字稿语法的东西。

这打破了我的应用程序,不知道为什么。

import * as types from "../actions/actionTypes";

const initialState = {
  firstName: "",
  lastName: "",
  userName: "",
  email: "",
  password: "",
  confirmPassword: ""
}

const userReducer = (state = initialState, action) => {
  switch(action.type) {
    case types.UPDATE_USER: {
      const newFirst: action.payload.firstName;
      const newLast: action.payload.lastName;
      const newUserName: action.payload.userName;
      const newEmail: action.payload.email;
      const newPass: action.payload.password;
      const newConfirmPass: action.payload.newPassword;
      return {
        ...state,
        firstName: newFirst,
        lastName: newLast,
        userName: newUserName,
        email: newEmail,
        password: newPass,
        confirmPassword: newConfirmPass
      }
    }
    default:
      return state;
  }
} 

export default userReducer;

VSCODE 终端错误:

Failed to compile.

./src/reducers/reducer.js
  Line 15:47:  Parsing error: Unexpected token

  13 |   switch(action.type) {
  14 |     case types.UPDATE_USER: {
> 15 |       const newFirst: action.payload.firstName;
     |                                               ^
  16 |       const newLast: action.payload.lastName;
  17 |       const newUserName: action.payload.userName;
  18 |       const newEmail: action.payload.email;

浏览器错误:

index.js:1 ./src/reducers/reducer.js
  Line 15:47:  Parsing error: Unexpected token

  13 |   switch(action.type) {
  14 |     case types.UPDATE_USER: {
> 15 |       const newFirst: action.payload.firstName;
     |                                               ^
  16 |       const newLast: action.payload.lastName;
  17 |       const newUserName: action.payload.userName;
  18 |       const newEmail: action.payload.email;

标签: reactjstypescriptreduxtypesannotations

解决方案


推荐阅读