Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

require function parameter to implement multiple interfaces

Tags:

typescript

In typescript, is it possible to do something of the sort:

module module1 {
    export interface Foo {
        data1: string;
    }
    export interface Bar {
        data2: string;
    }
    export function foobar(data: Foo & Bar) {
        //do stuff
        data.data1; data.data2;
    }
}

Namely, force foobar's data parameter to implement both Foo and Bar? And if so, what is the correct syntax?

Thanks.

like image 616
Peter Avatar asked Jul 15 '14 18:07

Peter


1 Answers

You'd have to make a new named interface:

module module1 {
    export interface Foo {
        data1: string;
    }
    export interface Bar {
        data2: string;
    }
    export interface FooAndBar extends Foo, Bar { }
    export function foobar(data: FooAndBar) {
        //do stuff
        data.data1; data.data2;
    }
}
like image 168
Ryan Cavanaugh Avatar answered Oct 02 '22 13:10

Ryan Cavanaugh