Skip to content

const K = classFactory(...) should be equivalent to class K extends classFactory(...) {}Β #63727

Description

πŸ” Search Terms

class factory

Checked all opened issues and the 5 first pages of the closed ones. Also searched for existing issues with IA.

βœ… Viability Checklist

⭐ Suggestion

Playground Link

Currently, if we have a class factory:

function classFactory<T extends Record<string, any>>(props: T) {
    class Foo {
        protected foo(){} // needs to disable declaration files.
    }

    return Foo as {new(): Foo&T};
}

And we assign the result into a constant variable:

const Klass = classFactory({a: 34})

Then, not only K can't be used as a type, but its type description is ugly:

const Klass: new () => classFactory<{
    a: number;
}>.Foo & {
    a: number;
}

Therefore, I'd like it to be equivalent to class Klass extends classFactory({a: 34}) {}, i.e. to produce a class Klass type.

πŸ“ƒ Motivating Example

The type description would be cleaner class Klass instead of:

const Klass: new () => classFactory<{
    a: number;
}>.Foo & {
    a: number;
}

Also, we will be able to use it as a type which could be quite useful. For exemple we would then be able to export default the generated class with its type.

πŸ’» Use Cases

Currently there are 2 workarounds:

1/ Using class Klass extends classFactory({foo: 34}) {}. However this creates an additional class and is more verbose to write.

2/ Re-creating the type. However dynamics and protected properties cause issues. This require to duplicate information, and likely require unsafe type casting of the factory function result.

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