React with TS 中奇怪的“解析错误:意外标记”

康格瑞姆

我有这个辅助函数,它在用 JavaScript 编写时运行良好。但是当我添加 TypeScript 时,奇怪的事情开始发生。那是一个带有函数的文件:

interface Toy {
  setId: string;
  name: string;
  year: number;
  themeId: number;
  themeName: string;
  subthemeId: number;
  subthemeName: string;
  parts: number;
  imageUrl: string;
  brandName: string;
}
enum SortBy {
  NameAsc = 'name-asc',
  NameDsc = 'name-dsc',
  PartsAsc = 'parts-asc',
  PartsDsc = 'parts-dsc',
  YearAsc = 'year-asc',
  YearDsc = 'year-dsc'
}
type Property = 'name' | 'parts' | 'year';
type Type = 'asc' | 'dsc';
type SortParams = [Property, Type];

const sortToys = (_toys: Toy[], sortBy: SortBy): any => {
  const toys = _toys.sort((toy1, toy2) => {
    const [property, type] = <SortParams>sortBy.split('-');
    const properties = {
      toy1: toy1[property],
      toy2: toy2[property]
    };
    if (properties.toy1 > properties.toy2) {
      return type === 'asc' ? 1 : -1;
    } else if (properties.toy1 < properties.toy2) {
      return type === 'asc' ? -1 : 1;
    }
    return 0;
  });
  const pages = [];
  while (toys.length) {
    pages.push(toys.splice(0, 30));
  }
  return pages;
};

export default sortToys;

这是我在尝试构建项目时遇到的错误:

src\utils\helpers\filterToys.ts
  Line 12:11:  Parsing error: Unexpected token, expected "}"      

  10 |     const [property, type] = <SortParams>sortBy.split('-');
  11 |     const properties = {
> 12 |       toy1: toy1[property],
     |           ^
  13 |       toy2: toy2[property]
  14 |     };
  15 |     if (

我知道对象本身没有问题,但可能发生了一些事情。我想不通,也许有人看到了一些奇怪的东西?

约塞连船长

出现这个错误是因为 TS 不确定你是否使用 JSX。

只是尽量避免<Type>variable符号。

如果要使用类型断言,只需使用as运算符。

interface Toy {
  setId: string;
  name: string;
  year: number;
  themeId: number;
  themeName: string;
  subthemeId: number;
  subthemeName: string;
  parts: number;
  imageUrl: string;
  brandName: string;
}
enum SortBy {
  NameAsc = 'name-asc',
  NameDsc = 'name-dsc',
  PartsAsc = 'parts-asc',
  PartsDsc = 'parts-dsc',
  YearAsc = 'year-asc',
  YearDsc = 'year-dsc'
}
type Property = 'name' | 'parts' | 'year';
type Type = 'asc' | 'dsc';
type SortParams = [Property, Type];

const sortToys = (_toys: Toy[], sortBy: SortBy): any => {
  const toys = _toys.sort((toy1, toy2) => {
    const [property, type] = sortBy.split('-') as SortParams; // fixed
    const properties = {
      toy1: toy1[property],
      toy2: toy2[property]
    };
    if (properties.toy1 > properties.toy2) {
      return type === 'asc' ? 1 : -1;
    } else if (properties.toy1 < properties.toy2) {
      return type === 'asc' ? -1 : 1;
    }
    return 0;
  });
  const pages = [];
  while (toys.length) {
    pages.push(toys.splice(0, 30));
  }
  return pages;
};

export default sortToys;

操场

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

React 组件构造函数(解析错误:意外标记,预期为“}”)

在新的 create-react-app 项目中通过“as”关键字键入断言会导致`解析错误:意外标记,预期为“;”`

JSON解析错误:意外标识符“未定义”(React Native)

ESLint:解析错误:意外标记“:”

反应:解析错误:意外标记,预期“...”

解析错误:意外标记,预期“...”

抑制:Jest 遇到来自 React Typescript 的意外标记

用于 DropdownTreeviewModule 的 webpack ts-loader 后出现意外标记“export”

react-redux connect() 和错误 TS2345

带有 React 的 Typescript - 通过 LI 元素循环的 TS 错误

在React中找不到模块TS2307

如何在 React TS 中访问嵌套的 props 对象?

如何在 React & TS 应用程序中重用类型

如何在 React 中描述 history.goBack 的 TS 接口?

如何在 usereducer react/ts 中翻转状态

@media 使用 .tsx 和 .ts 文件在一个非常奇怪的 React 实现中查询

我收到解析错误:解析错误:意外标记,预期为“;” 在反应中

“解析器错误”语法错误:位置 0 处的 JSON 中的意外标记 <

React + TypeScript:将React组件作为道具传递,使用道具进行渲染[错误:TS2339]

React TS函数参数类型

TS React:buildArray 不是函数

React TS useContext useReducer钩子

Swagger 无法解析 YAML:“语法错误:意外标记:”

Eslint:解析错误:意外标记...(分隔符 ES)

React组件中的TypeScript TS2339错误:类型“ IntrinsicAttributes ...”上不存在属性“ xyz”

映射上的TS错误:React Hooks Return中的类型不存在

使用 ts-jest 调试打字稿测试时 .tsx React 组件中的错误

如何讓 React 將 index.ts 解析為文件夾的根目錄?

vscode 中的 Python 类错误接近意外标记`('