简体   繁体   English

打字稿错误意外的令牌,预期为“}”

[英]Typescript error Unexpected token, expected “}”

I recently started with typescript and I'm getting a error I have no idea how to fix. 我最近开始使用打字稿,但遇到错误,我不知道如何解决。 Here's my function: 这是我的功能:

interface Arr<E extends Runtype> extends Runtype<Static<E>[]> {
  tag: 'array';
  element: E;
}

/**
 * Construct an array runtype from a runtype for its elements.
 */
function Arr<E extends Runtype>(element: E): Arr<E> {
  return create<Arr<E>>(
    xs => {
      if (!Array.isArray(xs)) throw new ValidationError(`Expected array, but was ${typeof xs}`);
      let newArray:Array<E> = new Array();
      for (const x of Array.from(xs)) {
        try {
            newArray.push(<E>element.check(x));

        } catch ({ message, key }) {
          throw new ValidationError(
            message,
            key ? `[${xs.indexOf(x)}].${key}` : `[${xs.indexOf(x)}]`,
          )
        }
      }

      return newArray;
    },
    { tag: 'array', element },
  )
}

export { Arr as Array };

Here's an image of my error: 这是我的错误的图片: 在此处输入图片说明

Tsconfig: tsconfig:

{
  "compilerOptions": {
    "allowJs": true,
    "allowSyntheticDefaultImports": true,
    "jsx": "preserve",
    "lib": ["dom", "es2017"],
    "module": "esnext",
    "moduleResolution": "node",
    "noEmit": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "preserveConstEnums": true,
    "removeComments": false,
    "skipLibCheck": true,
    "sourceMap": true,
    "strict": true,
    "target": "esnext"
  }
}

Would really appreciate some help on what's going wrong here.... I have tried switching the target environment and stuff 非常感谢您对这里出了什么问题有所帮助。...我试图切换目标环境和内容

Update: 更新:

Another similar error: 另一个类似的错误:

在此处输入图片说明

export interface Record<O extends { [_: string]: Runtype }>
  extends Runtype<{ [K in keyof O]: Static<O[K]> }> {
  tag: 'record';
  fields: O;
}

/**
 * Construct a record runtype from runtypes for its values.
 */
export function Record<O extends { [_: string]: Runtype }>(fields: O) {
  return create<Record<O>>(
    x => {
      if (x === null || x === undefined) {
        const a = create<any>(x, { tag: 'record', fields });
        throw new ValidationError(`Expected ${show(a)}, but was ${x}`);
      }
      let y:any = {};
      // tslint:disable-next-line:forin
      for (const key in fields) {
        try {
            const keyStr:string = <string>key;


          let val = fields[key].check(hasKey(key, x) ? x[key] : undefined);
          y[<string>key] = <any>val;
        } catch ({ key: nestedKey, message }) {
            console.log('key ',key,' ',message);
          throw new ValidationError(message, nestedKey ? `${key}.${nestedKey}` : key);
        }
      }

      return y as O;
    },
    { tag: 'record', fields },
  );
}

Anyone got a clue what those examples got wrong that makes typescript react to this? 任何人都知道这些示例出了什么问题,使打字稿对此有所反应?

Can you try changing this 你能尝试改变这个吗

newArray.push(<E>element.check(x));

with

newArray.push(<E>element.check(x)</E>);

As I don't have your full application, I'm not sure this will 100% work. 由于我没有您的完整申请,因此我不确定这将100%有效。 But this change solved many parenthesizations for me. 但是此更改为我解决了许多括号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM