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...
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.
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.
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.
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.
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:
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:
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.
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:
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)
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With