.NET Tips and Tricks

Blog archive

Create Factory Functions in TypeScript for Creating Multiple Classes

In TypeScript, you can define a function that, when passed a set of parameters, creates and returns a correctly configured object. One of the parameters passed to the function must be the class of the object being created (I'll call that the "class parameter"). Normally, that means your factory function can only create a single kind of class but, by leveraging generics and interfaces, you can create functions that will create and configure a variety of classes.

Your first step is to define an interface that specifies the properties that your factory function will configure. Here's an example of an interface that specifies two properties:

interface ICustomer
{
  name: string;
  age: number;
}

With that interface in place you can create a generic factory function that works with any class that implements that interface. First, when defining your factory function, follow the factory function's name with a data type marker and specify that the marker extends the interface. Second, in the factory function's parameter list, use TypeScript's new keyword along with the generic data type marker when defining the function's class parameter (you're actually specifying the type of the class' constructor).

The following code shows the declaration of a generic factory function that leverages my ICustomer interface. I first provide a type marker (c) that, using the extend keyword, ties the function to classes that implement the ICustomer interface. I then use that type marker with the new keyword to specify the return type of the function's class parameter (called cust in my example):

function CreateCustomer<c extends ICustomer>(cust:{new(): c;}, 
                                             name: string, age: number): c
{

Now, inside my function, I use the class parameter to instantiate the class passed in the class parameter. Once I've done that, I can configure the object by setting the properties specified in the interface:

    var newCust: c;
    newCust = new cust();
    newCust.name = name;
    newCust.age = age;
    return newCust;
}

To use this factory, I pass the class I want created (which must implement the ICustomer interface) along with the rest of the parameters required by my factory function. This example:

var cust: Customer;
cust = CreateCustomer(Customer, "Peter", 62);

uses my function to create an instance of a class called Customer.

Posted by Peter Vogel on 12/28/2015


comments powered by Disqus

Featured

  • Vibe Coding Pioneer Advises 'Tight Leash' to Rein In AI BS

    Andrej Karpathy, who started the whole "vibe coding" thing to describe AI-drive software development, is now warning developers to keep tight control of their "new over-eager junior intern savant with encyclopedic knowledge of software, but who also bull* you all the time, has an over-abundance of courage and shows little to no taste for good code."

  • Microsoft Unifies Cloud and AI Development with .NET Aspire and AI Template Updates

    Microsoft announced the availability of .NET Aspire 9.2 and the second preview of the .NET AI Chat Web App template, highlighting new capabilities that bring cloud-native orchestration and AI integration closer together in the .NET ecosystem.

  • Hands On with Microsoft's AI Playground in a Windows App

    Microsoft reintroduced the AI Dev Gallery, a Windows application that serves as a comprehensive playground for AI development using .NET. It simplifies AI development with .NET through interactive samples, easy model downloads, and exportable source code.

  • Vibe Coding Advances in Visual Studio 2022 Previews

    Preview 3 features next edit suggestions -- or tab, tab, tab coding -- following previous previews of GPT-4o code completions and assorted Copilot enhancements.

  • Mastering Modern JavaScript

    Master the latest features of modern JavaScript in a hands-on workshop covering clean code techniques, async programming, templating, and full-stack development with Node.js.

Subscribe on YouTube

Upcoming Training Events

0 AM
Visual Studio Live! San Diego
September 8-12, 2025
Live! 360 Orlando
November 16-21, 2025
Cloud & Containers Live! Orlando
November 16-21, 2025
Data Platform Live! Orlando
November 16-21, 2025
Visual Studio Live! Orlando
November 16-21, 2025