简体   繁体   English

向打字稿函数声明自定义对象的数组类型

[英]declaring an array type of custom object to a typescript function

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 carcomponent.ts

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

     export class carComponent implements onInit {
        public car : Car;

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

        }

     }

car.ts ....... 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. 编译器用红色下划线标出Car [],并说“代入类型既不是“ void”也不是“ any”的函数必须返回一个值。

Do I have to plug in car somewhere or change return type declaration way. 我是否必须在某处插入汽车或更改返回类型声明方式。

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 [];
  }

You are declaring a method with a return type so it should be returning a value 您正在声明具有返回类型的方法,因此它应该返回一个值

carsavailable(): Car[] { 
     return [];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 声明与服务TypeScript文件中声明的类型相同的数组? - Declaring array of the same type declared in a service TypeScript file? Angular:在Typescript中声明对象数组 - Angular: Declaring array of objects in Typescript 如何将从 webapi 返回的自定义对象数组的 HttpResponse 响应分配给 typescript angular 中相同数组类型的 object? - How to assign an HttpResponse response of custom array of objects returned from webapi to an object of same array type in typescript angular? 将 object 和数组作为多播类型传递到 Angular 2 的 function 参数的问题(打字稿问题) - Issue passing an object and array as a multi-cast type into a function argument for Angular 2 (Typescript Question) TypeScript类-将对象数组声明为属性 - TypeScript Classes - Declaring an Array of Objects as a Property TypeScript 在声明函数时显示错误 - TypeScript shows error when declaring a function Angular2 / TypeScript-类的类型数组的访问对象元素 - Angular2/TypeScript - Access object element of type array of class 如何使用 typescript 在 object 中声明特定类型的空数组? - How to declare a specific type of empty array inside a object using typescript? Typescript 自定义映射到 object - Typescript Custom Mapping to object 使用 Typescript 中的数组进行类型检查 - Type checking with an array in Typescript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM