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, orvar) 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:
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}[];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}[] = [];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
相关产品推荐
相关产品推荐

