You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

TypeScript报错TS1005: 应为'(',数组变量声明失败求助

Fixing TypeScript Error TS1005: '(' expected When Declaring an Array Variable

Let's break down why your code is triggering that error and how to fix it quickly.

The line errorList = Array<{severity: string}>; has two key issues:

  • You're missing a variable declaration keyword (let, const, or var) at the start. TypeScript requires explicit declaration for variables.
  • Array<{severity: string}> is a type annotation, not a value you can assign directly. You need to either define the variable's type and then initialize it, or let TypeScript infer the type from an initialized value.

Correct Ways to Declare Your Array

Here are a few valid options depending on your needs:

  1. Declare the type without immediate initialization

    // Using Array<T> syntax
    let errorList: Array<{severity: string}>;
    // Or the more concise T[] syntax (preferred by many developers)
    let errorList: {severity: string}[];
    
  2. Declare and initialize an empty array

    // Explicit type + empty array initialization
    let errorList: Array<{severity: string}> = [];
    // Or concise syntax with same functionality
    let errorList: {severity: string}[] = [];
    
  3. Let TypeScript infer the type (no explicit annotation needed)
    If you initialize the array with a matching structure, TypeScript will automatically detect the type for you:

    // Initialize with an example item to trigger type inference
    let errorList = [{ severity: "error" }];
    // Or initialize empty with a type assertion
    let errorList = [] as {severity: string}[];
    

Your TypeScript version (2.8.3) fully supports all these array type syntaxes, so the version isn't the issue here—it's just a syntax mistake in how you're declaring the variable.

内容的提问来源于stack exchange,提问作者Santosh Shelke

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 10:13:45