Search code examples
typescriptsyntaxconstants

Typescript - Missing initializer in const declaration


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);
}

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.

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


Solution

  • 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[] = [];