2

I suspect I have made a mistake, but I can't get the following to pass type checking. I end up with Property 'map' does not exist for type 'Restos'. I have set up the interface according to the TS tutorials, so it feels as though map is not included as default (I have "target": "es6" in tsconfig)

interface Resto {
    rname:String,
    qname:String,
    tel:String
}

interface Restos {
    [index:number]:Resto;
}

class MainController {
    headline: String;
    rnames: [String];

    constructor($http : ng.IHttpService) {
        this.headline = "hello world";

        $http.get('http://afbackend.herokuapp.com/api/restos')
        .success( (res:Restos) => {
            console.log(res);
            this.rnames = res.map(r => r.rname)
                              ^^^ 
        });
    }
}
1
  • In your interface for Restos where is the map method? Maybe just use Resto[] instead of Restos. Commented Aug 10, 2015 at 18:36

1 Answer 1

4

The only thing the compiler knows is true about Restos is that when you index it by a number, you get a Resto. Just because something has a numeric index signature doesn't mean it's an array (for example, the object {0: 'hello', 1: 'world'} can be indexed by numbers to produce strings, but you can't call .map on it).

What you probably want to write is type Restos = Array<Resto> instead of the interface.

Sign up to request clarification or add additional context in comments.

1 Comment

Cheers - I was making it too complex with Interface, type does it. And type Restos = Resto[] worked too. (Yes, I know [String] was wrong too). In my defence I would note that the type syntax is not mentioned on typescriptlang.org/Handbook

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.