Typescript: type class

Created on 8 Feb 2020  路  2Comments  路  Source: microsoft/TypeScript

Search Terms

type class

Suggestion

Specifying a class as a member type or parameter type as a class rather than just using any

Use Cases

To specify a class as a member type or parameter type

Examples

class View extends Events {
    constructor(options:any = {}) {
        super();
        this.options = options;
    }
    initialize() {
        this.model = this.options && this.options["model"] || {};
        this._model = new this.model;
    }

    options:any
    model?:class
    ...
}

class Model { ... }

class MyModel extends Model { ... }

class MyView extends View {
    constructor(options?:any) { ... }
    model = MyModel
    ...
}

Checklist

My suggestion meets these guidelines:

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

Most helpful comment

Are you just looking for a constructor signature (aka newable)? Try replacing class in your example with new (...args: any) => any and see if it meets your needs.

All 2 comments

Are you just looking for a constructor signature (aka newable)? Try replacing class in your example with new (...args: any) => any and see if it meets your needs.

@jcalz Thanks I will try that

Was this page helpful?
0 / 5 - 0 ratings

Related issues

zhuravlikjb picture zhuravlikjb  路  3Comments

DanielRosenwasser picture DanielRosenwasser  路  3Comments

Roam-Cooper picture Roam-Cooper  路  3Comments

wmaurer picture wmaurer  路  3Comments

jbondc picture jbondc  路  3Comments