JSPM

class-without-call-parent-constructor

2.0.5
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 439
  • Score
    100M100P100Q96019F
  • License MIT

create class skip call parent constructor

Package Exports

  • class-without-call-parent-constructor
  • class-without-call-parent-constructor/package.json
  • class-without-call-parent-constructor/src/index.ts

Readme

import classWithoutCallParentConstructor from 'class-without-call-parent-constructor';

describe(`describe`, () =>
{

    class A
    {
        constructor(a: any, b: any)
        {
            console.log("A.constructor");
            throw new Error('should not call A.constructor')
            a;
            b;
        }

        parentMethod()
        {
            console.log("A.parentMethod()");

            return "A.parentMethod()"
        }
    }

    test(`@ts-expect-error`, () =>
    {

        class B extends A
        {
            // @ts-expect-error
            constructor()
            {

            }
        }

        // @ts-ignore
        expect(() => new A).toThrowError();
        expect(() => new B).toThrowError();

    });

    test(`classWithoutCallParentConstructor`, () =>
    {
        class B extends classWithoutCallParentConstructor(A)
        {
            constructor()
            {
                super();
            }

            childMethod()
            {
                console.log("B.childMethod()");
                return "B.childMethod()"
            }
        }

        expect(() => new B).not.toThrowError();

        let actual = new B;

        expect(actual).toHaveProperty('parentMethod')
        expect(actual).toHaveProperty('childMethod')

        expect(actual.parentMethod()).toStrictEqual('A.parentMethod()')
        expect(actual.childMethod()).toStrictEqual('B.childMethod()')

    });

    test(`createNewTargetObject`, () =>
    {
        class B extends A
        {
            // @ts-ignore
            constructor()
            {
                return createNewTargetObject(new.target)
            }

            childMethod()
            {
                console.log("B.childMethod()");
                return "B.childMethod()"
            }
        }

        expect(() => new B).not.toThrowError();

        let actual = new B;

        expect(actual).toHaveProperty('parentMethod')
        expect(actual).toHaveProperty('childMethod')

        expect(actual.parentMethod()).toStrictEqual('A.parentMethod()')
        expect(actual.childMethod()).toStrictEqual('B.childMethod()')

    });

})