Which is recommended to use in Typescript? A class type object or any type with many params?

Viewed 160

I am new to Typescript and Javascript. I have written the following piece of code which works fine. I want to understand which is good and recommended to use in Typescript. Let me explain a bit. When we pass more than 4 parameters lets say 8 parameters, sonar complains. So we create an object and populate all the required fields and pass to a function and we get the result. We can also define all the fields inside curly braces like this given below.

const pType: any = {firstName: "John", lastName: "Abraham"};

At the same time, we can define a class like this.

export class Person {

private _firstName: string = "";
private _lastName: string = "";

// All get set methods/functions

}

Please help me to understand the difference between the above two, which is better and why in terms of memoray usage. I have written the sample class for simplicity.

export class PassNReturnMultipleParams {
    
    public getAddress_Type1(person: Person): Address {
        console.log("First Name: ", person.firstName);
        // write logic to validate
        const address: Address = new Address();
        address.cityName = "Bangalore";
        address.flatName = "#123";
        address.streetName = "Richmond Road";
        
        return address;
    }
    
    public getAddress_Type2(pType: any): any {
        console.log("First Name: ", pType.firstName);
        // write logic to validate
        const adrsType: any = {cityName: "Bangalore", flatName: "#123", streetName: "Richmond Road"};
        return adrsType;
    }
    
    public check_Type1() {
        const person: Person = new Person();
        person.firstName = "John";
        // Set other values
        const adrs: Address = this.getAddress_Type1(person);
        console.log("Address City Name: ", adrs.cityName);
    }
    
    public check_Type2() {
        const pType: any = {firstName: "John", lastName: "Abraham"};
        // Set other values
        const adrs: any = this.getAddress_Type2(pType);
        console.log("Address City Name: ", adrs.cityName);
    }
    
}

const test: PassNReturnMultipleParams = new PassNReturnMultipleParams();
test.check_Type1();
test.check_Type2();

In the above class, there are two functions getAddress_Type1() and getAddress_Type2(), which one is always recommended in Javascript, Typescript world. Both work fine for me.

2 Answers

By my opinion, there shouldn't be usage of "any" in TypeScript. In this case I would recommend you use plain object with interface:

interface PersonType {
  firstName: string;
  lastName: string;
}

const pType: PersonType = {
  firstName: "John",
  lastName: "Abraham"
};

This question is pretty opinion-based but you need to take care of some programmatic principles.

Asking for memory efficiency between an javascript object and a class would result to a premature optimization and should be avoided.

You need to ask yourself what do you really need. In your case it will be 'something' that store data as a single instance that you can easily retrieve. If it's just about storing something that you will not frequently manipulate, then a simple object fulfilled all the requirements. Something else would be overengineerd.

Related