-3

So I am learning how to use the factory function in javascript/typescript. I created a to-do list project where I had to pass the instance as a parameter to the function. I wanted to ask is it a bad practice to use this approach

eg

const Animal = ()=>{
    const legs = (noOfLegs:number)=>{
        console.log(noOfLegs)
    }
    return {
        legs
    }
}

const lion = Animal();

//not sure how to typeheck the instance passed to the function 
const randomFunc = (lion:ReturnType<typeof Animal>)=>{
    lion.legs(4)
}

secondly, if there is no issue with the passing instance as a parameter to the function then how type-check the parameter?

5
  • You created a factory for a legs logging function?
    – Erik Eidt
    Commented Jul 1, 2022 at 18:48
  • Ok, what happens if you want to add another function?
    – Erik Eidt
    Commented Jul 1, 2022 at 18:49
  • this is just a dummy example. I can create a function inside the animal factory and return that function similar to the legs function or if I want it to be private I will not return it and just use it inside the animal factory Commented Jul 1, 2022 at 18:51
  • 2
    Ok, how are we supposed to answer if it is bad practice if you're giving a dummy example, and no use case to go with it?
    – Erik Eidt
    Commented Jul 1, 2022 at 18:58
  • I wanted to know generally when writing javascript code using factory patterns are instances passed as arguments and for better understanding, I added the code Commented Jul 1, 2022 at 19:01

1 Answer 1

2

While you have found an approach that happens to work, it would typically be much simpler to define a class. In TypeScript, a class does two things. First, it defines a JavaScript class with a constructor and so on. Secondly, it defines an interface that we can use for typing. Then:

class Animal {
  legs(noOfLegs: number) {
    console.log(noOfLegs);
  }
}

const lion = new Animal();

const randomFunc = (lion: Animal)=>{
    lion.legs(4)
}

Alternatively, we could keep your existing function but just define an interface in addition. For example:

interface Animal {
  legs(noOfLegs: number): void;
}

const Animal = (): Animal => {
    const legs = (noOfLegs:number)=>{
        console.log(noOfLegs)
    }
    return {
        legs
    }
}

const lion = Animal();

const randomFunc = (lion: Animal)=>{
    lion.legs(4)
}

This works because values and types are in different namespaces, though in practice you might want to consider different names, for example an Animal interface but a makeAnimal function.

1
  • There's no reason to prefer a class in TypeScript as opposed to JavaScript. You are indeed correct that TypeScript automatically creates an instance type when you create a class (it also creates a type for the class itself: typeof C) but the language determines a type for every value, classes are simply a subset of this. Commented Jul 13, 2023 at 14:41

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.