Full answer
TypeScript types help while you write code, but the browser runs plain JavaScript.
An interface (or type) describes a shape. It produces no JavaScript, so there is no size cost. It matches the plain objects that JSON.parse and HttpClient return: http.get<Student[]>('/api/students'). TypeScript checks the shape in your code, using structural typing: any object with the right properties fits.
A class produces real JavaScript. Use it when you need:
- Methods on the data (
student.isPassing()). - Default values and a constructor.
instanceofchecks, or a class as a dependency injection token.- Private fields.
Important point: http.get<Student[]>() does not create Student objects. The generic only tells the compiler what you expect. If the server sends something else, there is no error at run time, and calling a method that exists only on the class would fail. If you want class instances, map the data: .pipe(map(rows => rows.map(r => new Student(r)))).
Guidance for Angular apps:
- Use interfaces or types for API data, and keep them in a models file near the feature.
- Use classes for services, components, and rich domain objects.
- Use
readonlyandReadonly<T>for data that should not change, which fits signals andOnPush. - Use union types for fixed sets:
type Status = 'pending' | 'live'. - Validate untrusted data at the boundary with a type guard or a schema library when the API is not fully trusted.
Interfaces cannot be used as injection tokens, because they do not exist at run time. Use an InjectionToken or an abstract class.
interface StudentDto {
name: string;
marks: number;
}
class Student {
readonly name: string;
readonly marks: number;
constructor(name: string, marks: number) {
this.name = name;
this.marks = marks;
}
isPassing(): boolean {
return this.marks >= 40;
}
}
const json = '[{"name":"Asha","marks":82}]';
const dtos = JSON.parse(json) as StudentDto[]; // a type claim, not a conversion
console.log('is a Student?', dtos[0] instanceof Student);
const students = dtos.map((d) => new Student(d.name, d.marks));
console.log('after mapping, passing?', students[0].isPassing());
function isStudentDto(value: unknown): value is StudentDto {
const v = value as Partial<StudentDto> | null;
return typeof v === 'object' && v !== null && typeof v.name === 'string' && typeof v.marks === 'number';
}
console.log('guard accepts bad data?', isStudentDto({ name: 5 }));TypeScript 5.8, type-checked. The script was run in Node 24 twice with identical output. It shows that parsed JSON is not a class instance until you map it.
Key points
- Interfaces are erased at compile time and suit plain JSON.
- Use classes for behaviour, defaults, `instanceof`, and DI tokens.
- `http.get<T>()` does not create instances or validate data.
- Validate untrusted API data at the boundary.
Common mistakes
- Expecting `http.get<Student>()` to return a class with methods.
- Trying to inject an interface.
- Trusting types for data that comes from outside the app.
Follow-ups
- How would you convert API JSON to class instances?
- What is structural typing?
- When would you use a type guard?
Tips
- State the erased-at-runtime fact clearly.
- Warn about the `http.get<T>` illusion.
- Mention `InjectionToken` for interfaces.
Evaluation rubric
- Explains compile time versus run time.
- Chooses interface or class with reasons.
- Knows the generic HTTP limitation.