I've reading up on interfaces and type assertion. Been looking at pages like:
- Typescript parse json with class and interface
- How do I cast a json object to a typescript class (this one has nothing to do with TS!)
- Parse complex json objects with Typescript
- TS doc about interfaces
I'm getting the hang of it, and it's pretty straightforward in understanding the basics. Though: Nowhere do I find how to type a JSON object to a object I use in my app, when HTTP is involved.
For example, I have an Array of Teacher-objects. Each Teacher has an id, name and an Array of Students. I have another class Students that contain their attributes..... etc.
Somewhere in those links I read that, unless you need to perform actions to an object, it's enough to have just an Interface. But unless you want to do actions to the Object you need a separate class?
My actual Teacher class begins with... :
export class Teacher {
    private students: Array<Student>;
    constructor(public id: string, public name: string) {
        this.students = new Array<Student>();
    }
    public getStudents(): Array<Student> {
        return this.students;
    }
}
First of all, how would the code look like if I want to cast (or assert the type) the JS object to a Teacher object?
Now my code looks something like this:
Service:
getTeachers() {
    return this.http.get('someUrl')
    .map((res: Response) => res.json())
}
Component (Angular 2 Component):
export class ListComponent implements OnActivate {
id: string;
name: string;
teachers: Teacher[];
constructor(public _service: Service, public _router: Router) {
}
routerOnActivate(): void {
    this._service.getTeachers()
        .subscribe(teachers => this.teachers = teachers);
}
My interface would look like:
export interface TeacherJSON {
        id: string,
        name: string,
        students: Array<Student>;
}
If the the above interface is not enough to be able to perform actions on the object, how do I move forward? I did learn that you can add methods to your interface like:
interface Thing {
    a: number;
    b: string;
    foo(s: string, n: number): string;
}
process(x: Thing) {
    return x.foo("abc");
}
I do understand the above example, but I guess the http, mapping and subscribing is throwing me off, and I don't know how to implement it in my code!
 
     
     
     
    