12

I have two classes that inherit from the same superclass:

class Vehicle {}

class Bus extends Vehicle {}

class Truck extends Vehicle {}

Let's have two typed arrays:

var buses : Bus[];
var trucks : Truck[];

and a function that accepts an array of the superclass type.

function checkOil(vehicles : Vehicle[]) {}

I can pass in array of busses or array of trucks but I can not merge them and pass them together:

function checkOil(buses.concat(trucks));


//error TS2082: Supplied parameters do not match any signature of call target:
    Types of property 'pop' of types 'Bus[]' and 'Track[]' are incompatible:

How do I merge those arrays?

EDIT: TypeScript Playground

2
  • You could copy them both into a sufficiently large array of Vehicles. Why do you want to do that? Commented Jul 25, 2014 at 15:46
  • 1
    Concat to array of Vehicle won't work. Copy via for loop will but that is suboptimal, that's why I seek for better solution here. Commented Jul 25, 2014 at 16:00

2 Answers 2

14

The casting to <Vehicle[]> should work

function checkOil(vehicles : Vehicle[]) {}

checkOil((<Vehicle[]>buses).concat(trucks));

Typescript will cast the (busses) to Vehicle[], and the same will be done with the rest

e.g. this will return (in console) two objects - Vehicles

class Vehicle
{
    public Type: string;
}
class Bus extends Vehicle
{
    public A: string;
}
class Truck extends Vehicle
{
    public B: number
}

var buses: Bus[] = [];
buses.push({Type: 'Bus', A : 'A1'});
var trucks: Truck[] = [];
trucks.push({ Type: 'Truck', B: 1 });

function checkOil(vehicles: Vehicle[]) : Vehicle[]
{
    return vehicles;
}
var result = checkOil((<Vehicle[]>buses).concat(trucks));
console.log(result)
Sign up to request clarification or add additional context in comments.

6 Comments

Well, the above code snippet is working... am I missing some part of your question?
If you use actually empty classes, you'll basically never get errors. Using empty types to experiment with a structural type system is a recipe for bad results.
it works as long as they have equal signatures (no methods == equal signatures). Try to add 'class Bus extends Vehicle { foo() {} }' and you will get "Type '{}' is missing property 'foo' from type 'Bus'."
Sorry guys, I did it in a hurry, ... now the example is working properly... but the concept of casting (assert in Typescript) is the way...
Please, take my updated snippet WILL Work ;) sorry for the fast... but not so accurate answer ;) now it works
|
4

Just type assert the first array to a common type of the two array types:

checkOil((<Vehicle[]>buses).concat(trucks));

5 Comments

Thanks for your comment, you are right. The Assert is the way
Sorry guys but that works only as far as the classes signatures are empty. Add a method and you get the same error: typescriptlang.org/Playground
Those parentheses made the difference :). Thanks
if you edit your answer I can give you the upvote you deserve :)
The answer has the correct parentheses. The link you posted does not. I'm not sure what you want me to edit.

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.