In our development environment, we have a unique "framework" that creates ES5 "classes" in a specific way:
var MyClass = ourClass({
extends: Base,
constructor: function MyClass(...){...}
static: {},
...
})
// later somewhere
var myClassInstance = new MyClass(...);
This process involves enhancing the returned constructor with static properties and the inheritance chain of the constructor functions.
For example, in a hierarchy like Base -> Specialized -> EvenMoreSpecialized,
EvenMoreSpecialized.staticChain === [EvenMoreSpecialized, Specialized, Base, Object]
. If we have an instance theInstance
of EvenMoreSpecialized
, calling theInstance.static('aField')
retrieves the first 'aField' property defined in the inherited constructor functions.
I am exploring options to migrate to TS/ES6 classes without compromising the existing functionality of the static() feature. The challenge lies in integrating this feature smoothly into TypeScript while preserving its current capabilities.
The proposed signature for the static() function would be:
static(identifier:string):any
Is there a way to catch misspelled identifiers at compile time? Is it possible to narrow down the return type from any
for better type safety?
Here's a more detailed example:
var Base = ourClass({
extends: Object,
constructor: function Base(){},
static: {
secret: null
}
})
var Specialized = ourClass({
extends: Base,
constructor: function Specialized() {};
static: {
secret: '12345'
});
var EvenMoreSpecialized = ourClass({
extends: EvenMoreSpecialized,
static: {
secret: '23456'
}
});
var base = new Base();
// base.constructor.staticChain === [Base, Object];
base.static('secret'); // => null; Base.secret
var specialized = new Specialized();
// specialized.constructor.staticChain === [Specialized, Base, Object]
specialized.static('secret'); // => '12345'; Specialized.secret
var even = new EvenMoreSpecialized();
// even.constructor.staticChain === [EvenMoreSpecialized, Specialized, Base, Object]
even.static('secret'); // => '23456'; EvenMoreSpecialized.secret