Clone an ES6 Class as Another Class Name for Isolating Class Static Properties.
Run the following example by:
git clone git@github.com:zixia/clone-class.git
cd clone-class
npm install
npm run example
import * as assert from 'assert'
import {
cloneClass,
instanceToClass,
} from '../src/clone-class'
class Employee {
public static company: string
constructor(
public name: string,
) {
}
public info() {
console.log(`Employee ${this.name}, Company ${(this.constructor as any).company}`)
}
}
/**
* Example 1: `cloneClass()`
*/
const GoogleEmployee = cloneClass(Employee)
GoogleEmployee.company = 'Google'
const MicrosoftEmployee = cloneClass(Employee)
MicrosoftEmployee.company = 'Microsoft'
const employeeGg = new GoogleEmployee('Tom')
const employeeMs = new MicrosoftEmployee('Jerry')
employeeGg.info()
// Output: Employee Tom, Company Google
employeeMs.info()
// Output: Employee Jerry, Company Microsoft
/**
* Example 2: `instanceToClass()`
*/
const RestoreGoogleEmployee = instanceToClass(employeeGg, Employee)
assert(RestoreGoogleEmployee === GoogleEmployee, 'Should get back the Class which instanciated the instance)
assert(RestoreGoogleEmployee !== Employee, 'Should be different with the parent Class')
const anotherEmployee = new RestoreGoogleEmployee('Mary')
anotherEmployee.info()
// Output: Employee Mary, Company Google
The most tricky part of this code is (this.constructor as any).company
.
It will be very clear after we break down it as the following steps:
-
this.constructor
is the constructor function of the class, which shuold be the class function itself. -
company
is a static properity defined inEmployee
class, which will be set as a property on the class function. - So
this.constructor.company
is equal toEmployee.company
, except that we will not need to know the exact name of the class,Employee
in this case. We use this pattern is because we need to visit the class function even we do not know it's name.
We have two APIs for dealing with the classes:
-
cloneClass(OriginalClass)
: create a new Class that isextend
from theOriginalClass
which can isolate static properties for stored values, and return the new Class. -
instanceToClass(instance, BaseClass)
: get the Class which had instanciated theinstance
, which is theBaseClass
, or the child class ofBaseClass
, and return it.
const GoogleEmployee = cloneClass(Employee)
const instance = new GoogleEmployee()
assert(instance instanceof GoogleEmployee)
assert(instance instanceof Employee)
assert(GoogleEmployee.name === Employee.name, 'Should be the same class name')
const instance = new GoogleEmployee()
const RestoredClass = instanceToClass(instance, Employee)
assert(RestoredClass === GoogleEmployee, 'because `instance` was created by `new GoogleEmployee()`')
class PrivateConstructorClass {
private constructor() {
console.info('private constructor called')
}
}
const NewableClass = PrivateConstructorClass as any as Constructor<PrivateConstructorClass>
const instance = new NewableClass()
// Output: private constructor called
It seems useless at first, but if you want to use manage many Child Class for a Abstract Class with typings, then it will be a must have tool.
use it to check whether a class instance is:
-
instanceof
the classKlass
-
instanceof
the classKlass
's child class - the
constructor.name
is the same as the classKlass
Usage:
looseInstanceOfClass = <C extends Constructor> (Klass: C) => (target: any): target is InstanceType<C>
use it to check where a target object has all the same properties from class Klass
Usage:
interfaceOfClass = <C extends Constructor> (Klass: C) => <I extends {}> () => (target: any): target is I
- Add
interfaceOfClass()
method to check whether a object satisfy a class interface.
- ESM Support
- GitHub Action enabled
- add
looseInstanceOfClass()
method to check whether two class has the same name (See (wechaty/wechaty#2090))
- add new function:
instanceToClass()
for getting back theClass
from an existinginstance
. - add new type:
Constructor<T>
for addingnew (): T
to abstract class declaration.
First publish version.
-
cloneClass()
work as expected.
Initial version, code comes from Project Wechaty.
Learn more about the full story at Chatie blog: New Feature: Multi-Instance Support for Wechaty v0.16(WIP)
An UseCase of clone-class
can be found in an article writen by me from Chatie blog, it's also the place where this module comes from. It's Worth to spent some time to have a look if you are interested.
- New Feature: Multi-Instance Support for Wechaty v0.16(WIP)
- TypeScript 2.1 keyof and Lookup Types
- TypeScript Evolution
- TypeScript Advanced Types
- Code & Docs © 2018 Huan LI <zixia@zixia.net>
- Code released under the Apache-2.0 License
- Docs released under Creative Commons