Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

typescript: reference current class type

Tags:

typescript

Is it possible to reference current class type in type signature? so that I can do something like this:

 export class Component{
  constructor(config?: { [field in keyof self]: any }) {
      Object.assign(this, config)
  }
}

the idea is to pass a configuration object that would consist of current class keys.

I could go with interfaces but then I need to type same portion of code twise (in interface and in implementing class)

Another way would be to use generics. Something like this:

export class Component<T>{
  init(config?: { [field in keyof T]?: any }) {
    Object.assign(this, config)
  }
}

class TestComponent extends Component<TestComponent>{
  foo: number
}
const component = new TestComponent().init({ foo: 11 })

But having code like class TestComponent extends Component<TestComponent> makes me to search for better ways...

like image 790
SET Avatar asked Nov 03 '18 15:11

SET


People also ask

How do you call a class in TypeScript?

Create an instance of Foo to be able to call it: let foo:Foo = new Foo(); let result:number = foo. calcSomeThing( parameter ); Never use var in Typescript - let is your friend.

What is class type in TypeScript?

When using the class keyword in TypeScript, you are actually creating two things with the same identifier: A TypeScript interface containing all the instance methods and properties of the class; and. A JavaScript variable with a different (anonymous) constructor function type.

How do I create an instance of a class in TypeScript?

You can do this by using the new keyword, followed by a syntax similar to that of an arrow function, where the parameter list contains the parameters expected by the constructor and the return type is the class instance this constructor returns. The TypeScript compiler now will correctly compile your code.

How do I inherit a class in TypeScript?

To inherit a class, you use the extends keyword. For example the following Employee class inherits the Person class: class Employee extends Person { //.. } In this example, the Employee is a child class and the Person is the parent class.

What is a typescript class?

As with other JavaScript language features, TypeScript adds type annotations and other syntax to allow you to express relationships between classes and other types. Here’s the most basic class - an empty one:

How do I have multiple generic types in typescript?

TypeScript allows you to have multiple generic types in the type parameter list. For example: class className<K,T> { //... } The generic constraints are also applied to the generic types in the class:

How do you declare a property in typescript?

With TypeScript, you usually have to declare the property first in the body of the class and give it a type. For example, add a name property to your Person class: In this example, you declare the property name with type string in addition to setting the property in the constructor.

What is the default visibility of a class member in typescript?

This is the default visibility of class members in TypeScript. When you do not add the visibility modifier to a class member, it is the same as setting it to public. Public class members may be accessed anywhere, without any restrictions. To illustrate this, return to your Person class from earlier:


1 Answers

You can reference the current class using polymorphic this type

export class Component{
  init(config?: { [field in keyof this]?: this[field] }) {
    Object.assign(this, config)
  }
}

class TestComponent extends Component{
  foo: number
}
const component = new TestComponent().init({ foo: 11 })
const component2 = new TestComponent().init({ foo: "11" }) // err

You can't however use this as a type in the constructor

export class Component{
  constructor(config?: { [field in keyof this]?: this[field] }) { // error
    Object.assign(this, config)
  }
}
like image 141
Titian Cernicova-Dragomir Avatar answered Oct 19 '22 16:10

Titian Cernicova-Dragomir