Typescript-const声明中缺少初始化程序 [英] Typescript - Missing initializer in const declaration

查看:70
本文介绍了Typescript-const声明中缺少初始化程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下面的代码.出现错误SyntaxError:const声明中缺少初始化程序

I have below code. Getting error SyntaxError: Missing initializer in const declaration

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

如果我将声明更改为 const producer = []; 代码工作正常,但VSCode显示警告变量制造商"在某些无法确定其类型的位置中隐式地具有类型"any []".

if I change declaration to const manufacturers= []; code works fine, but VSCode shows warning "Variable 'manufacturers' implicitly has type 'any[]' in some locations where its type cannot be determined.

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

I am using node js v12.16.1 and typescript : ^2.5.3

推荐答案

您需要为制造商声明一个接口,因为如果使用任何:

You need to declare an interface for manufacturers, as TypeScript won't be able infer the properties for typechecking if you use any:

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

const manufacturers: Manufacturer[] = [];      

这篇关于Typescript-const声明中缺少初始化程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