Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8672415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:11:53+00:00 2026-06-12T19:11:53+00:00

I am trying to create a helper function, to add currying, to generate common

  • 0

I am trying to create a helper function, to add currying, to generate common setups for classes. For example:

class Person {
    private name: string;
    private sex: string;

    constructor (name: string, sex: string) {
        this.name = name;
        this.sex = sex;
    }
}

var newPersonSex = function (sex: string) {
    return function (name: string) {
        return new Person(name, sex);
    }
}

var MalePerson = newPersonSex('male'); 
var FemalePerson = newPersonSex('female'); 

So that now when MalePerson and FemalePerson objects are created, it actually just returns a new instance of Person instead.

var m = new MalePerson( 'john' );

In order for the type system allow me to still use ‘new’, MalePerson needs to be a constructor type. For example: ‘new (name:string) => Person’. That also needs to be the return type of the function.

However with those declarations added, I cannot return a function from inside ‘newPersonSex’. For example this is invalid:

var newPersonSex = function (sex: string) : new(name:string) => Person {
    return function (name: string) {
        return new Person(name, sex);
    }
}

How can I create and return a new constructor function or class on the fly?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T19:11:55+00:00Added an answer on June 12, 2026 at 7:11 pm

    Try this definition of newPersonSex:

    var newPersonSex = function (sex: string) {
        return <new (name: string) => Person> <any> function (name: string) {
            return new Person(name, sex);
        }
    }
    

    The problem with your code is that functions just have call signatures, not construct signatures. My example above works around this by first asserting that the type of that function is any, and then asserting that the any type is actually a constructor.

    Note that the type of newPersonSex will be inferred from its return type so no need to add a function annotation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was trying to create this helper function in C# that returns the first
I am trying to create a helper function to read a file and mock
I am trying to create a helper function (eg in application_helper.rb ) that generates
I am trying to create my own helper function in ASP.NET MVC 3. Not
Having a problem trying to create a function, as part of a BizTalk helper
I am trying to create a little helper application, one scenario is file duplication
I'm trying to create a simple helper module in rails, and I'm stumped on
I'm trying to create a custom HTML Helper to help simplify my masterpages menu,
Trying to create Database as follows: USE Master GO IF NOT EXISTS(SELECT [Name] FROM
I'm trying to create a generic function to build a Linq expression from a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.