0

I hae a model of car imported into current component. Trying to declare an array of cars as a return type to one of the functions. But the compiler would not recognize the car array type. Can you please help ?

carcomponent.ts

     import  { Car } from '../models/car';

     export class carComponent implements onInit {
        public car : Car;

        carsavailable(): Car[] { //delcaring Car[] is not recognized.

        }

     }

car.ts .......

     export class Car {
          name: string;
     }

ERROR

The compiler underlines Car[] in red and says 'A function whose delcared type is neither 'void' nor 'any' must return a value.

Do I have to plug in car somewhere or change return type declaration way.

1
  • 1
    It does seem to recognise the type, it just bitches about your implementation Commented Feb 7, 2018 at 18:53

2 Answers 2

2

You are declaring a method with a return type so it should be returning a value

carsavailable(): Car[] { 
     return [];
}
Sign up to request clarification or add additional context in comments.

Comments

2

As you are not returning any value from the function so that compiler is showing the error

carsavailable(): Car[] { //delcaring Car[] is not recognized.
    return [];
  }

Comments

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.