Skip to content

TS Proposal : abstract class expression, generic metadata, constructor typing, abstract class decorators, abstract members decorators #20887

Closed
@SalathielGenese

Description

@SalathielGenese

TypeScript Issue-Proposal

TypeScript 2.6.2

What follow are proposals for TypeScript. If follow a well know discussion, rather official #3628. It propose handling [1] abstract class decorators, abstract members decorators, generic metadata, and [2] constructor typing, and abstract class expression

[1]: Abstract class decorators, abstract members decorators, generic metadata

As to me, it is clear that by no mean should Interface exist at runtime as far as ECMA doesn't consider it worth runtime-life. However, need for this feature is more and more demanded, the reason is simple for me: more poeple are moving from JAVA to TypeScript (steping over Javascript) and so will it be with Kotlin, which they bitterly defend already: I wonder if they face the same challenging Interface DI when transpiling to Javascript.

Initial scenario

export interface CrudLike<E extends Entity>
{
    search(id: ObjectID): PromiseLike<E>;
    drop(entity: E): PromiseLike<void>;
    save(entity: E): PromiseLike<E>;
    search(): PromiseLike<E[]>;
}
public class ProductRepository implements RepositoryLike<Product>
{
    @Inject()
    public categoryCrud: CrudLike<Category>;

    @Inject()
    public productCrud: CrudLike<Product>;
}

Writing such code is very difficult, not to say impossible in typescript, at least, using the official transpiler. Keeping in mind that no interface should be decorated at the moment, my suggest should make this possible :

Scenario -001

@CustomCRUD()
export abstract class Crud<E extends Entity> implements CrudLike<E>
{}

In this case, generated metadata following @CustomCrud() should describe methods and attibutes from CrudLike which the abstract class implements.

Scenario -002

@CustomDAO()
export abstract class Dao<E extends Entity>
{
    @SqlRequest('INSERT INTO ...')
    public abstract create(entity: E): PromiseLike<E>;

    @SqlRequest('SELECT E.* FROM ...')
    public abstract search(): PromiseLike<E>;

    @SqlRequest('SELECT E.* FROM ... WHERE id = E.id')
    public abstract search(id: number): PromiseLike<E>;
}

This second senario just improves the first one, enabling us to write things like :

@DiFactory(daoFactory)
@CustomDAO()
export abstract class Dao<E extends Entity>
{
    //...
}

function daoFactory<E extends Entity, G extends {new(...args: any[]): E}>(generic: G): Dao<E>
{
    //...
    // the injector may rely on this factory to forge clean instances
    // tis factory can even rely on metadata to enforce adequate method implementations
    //...
}

// ...and later on
class UsefulClass
{
    @Inject()
    private productDao: Dao<Product>;

    @Inject()
    private categoryDao: Dao<Category>;
}

A word to conclude : the proposal

It come up that this would be possible :

  1. When abstract class will support decorators
  2. When decorator could be applied to abstract members of abstract classes
  3. When generated metadata will describe even generic types (If TS support complex generics, it means they can be modeled and represented, thus supported)

I really wonder why such constraints were first enforced !

[2]: constructor typing, and abstract class expression

TypeScript documentation proudly exhibit this fact :

One of TypeScript’s core principles is that type-checking focuses on the shape that values have. This is sometimes called “duck typing” or “structural subtyping”. In TypeScript, interfaces fill the role of naming these types, and are a powerful way of defining contracts within your code as well as contracts with code outside of your project.

Please, stop when I am wrong. This statement also implies that the following are identical :

interface NameableLike { name: string; }
// and
type NameableLike = {name: string; };

Scenario -001 : Constructor typing

class Type {}
interface TypeConstructor
{
    new(): Type;
}

function fn_001(clazz: TypeConstructor): TypeConstructor
{
    return class extends clazz {};
}

function fn_002(clazz: {new(): Type}): TypeConstructor
{
    return class extends clazz {};
}

/**
 * 
 * Tests serie #001
 * 
 */
fn_001(Type);
fn_002(Type);

/**
 * 
 * Tests serie #002
 * 
 */
class AnotherType {}
interface AnotherTypeConstructor extends TypeConstructor
{
    new(): AnotherType;
}
fn_001(AnotherType);
fn_002(AnotherType);

The decorators fn_001 and fn_002 where type enforced to work with Type and are now traped to accept even AnotherType. If one say this design is backed by no logic, I answer that typing was introduced for best practice and early violation detection. The question that springs up now is [W]hat typescript construct will ensure for sure that our constructor is for Type and Type only.

Scenario -002 : abstract class expression

The following scenario is a simplified image of my actual case, which is much more complicated.

abstract class MuchAbstraction
{
    public abstract get identifier(): string;
}

abstract class KindaLessAbstraction extends MuchAbstraction
{
    // `identifier` cannot be implemented here as it is generated at runtime
}

function fn_001(clazz: {new(): MuchAbstraction})
{
    return class extends clazz
    {
        public constructor()
        {
            super();
            // someLogic(); //...
        }
    }
}

