3

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

1
  • Can't reproduce this error on TypeScript versions going back as far as 2.4.1. Commented May 7, 2020 at 5:52

1 Answer 1

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[] = [];      
Sign up to request clarification or add additional context in comments.

5 Comments

Why need to declare interface ? I need an array of type Any. const manufacturers: any[] = [ {id =101 , name = 'test 1'}, {id =102 , name = 'test 2'}];
Either you use an interface, or you declare it with any. I highly recommend interface because TypeScript needs it for typechecking.
@RajeshreeNevare hey, did my answer solve your issue?
Yes, Declaring interface will work. But any is not working. I have suppressed strict type checking from tsconfig.json file
Yeahh good call. You should limit the use of any due to my above point

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.