Reputation: 544
My goal is to use TypeScript to enhance the readability of my current code base. I've realized in many Services & Components I am ending up with a lot of code that has parameters types and return types of any
.
For example something like the code below, but with many more functions.
import { Injectable } from '@angular/core';
@Injectable()
export class ExampleService {
private someFuncA(objA: any): void {
// Do some work
}
private someFunB(objA: any): any {
// Do some work
}
private someFunC(objB: any): string {
// Do some work
}
}
The problem is objA
is a really large object that can have hundreds of properties. Sometimes those properties are present and sometimes they are not. The objects are coming from the Server and I do not have the ability to change them.
What I would like to do is change the any
types to something meaningful so the code is more easily read. For example like the following code:
import { Injectable } from '@angular/core';
@Injectable()
export class ExampleService {
private someFuncA(objA: SeismicData): void {
// Do some work
}
private someFunB(objA: SeismicData): SeismicData {
// Do some work
}
private someFunC(objB: ReflectiveData): number {
// Do some work
}
}
I know I can't use any interface, because the structure of the objects are so dynamic. Properties can exist in one instance and not in another. I am not sure if it makes sense to create a model that is just blank for example:
export class SeismicData {}
However, I do not feel that is the best/correct practice and hope that someone in the community can guide me in the right direction on how to handle a use case like this.
Upvotes: 0
Views: 5424
Reputation: 60538
If there is some set of standard properties and the issue is that some exist in some instances and others don't, then you can still use an interface:
For example:
export interface Movie {
id: number | null;
approvalRating?: number | null;
description?: string;
director?: string;
imageurl?: string;
mpaa?: string;
price?: number | null;
releaseDate?: string;
starRating?: number | null;
title?: string;
category?: string;
tags?: string[];
}
The question marks imply that the properties are optional. So in the above example the only required property is the id.
This would only make sense if there is some predefined set of properties for your object, but only some of them are present.
If you really have no idea what properties you'll get, then an interface does not make sense.
Upvotes: 1
Reputation: 101
While the type that your function returns is very important and you should use it when possible, I don't think you have to create classes just for specific types of data that can return results of type any.
It's not a problem if a class contains several functions and properties, what matters most are the concepts of cohesion and coupling. If you know your functions are relevant to the objective of the class itself, and you think that each method is unique enough to justify being on its own and not adding as part of another function, you are writing your code properly. That being said, if you want to improve readability, make the names of your variables as unique as you can,and leave comments in places where there might be confusion, and if you have to return an any type, then you have to.
Might've gone on a tangent, but hope this helped.
Upvotes: 2