40

In JavaScript, I can define a constructor function which can be called with or without new:

function MyClass(val) {
    if (!(this instanceof MyClass)) {
        return new MyClass(val);
    }

    this.val = val;
}

I can then construct MyClass objects using either of the following statements:

var a = new MyClass(5);
var b = MyClass(5);

I've tried to achieve a similar result using the TypeScript class below:

class MyClass {
    val: number;

    constructor(val: number) {
        if (!(this instanceof MyClass)) {
            return new MyClass(val);
        }

        this.val = val;
    }
}

But calling MyClass(5) gives me the error Value of type 'typeof MyClass' is not callable. Did you mean to include 'new'?

Is there any way I can make this pattern work in TypeScript?

dan
  • 501
  • 1
  • 4
  • 3

8 Answers8

36

What about this? Describe the desired shape of MyClass and its constructor:

interface MyClass {
  val: number;
}

interface MyClassConstructor {
  new(val: number): MyClass;  // newable
  (val: number): MyClass; // callable
}

Notice that MyClassConstructor is defined as both callable as a function and newable as a constructor. Then implement it:

const MyClass: MyClassConstructor = function(this: MyClass | void, val: number) {
  if (!(this instanceof MyClass)) {
    return new MyClass(val);
  } else {
    this!.val = val;
  }
} as MyClassConstructor;

The above works, although there are a few small wrinkles. Wrinkle one: the implementation returns MyClass | undefined, and the compiler doesn't realize that the MyClass return value corresponds to the callable function and the undefined value corresponds to the newable constructor... so it complains. Hence the as MyClassConstructor at the end. Wrinkle two: the this parameter does not currently narrow via control flow analysis, so we have to assert that this is not void when setting its val property, even though at that point we know it can't be void. So we have to use the non-null assertion operator !.

Anyway, you can verify that these work:

var a = new MyClass(5); // MyClass
var b = MyClass(5); // also MyClass

Hope that helps; good luck!


UPDATE

Caveat: as mentioned in @Paleo's answer, if your target is ES2015 or later, using class in your source will output class in your compiled JavaScript, and those require new() according to the spec. I've seen errors like TypeError: Class constructors cannot be invoked without 'new'. It is quite possible that some JavaScript engines ignore the spec and will happily accept function-style calls also. If you don't care about these caveats (e.g., your target is explicitly ES5 or you know you're going to run in one of those non-spec-compliant environments), then you definitely can force TypeScript to go along with that:

class _MyClass {
  val: number;

  constructor(val: number) {
    if (!(this instanceof MyClass)) {
      return new MyClass(val);
    }

    this.val = val;
  }
}
type MyClass = _MyClass;
const MyClass = _MyClass as typeof _MyClass & ((val: number) => MyClass)

var a = new MyClass(5); // MyClass
var b = MyClass(5); // also MyClass

In this case you've renamed MyClass out of the way to _MyClass, and defined MyClass to be both a type (the same as _MyClass) and a value (the same as the _MyClass constructor, but whose type is asserted to also be callable like a function.) This works at compile-time, as seen above. Whether your runtime is happy with it is subject to the caveats above. Personally I'd stick to the function style in my original answer since I know those are both callable and newable in es2015 and later.

Good luck again!


UPDATE 2

If you're just looking for a way of declaring the type of your bindNew() function from this answer, which takes a spec-conforming class and produces something which is both newable and callable like a function, you can do something like this:

function bindNew<C extends { new(): T }, T>(Class: C & {new (): T}): C & (() => T);
function bindNew<C extends { new(a: A): T }, A, T>(Class: C & { new(a: A): T }): C & ((a: A) => T);
function bindNew<C extends { new(a: A, b: B): T }, A, B, T>(Class: C & { new(a: A, b: B): T }): C & ((a: A, b: B) => T);
function bindNew<C extends { new(a: A, b: B, d: D): T }, A, B, D, T>(Class: C & {new (a: A, b: B, d: D): T}): C & ((a: A, b: B, d: D) => T);
function bindNew(Class: any) {
  // your implementation goes here
}

This has the effect of correctly typing this:

class _MyClass {
  val: number;

  constructor(val: number) {    
    this.val = val;
  }
}
type MyClass = _MyClass;
const MyClass = bindNew(_MyClass); 
// MyClass's type is inferred as typeof _MyClass & ((a: number)=> _MyClass)

