Skip to content

Use typeof with generic #20719

Closed
Closed
@donaldpipowitch

Description

@donaldpipowitch

TypeScript Version: Current playground version (?)

Code

class GenericGroup<T> {
  items: Array<T> = [];
  constructor(name: string) {}
}

type CheckboxValues = string;
type CheckboxesGroup = new (name: string) => GenericGroup<CheckboxValues>;
const Checkboxes: CheckboxesGroup = GenericGroup;
const checkboxes = new Checkboxes('checkboxes');
checkboxes.items.map(item => item.toUpperCase());


type RadioValues = string;
type RadiosGroup = typeof GenericGroup<RadioValues>;
const Radios: RadiosGroup = GenericGroup;
const radios = new Radios('radios');
radios.items.map(item => item.toUpperCase());

link

Expected behavior:

typeof GenericGroup<RadioValues>; is equivalent to new (name: string) => GenericGroup<RadioValues>;

Actual behavior:

Syntax error, because typeof GenericGroup<RadioValues>; is not supported.

Motivation:

In my use case I have a class with a generic (like GenericGroup) which extends a class from a 3rd party lib. The class from the 3rd party lib uses multiple params in its constructor. When I alias my class with the filled generic I don't want to write the parameters for the 3rd party lib every time (as done with new (name: string) => GenericGroup<CheckboxValues>;) as I don't really maintain them.

(There seem to be multiple related issues, but I couldn't found an issue with exactly this problem.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    QuestionAn issue which isn't directly actionable in code

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions