代码之家  ›  专栏  ›  技术社区  ›  Ilja

类型脚本为排列运算符引发错误

  •  -1
  • Ilja  · 技术社区  · 7 年前

    我在react中使用了typescript,下面的文件抛出了很多错误,我不知道为什么(它作为JS工作),但我怀疑这里的某些内容与spread操作符有关?

    从'react'导入react 从“未声明”导入订阅 const getstoreasprops=(storearr)=>。{ const storeprops= storearr.map((value)=>(storeprops[value.name]=value)) 返回仓库道具 } const withstore=(…args)=>(element)=>()=>。( <subscribe to=[…args]>(…args)=><element…GetStoreAsProps(args)/></subscribe> ) 导出默认withstore

    tsc引发的错误

    workspace/app/store/index.ts:11:14-错误ts1005:应为“>”。

    11(…args)=> ~~

    workspace/app/store/index.ts:11:16-错误ts1005:应为')'。

    11(…args)=> ~

    workspace/app/store/index.ts:11:19-错误ts1109:表达式 预期。

    11(…args)=> ~~~

    workspace/app/store/index.ts:11:26-错误ts1005:“,”应输入。

    11(…args)=> ~

    工作区/应用程序/商店/索引。ts:11:30-错误ts1136:属性分配 预期。

    11(…args)=> ~

    workspace/app/store/index.ts:11:40-错误ts1005:应为“;”。

    11(…args)=> ~~

    workspace/app/store/index.ts:11:52-错误ts1005:应为“>”。

    11(…args)=> ~

    workspace/app/store/index.ts:11:80-错误ts1109:表达式 预期。

    11(…args)=> ~

    workspace/app/store/index.ts:11:81-错误ts1109:表达式 预期。

    11(…args)=> ~

    workspace/app/store/index.ts:11:83-错误ts1110:需要类型。

    11(…args)=> ~

    工作区/应用程序/商店/索引。ts:11:84-错误ts1161:未终止 正则表达式literal.

    11(…args)=>

    workspace/app/store/index.ts:12:1-错误ts1128:声明或 应输入语句。

    12)~

    如果有帮助,语法突出显示也会中断。

    .

    import React from 'react'
    import { Subscribe } from 'unstated'
    
    const getStoreAsProps = (storeArr) => {
      const storeProps = {}
      storeArr.map((value) => (storeProps[value.name] = value))
      return storeProps
    }
    
    const withStore = (...args) => (Element) => () => (
      <Subscribe to={[...args]}>{(...args) => <Element {...getStoreAsProps(args)} />}</Subscribe>
    )
    
    export default withStore
    

    引发的错误tsc

    workspace/app/store/index.ts:11:14-错误ts1005:应为“>”。

    11(…args)=> ~~

    workspace/app/store/index.ts:11:16-应为错误ts1005:“)”。

    11(…args)=> ~

    workspace/app/store/index.ts:11:19-错误ts1109:表达式 预期。

    11(…args)=> ~~~

    workspace/app/store/index.ts:11:26-错误ts1005:应为“,”。

    11(…args)=> ~

    工作区/应用程序/商店/索引。ts:11:30-错误ts1136:属性分配 预期。

    11(…args)=> ~

    workspace/app/store/index.ts:11:40-错误ts1005:应为“;”。

    11(…args)=> ~~

    workspace/app/store/index.ts:11:52-错误TS1005:应为“>”。

    11(…args)=> ~

    workspace/app/store/index.ts:11:80-错误ts1109:表达式 预期。

    11(…args)=> ~

    workspace/app/store/index.ts:11:81-错误ts1109:表达式 预期。

    11(…args)=> ~

    workspace/app/store/index.ts:11:83-错误ts1110:应为类型。

    11(…args)=> ~

    工作区/应用程序/商店/索引。ts:11:84-错误ts1161:未终止 正则表达式文本。

    11(…args)=>

    workspace/app/store/index.ts:12:1-错误ts1128:声明或 应为语句。

    12)~

    如果有帮助,语法突出显示也会中断。

    2 回复  |  直到 7 年前
        1
  •  2
  •   Estus Flask    7 年前

    这些错误意味着编译器无法识别JSX语法。为了识别它,文件应该有.tsx扩展名,而它当前的扩展名是.ts。

    jsx compiler option 也应启用并设置为 react .

        2
  •  0
  •   Readone Mohamed    7 年前

    我也有同样的问题: let temp: any = ...props.data.map((row:any) => {return row.json});

    React已经像上面一样导入:

    import * as React from "react";
    

    TSX选项也设置为在tsconfig中响应

    现在我回答我自己的问题:

    如果您正在处理一个对象,则需要使用object.assign()。

    let temp: any =  Object.assign({}, props.data.map((row:any) => {return row.json}));