Take note that even fn_001 SHOULD NOT implement identifier accessor as it is not its reponsibility: another decorator ensures it. As some may have seen, abstract keyword isn't allowed in class expression which is a limitation.

The following isn't a solution !

interface MuchAbstractionConstructor
{
    new(): MuchAbstraction;
}

function fn_002(clazz: MuchAbstractionConstructor)
{
    return class extends clazz // same error for abstract classes
    {
        public constructor()
        {
            super();
            // someLogic(); //...
        }
    }
}

What follow works well, BUT a 'one decorator - all purpose' isn't a great design.

function fn_003(clazz: MuchAbstractionConstructor)
{
    return class extends clazz
    {
        public constructor()
        {
            super();
            // someLogic(); //...
        }
        
        public get identifier(): string
        {
            return <any>void 0;
        }
    }
}

However, the following calls are rejected : ask me why ?

// 'typeof MuchAbstraction' is not assignable to parameter of type 'MuchAbstractionConstructor'
fn_003(MuchAbstraction); // error

// 'typeof KindaLessAbstraction' is not assignable to parameter of type 'MuchAbstractionConstructor'
fn_003(KindaLessAbstraction);  // error

In fact, none of the above fn_001 and fn_002 works when called.

function fn_004_from_fn_001(clazz: {new(): MuchAbstraction})
{}
function fn_004_from_fn_002(clazz: MuchAbstractionConstructor)
{}

fn_004_from_fn_001(MuchAbstraction); // error
fn_004_from_fn_002(MuchAbstraction); // error

However, the following works great :

class NoAbstraction extends KindaLessAbstraction
{
    public get identifier(): string
    {
        return <any>void 0;
    }
}

fn_003(NoAbstraction); // function fn_003(clazz: MuchAbstractionConstructor) { /** some logic **/ }

Conclusion: Even constructor construct to target static side doesn't accept abstract classes. Where then is the shape or duck typing proned in TypeScript documentation? Or where it does stop (I have not found in docs).

This somehow take us to the question in scenario -001 : constructor typing.

Nonetheless, I was able to come up with something that works :

function fn_004_from_fn_003(clazz: typeof MuchAbstraction)
{}

fn_004_from_fn_003(MuchAbstraction);

The following implementation leads to unsuspected results : everything works well.

function fn_005(clazz: typeof MuchAbstraction)
{
}

let a = fn_005(MuchAbstraction);
fn_005(KindaLessAbstraction);
fn_005(NoAbstraction);

This encouraged me to go a little further :

function fn_006(clazz: typeof MuchAbstraction): typeof MuchAbstraction
{
    return class extends clazz
    {
        public constructor()
        {
            super();
            // someLogic(); //...
        }
        
        public get identifier(): string
        {
            return <any>void 0;
        }
    }
}

const MyLessToNoAbstraction: typeof MuchAbstraction = fn_005(MuchAbstraction);

If, Ôh if abstract classes were allowed in class expressions, I could have wrote something like... Being confident of its validity at rutime.

function fn_007(clazz: typeof MuchAbstraction): typeof MuchAbstraction
{
    return abstract class extends clazz
    {
        public toString(): string
        {
            return `${ this.valueOf() } identified as #${ this.identifier }`;
        }
    }
}

const MyLessToNoAbstraction: typeof MuchAbstraction = fn_005(MuchAbstraction);

A word to conclude : the proposal

The proposal is as follow :

  1. Support of abstract class in class expression

  2. Better constructor typing:

    • Either by adding a core generic type. Something like what is not valid yet now: type Constructor<T> = typeof T; (because T is used here as a value !?)
    • Or by constraining that constructor interfaces to build the same type, even when extending another constructor type : this means that the extending can just overload constructor by parameters.
    class Type
    {
    }
    
    interface TypeConstructor
    {
        new(): Type;
    }
    
    class AnotherType
    {
    }
    
    interface AnotherTypeConstructor extends TypeConstructor
    {
        new(): AnotherType;  //wrong: expected Type instead of AnotherType
    }
    
    interface ProposedTypeConstrustor extends TypeConstructor
    {
        new(timestamp: number): Type; // ok (at this proposal stage)
    }
  3. By the way, it should be considered adding a name attribute to constructor types (i.e {new()...} and its interface avatar). So as to ease the following.

function fn<T, C extends {new(): T}>(clazz: C)
{
    return {[clazz.name]: class extends clazz
    {
    }}[clazz.name];
}

Summary

The proposals are summed as follows :

  1. Decorator support for abstract class
  2. Decorator support for abstract members (of abstract classes) with the third parameter being something like AbstractedPropertyDescriptor or a fourth parameters as boolean (why not? let's be crazy this far)
  3. Generate metadata that describe up to generic types
  4. Support of abstract class expression
  5. Better constructor typing:
    • Either by adding a core generic type. Something like what is not valid yet now: type Constructor<T> = typeof T; (because T is used here as a value !?)
    • Or by constraining that constructor interfaces build the same type, even when extending another constructor type
  6. Considered adding a name attribute to constructor types (i.e {new()...} and its interface avatar).
Thanks,
Eager to hear from you all,
@SalathielGenese from Squall.IO.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions