Skip to content

Allow for static members to reference class type parameters (Error: TS2302) #32211

Closed
@geoyws

Description

@geoyws

Search Terms

  1. [allow for static members to reference class type parameters] (Allow static members to reference class type parameters #24018)
  2. ts2302

Suggestion

Allow for static members to reference class type parameters because it saves typing.

Perhaps we could use a factory to dynamically create classes based on the generic type provided.

Use Cases

import Big from 'big.js';

// db column
class Column<T> {
  static readonly _name: string;
  static readonly comment: string;
  static readonly dataType: typeof T = T; // error: Static members cannot reference class type parameters ts(2302)

  constructor(readonly value: T) {}
}

class Bigint {
  constructor(readonly value: Big = new Big(0)) {}
}

class Amount extends Column<Bigint> {
  static readonly _name: string = '"amt"';
  //static readonly dataType: typeof Bigint = Bigint; // this would be unnecessary
}
														
class Quantity extends Column<BigInt> {
  static readonly _name: string = '"qty"';
  //static readonly dataType: typeof Bigint = Bigint; // this would be unnecessary
}

Examples

See previous section.

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions