GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Use typeof with generic · Issue #20719 · microsoft/TypeScript

Use typeof with generic #20719

New issue
Copy link
New issue
Copy link
Closed
Closed
Use typeof with generic#20719
Copy link
Labels
QuestionAn issue which isn't directly actionable in codeAn issue which isn't directly actionable in code

Description

@donaldpipowitch
Donald Pipowitch (donaldpipowitch)
opened on Dec 15, 2017
Issue body actions

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.)

Activity

  1. superamadeus commented on Dec 15, 2017

    @superamadeus
    superamadeus
    on Dec 15, 2017
    More actions

    The 'typeof' operator is not meant to describe the constructor of a type, but rather the type of a value. SomeGeneric<T> is not a value, it is a type in and of itself. Any other interface/type (like the one defined below) will not work in this way either.

    interface MyInterface {
        name: string;
    }
    
    let m: typeof MyInterface; // error "MyInterface only refers to a type, but is being used as a value here."

    A solution to this is to define a generic type to describe constructors like Angular does:

    // angular/packages/core/src/type.ts
    
    export interface Type<T> extends Function { 
        new (...args: any[]): T; 
    }

    from (angular/packages/core/src/type.ts)

    Usage example:

    type RadioValues = string;
    type RadiosGroup = Type<GenericGroup<RadioValues>>;
    const Radios: RadiosGroup = GenericGroup;
    const radios = new Radios('radios');
    radios.items.map(item => item.toUpperCase());
    Reacted by Ryan Cavanaugh, Apperside and XGHeaven
  2. Ryan Cavanaugh (RyanCavanaugh)
    added
    QuestionAn issue which isn't directly actionable in codeAn issue which isn't directly actionable in code
    on Dec 18, 2017
  3. RyanCavanaugh commented on Dec 18, 2017

    @RyanCavanaugh
    Ryan Cavanaugh (RyanCavanaugh)
    on Dec 18, 2017
    Member
    More actions

    A subtle thing is that generic class constructor functions are not contained in a generic type. You can tell that because the type parameter is not in scope for static members! It "feels" like you have a type like this

    interface GenericCtor<T> {
      new(x: string): InstanceType<T>;
    }

    but the real type is like this

    interface GenericCtor {
      new<T>(x: string): InstanceType<T>;
    }
    Reacted by superamadeus and kuzhelov
  4. donaldpipowitch commented on Dec 19, 2017

    @donaldpipowitch
    Donald Pipowitch (donaldpipowitch)
    on Dec 19, 2017
    ContributorAuthor
    More actions

    Thank you both. I need to wrap my head around this. In my case I extend from a 3rd party constructor (React.Component) so I need to figure out how to apply your feedback there. Thanks.

  5. Donald Pipowitch (donaldpipowitch)
    closed this as completedon Dec 19, 2017
  6. microsoft
    locked and limited conversation to collaborators on Jun 21, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    QuestionAn issue which isn't directly actionable in codeAn 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

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.