var a = new MyClass(5); // MyClass
var b = MyClass(5); // also MyClass

But beware the the overloaded declarations for bindNew() don't work for every possible case. Specifically it works for constructors which take up to three required parameters. Constructors with optional paramaters or multiple overload signatures will probably not be properly inferred. So you might have to tweak the typings depending on use case.

Okay, hope that helps. Good luck a third time.


UPDATE 3, AUG 2018

TypeScript 3.0 introduced tuples in rest and spread positions, allowing us to easily deal with functions of an arbitrary number and type of arguments, without the above overloads and restrictions. Here's the new declaration of bindNew():

declare function bindNew<C extends { new(...args: A): T }, A extends any[], T>(
  Class: C & { new(...args: A): T }
): C & ((...args: A) => T);
jcalz
  • 264,269
  • 27
  • 359
  • 360
  • Thanks for your answer! Would it still be possible to reconcile this whith using `class` syntax and the `bindNew` function or the classy-decorator mentioned in my answere here? https://stackoverflow.com/a/48326964/738768 – kasbah Jan 22 '18 at 03:23
  • See Update 2 above (the first update was me misreading your question; the latter update addresses how to reconcile using your `bindNew()` function) – jcalz Jan 22 '18 at 14:49
  • Thanks for elaborating. I awarded you the bounty. It's probably not possible to use `instanceof MyClass` then? Also, is there really no better way to type variable numbers of arguments? On the whole this seems like much more trouble than it's worth :) – kasbah Jan 22 '18 at 15:02
  • 1
    I would think you could get `instanceof MyClass` to work, depending on the implementation of `bindNew()`, but I haven't tried it. ... Right now, TypeScript as of v2.7 lacks some type operators you'd need to do variable numbers of arguments "correctly". You can't inspect a function signature or `new()` signature to extract its argument types (likely as some sort of tuple) and return type. – jcalz Jan 22 '18 at 15:15
4

The keyword new is required for ES6 classes:

However, you can only invoke a class via new, not via a function call (Sect. 9.2.2 in the spec) [source]

Paleo
  • 21,831
  • 4
  • 65
  • 76
  • I'm having the same issue, but with declares that are describing an ES5 module similar to the above. My understanding is that the declares should be able to describe ES5 behavior, so there must be a way to do this? – Andrew Feb 07 '17 at 13:54
  • 1
    @dan's first two examples are still valid ES5 syntax (both with and without the new keyword), and Typescript declares files (https://www.typescriptlang.org/docs/handbook/declaration-files/introduction.html) are supposed to be able to describe existing libraries written in ES5, so there still must be some way to do this... – Andrew Feb 07 '17 at 15:17
  • @Andrew The ES5 code [works with TS](http://www.typescriptlang.org/play/#src=function%20MyClass%28val%29%3A%20void%20{%0A%20%20%20%20if%20%28!%28this%20instanceof%20MyClass%29%29%20{%0A%20%20%20%20%20%20%20%20return%20new%20MyClass%28val%29%3B%0A%20%20%20%20}%0A%0A%20%20%20%20this.val%20%3D%20val%3B%0A}%0A%0Avar%20a%20%3D%20new%20MyClass%285%29%3B%0Avar%20b%20%3D%20MyClass%285%29%3B). But I wouldn't know how to declare the function in a definition file. You should create a new question. Then, give the link here. ;) – Paleo Feb 07 '17 at 15:46
  • Ok... here it is: http://stackoverflow.com/questions/42095085/es5-factory-function-in-a-typescript-declares-files-without-the-new-keyword – Andrew Feb 07 '17 at 16:30
4

Solution with instanceof and extends working

The problem with most of the solution I've seen to use x = X() instead of x = new X() are:

  1. x instanceof X doesn't work
  2. class Y extends X { } doesn't work
  3. console.log(x) prints some other type than X
  4. sometimes additionally x = X() works but x = new X() doesn't
  5. sometimes it doesn't work at all when targeting modern platforms (ES6)

My solutions

TL;DR - Basic usage

Using the code below (also on GitHub - see: ts-no-new) you can write:

interface A {
  x: number;
  a(): number;
}
const A = nn(
  class A implements A {
    x: number;
    constructor() {
      this.x = 0;
    }
    a() {
      return this.x += 1;
    }
  }
);

or:

class $A {
  x: number;
  constructor() {
    this.x = 10;
  }
  a() {
    return this.x += 1;
  }
}
type A = $A;
const A = nn($A);

instead of the usual:

class A {
  x: number;
  constructor() {
    this.x = 0;
  }
  a() {
    return this.x += 1;
  }
} 

to be able to use either a = new A() or a = A() with working instanceof, extends, proper inheritance and support for modern compilation targets (some solutions only work when transpiled to ES5 or older because they rely on class translated to function which have different calling semantics).

Full examples

#1

type cA = () => A;

function nonew<X extends Function>(c: X): AI {
  return (new Proxy(c, {
    apply: (t, _, a) => new (<any>t)(...a)
  }) as any as AI);
}

interface A {
  x: number;
  a(): number;
}

const A = nonew(
  class A implements A {
    x: number;
    constructor() {
      this.x = 0;
    }
    a() {
      return this.x += 1;
    }
  }
);

interface AI {
  new (): A;
  (): A;
}

const B = nonew(
  class B extends A {
    a() {
      return this.x += 2;
    }
  }
);

#2

type NC<X> = { new (): X };
type FC<X> = { (): X };
type MC<X> = NC<X> & FC<X>;
function nn<X>(C: NC<X>): MC<X> {
  return new Proxy(C, {
    apply: (t, _, a) => new (<any>t)(...a)
  }) as MC<X>;
}

class $A {
  x: number;
  constructor() {
    this.x = 0;
  }
  a() {
    return this.x += 1;
  }
}
type A = $A;
const A: MC<A> = nn($A);
Object.defineProperty(A, 'name', { value: 'A' });

class $B extends $A {
  a() {
    return this.x += 2;
  }
}
type B = $B;
const B: MC<B> = nn($B);
Object.defineProperty(B, 'name', { value: 'B' });

#3

type NC<X> = { new (): X };
type FC<X> = { (): X };
type MC<X> = NC<X> & FC<X>;
function nn<X>(C: NC<X>): MC<X> {
  return new Proxy(C, {
    apply: (t, _, a) => new (<any>t)(...a)
  }) as MC<X>;
}

type $c = { $c: Function };

class $A {
  static $c = A;
  x: number;
  constructor() {
    this.x = 10;
    Object.defineProperty(this, 'constructor', { value: (this.constructor as any as $c).$c || this.constructor });
  }
  a() {
    return this.x += 1;
  }
}
type A = $A;
var A: MC<A> = nn($A);
$A.$c = A;
Object.defineProperty(A, 'name', { value: 'A' });

class $B extends $A {
  static $c = B;
  a() {
    return this.x += 2;
  }
}
type B = $B;
var B: MC<B> = nn($B);
$B.$c = B;
Object.defineProperty(B, 'name', { value: 'B' });

#2 simplified

type NC<X> = { new (): X };
type FC<X> = { (): X };
type MC<X> = NC<X> & FC<X>;
function nn<X>(C: NC<X>): MC<X> {
  return new Proxy(C, {
    apply: (t, _, a) => new (<any>t)(...a)
  }) as MC<X>;
}

class $A {
  x: number;
  constructor() {
    this.x = 0;
  }
  a() {
    return this.x += 1;
  }
}
type A = $A;
const A: MC<A> = nn($A);

class $B extends $A {
  a() {
    return this.x += 2;
  }
}
type B = $B;
const B: MC<B> = nn($B);

#3 simplified

type NC<X> = { new (): X };
type FC<X> = { (): X };
type MC<X> = NC<X> & FC<X>;
function nn<X>(C: NC<X>): MC<X> {
  return new Proxy(C, {
    apply: (t, _, a) => new (<any>t)(...a)
  }) as MC<X>;
}

class $A {
  x: number;
  constructor() {
    this.x = 10;
  }
  a() {
    return this.x += 1;
  }
}
type A = $A;
var A: MC<A> = nn($A);

class $B extends $A {
  a() {
    return this.x += 2;
  }
}
type B = $B;
var B: MC<B> = nn($B);

In #1 and #2:

  • instanceof works
  • extends works
  • console.log prints correctly
  • constructor property of instances point to the real constructor

In #3:

  • instanceof works
  • extends works
  • console.log prints correctly
  • constructor property of instances point to the exposed wrapper (which may be an advantage or disadvantage depending on the circumstances)

The simplified versions don't provide all meta-data for introspection if you don't need it.

See also

rsp
  • 107,747
  • 29
  • 201
  • 177
1

My workaround with a type and a function:

class _Point {
    public readonly x: number;
    public readonly y: number;

    constructor(x: number, y: number) {
        this.x = x;
        this.y = y;
    }
}
export type Point = _Point;
export function Point(x: number, y: number): Point {
    return new _Point(x, y);
}

or with an interface:

export interface Point {
    readonly x: number;
    readonly y: number;
}

class _PointImpl implements Point {
    public readonly x: number;
    public readonly y: number;

    constructor(x: number, y: number) {
        this.x = x;
        this.y = y;
    }
}

export function Point(x: number, y: number): Point {
    return new _PointImpl(x, y);
}

cuz
  • 1,172
  • 11
  • 12
1

TL;DR

If you are targeting ES6 and you really want to use class to store your data, not a function:

  • Create a function that simply invokes your class constructor with its arguments;
  • Set that function's prototype to the prototype of your class.

From now you are able to call that function either with or without new keyword to generate new class instances.

Typescript playground


Typescript provides an ability to create such a function (let's call it a "callable constructor") in a strongly typed way. Well, any type is necessary in intermediate type definitions (replacing it with unknown causes errors), but this fact will not affect your experience.

First of all we need to define basic types to describe entities we are working with:

// Let's assume "class X {}". X itself (it has type "typeof X") can be called with "new" keyword,
// thus "typeof X" extends this type
type Constructor = new(...args: Array<any>) => any;

// Extracts argument types from class constructor
type ConstructorArgs<TConstructor extends Constructor> =
    TConstructor extends new(...args: infer TArgs) => any ? TArgs : never;

// Extracts class instance type from class constructor
type ConstructorClass<TConstructor extends Constructor> =
    TConstructor extends new(...args: Array<any>) => infer TClass ? TClass : never;

// This is what we want: to be able to create new class instances
// either with or without "new" keyword
type CallableConstructor<TConstructor extends Constructor> =
  TConstructor & ((...args: ConstructorArgs<TConstructor>) => ConstructorClass<TConstructor>);

The next step is to write a function that accepts regular class constructors and creates corresponding "callable constructors".

function CreateCallableConstructor<TConstructor extends Constructor>(
    type: TConstructor
): CallableConstructor<TConstructor> {
    function createInstance(
        ...args: ConstructorArgs<TConstructor>
    ): ConstructorClass<TConstructor> {
        return new type(...args);
    }

    createInstance.prototype = type.prototype;
    return createInstance as CallableConstructor<TConstructor>;
}

Now all we have to do is to create our "callable constructor" and check it really works.

class TestClass {
  constructor(readonly property: number) { }
}

const CallableTestConstructor = CreateCallableConstructor(TestClass);

const viaCall = CallableTestConstructor(56) // inferred type is TestClass
console.log(viaCall instanceof TestClass) // true
console.log(viaCall.property) // 56

const viaNew = new CallableTestConstructor(123) // inferred type is TestClass
console.log(viaNew instanceof TestClass) // true
console.log(viaNew.property) // 123

CallableTestConstructor('wrong_arg'); // error
new CallableTestConstructor('wrong_arg'); // error
N. Kudryavtsev
  • 3,556
  • 1
  • 26
  • 30
1

I like @N. Kudryavtsev solution for creation smart instance factories (constructor wrapping with CreateCallableConstructor). But simple Reflect.construct(type, args) works perfectly if using any[] args is enough. Here is the example with mobx (v5), which shows that there is no problems with prototypes and decorators:


import { observable, reaction } from "mobx";

class TestClass {
  @observable
  stringProp: string;

  numProp: number;

  constructor(data: Partial) {
    if (data) {
      Object.assign(this, data);
    }
  }
}

var obj = Reflect.construct(TestClass, [{numProp: 123, stringProp: "foo"}]) as TestClass;
// var obj = new TestClass({numProp: 123, stringProp: "foo"});

console.log(JSON.stringify(obj));

reaction(() => obj.stringProp, v => {
    console.log(v);
  }
);

obj.stringProp = "bar";

And even this simple wrapper functions works:


type Constructor = new (...args: any[]) => any;
const createInstance = (c: Constructor, ...args) => new c(...args);
var obj = createInstance(TestClass, {numProp: 123, stringProp: "foo"});

// or
const createInstance1 = (c: Constructor) => (...args) => new c(...args);
var obj1 = createInstance(TestClass)({numProp: 123, stringProp: "foo"}, 'bla');

SalientBrain
  • 2,431
  • 16
  • 18
0

Here's how I've solved this in jest for testing groups of immutable models. The makeHash function isn't doing anything special, just a utility creating short, random strings from uuid().

The 'magic' for me was declaring type as new (...args: any[]) => any allowing it to be 'newed' as let model = new set.type(...Object.values(set.args));. So, less about getting around new and more about working in 'newable' forms.

// models/oauth.ts
export class OAuthEntity<T = string> {
  constructor(public readonly id: T) {}
  [key: string]: any;
}

export class OAuthClient extends OAuthEntity {
  /**
   * An OAuth Client
   * @param id A unique string identifying the client.
   * @param redirectUris Redirect URIs allowed for the client. Required for the authorization_code grant.
   * @param grants Grant types allowed for the client.
   * @param accessTokenLifetime Client-specific lifetime of generated access tokens in seconds.
   * @param refreshTokenLifetime Client-specific lifetime of generated refresh tokens in seconds
   * @param userId The user ID for client credential grants
   */
  constructor(
    public readonly id: string = '',
    public readonly redirectUris: string[] = [],
    public readonly grants: string[] = [],
    public readonly accessTokenLifetime: number = 0,
    public readonly refreshTokenLifetime: number = 0,
    public readonly userId?: string,
    public readonly privateKey?: string
  ) {
    super(id);
  }
}
// models/oauth.test.ts
import { makeHash, makePin } from '@vespucci/utils';
import { OAuthEntity, OAuthClient } from '@vespucci/admin/server/models/oauth';

type ModelData = { type: new (...args: any[]) => any; args: { [key: string]: any }; defs?: { [key: string]: any } };

describe('Model Tests', () => {
  const dataSet: ModelData[] = [
    { type: OAuthEntity, args: { id: makeHash() } },
    {
      type: OAuthClient,
      args: {
        id: makeHash(),
        redirectUris: [makeHash()],
        grants: [makeHash()],
        accessTokenLifetime: makePin(2),
        refreshTokenLifetime: makePin(2),
        userId: makeHash(),
        privateKey: makeHash(),
      },
    },
    {
      type: OAuthClient,
      args: {},
      defs: {
        id: '',
        redirectUris: [],
        grants: [],
        accessTokenLifetime: 0,
        refreshTokenLifetime: 0,
      },
    },
  ];
  dataSet.forEach((set) => {
    it(`Creates ${set.type.name} With ${Object.keys(set.args).length} Args As Expected`, () => {
      let model!: any;
      const checkKeys = Object.keys(set.args).concat(Object.keys(set.defs || {}).filter((k) => !(k in set.args)));
      const checkValues: any = checkKeys
        .map((key) => ({ [key]: set.args[key] || set.defs?.[key] }))
        .reduce((p, c) => ({ ...p, ...c }), {});
      expect(() => {
        model = new set.type(...Object.values(set.args));
      }).not.toThrow();
      expect(model).toBeDefined();
      checkKeys.forEach((key) => expect(model[key]).toEqual(checkValues[key]));
    });
  });
});

The end result, for me, is:

enter image description here

Gio
  • 2,242
  • 1
  • 16
  • 13
  • Where are either class constructors called without `new`? – jordanbtucker Jan 23 '22 at 20:31
  • @jordanbtucker as I shared in my answer: "less about getting around new and more about working in 'newable' forms." The OP didn't clarify why they want to avoid the `new` keyword, so I offered an alternative to explicitly newing a type and how methods can dynamically new a type without knowing the type's actual constructor. – Gio Jan 23 '22 at 21:05
0

You can use const obj = Object.create(MyClass.prototype) and then assign the values you want with Object.assign(obj, { foo: 'bar' })

This creates a class instance without using the new keyword or the constructor.

Christiaan Maks
  • 3,248
  • 3
  • 23
  • 27