Typescript - const 声明中缺少初始值设定项

问题描述 投票:0回答:2

我有以下代码。收到错误 SyntaxError:const 声明中缺少初始值设定项

const manufacturers: any[] = [];        
console.log('Available Products are: ');
for (const item of manufacturers) {
     console.log(item.id);
}

如果我将声明更改为

const manufacturers= [];
代码工作正常,但 VSCode 显示警告 “变量‘制造商’在某些无法确定其类型的位置隐式具有类型‘any[]’。

我正在使用节点 js v12.16.1 和打字稿:^2.5.3

typescript syntax constants
2个回答
3
投票

您需要为

manufacturers
声明一个接口,因为如果您使用
any
,TypeScript 将无法推断类型检查的属性:

interface Manufacturer {
  id: string;
  // add other properties
}

const manufacturers: Manufacturer[] = [];      

0
投票

@wentjun .. 我面临着类似的类型问题。它不是一个接口,它的类型

© www.soinside.com 2019 - 2024. All rights reserved.