You are looking for information on the topic “TypeScript Assignability between Parent method and Child class method is different from regular function’s assignability”. toamdautien.com will gather information to answer your questions in this article. Please read this article to the end to find the information you want to see. You can also check out some other related articles here: toamdautien.com/blog TypeScript class type, Constructor TypeScript, Class TypeScript, TypeScript static class, TypeScript playground, Extend class TypeScript, TypeScript type, TypeScript class implements interface.
View information about the TypeScript Assignability between Parent method and Child class method is different from regular function’s assignability
Information about this question
- Question: TypeScript Assignability between Parent method and Child class method is different from regular function’s assignability
- Time: Asked 6 minute ago
- Modified: Updated 92 minute ago
- View: 4233
TypeScript Assignability between Parent method and Child class method is different from regular function’s assignability
Yes, TypeScript’s assignability rules for parent and child class methods are slightly different from regular function assignability.
In TypeScript, a child class method is considered assignable to a parent class method if the child class method has the same parameter types as the parent class method, or if the child class method’s parameters are a subset of the parent class method’s parameters. Additionally, the child class method must have the same or narrower return type than the parent class method.
Here is an example to illustrate this concept:
typescriptclass Animal { move(distanceInMeters: number): void { console.log(`Animal moved ${distanceInMeters}m.`); } } class Dog extends Animal { move(distanceInMeters: number, direction: string): void { console.log(`Dog moved ${distanceInMeters}m ${direction}.`); } } let animal: Animal = new Dog(); animal.move(10); // Output: "Dog moved 10m undefined."
In this example, the Dog
class extends the Animal
class and overrides the move
method to include an additional direction
parameter. Despite the difference in parameter lists, the Dog
class method is still considered assignable to the Animal
class method because the move
method in the Dog
class has the same or narrower return type than the move
method in the Animal
class.
On the other hand, if we had two regular functions with different parameter lists, they would not be assignable to each other:
typescriptfunction move(distanceInMeters: number): void { console.log(`Moved ${distanceInMeters}m.`); } function move(direction: string): void { console.log(`Moved in ${direction} direction.`); } let animalMove: (distanceInMeters: number) => void = move; animalMove(10); // Output: "Moved 10m."
In this example, the two move
functions have different parameter lists, so they are not assignable to each other. If we try to assign the move
function to a variable with a narrower parameter list, like animalMove
, we get a compilation error.
Watch videos related to “TypeScript Assignability between Parent method and Child class method is different from regular function’s assignability”
No BS TS #3 – Typescript Functions with Functions
How is class in JavaScript different from class in TypeScript?
In JavaScript, classes were introduced in ES6 as a syntactical sugar over the existing prototype-based inheritance model. Classes in JavaScript are essentially constructor functions that define a blueprint for creating objects with a set of properties and methods. However, unlike classes in other object-oriented languages, JavaScript classes are not true classes in the traditional sense.
TypeScript, on the other hand, is a superset of JavaScript that adds optional static typing and other features to the language. TypeScript also supports classes, but unlike JavaScript, classes in TypeScript are a true class-based object-oriented programming construct. They are more similar to classes in other object-oriented languages like Java or C#, with features such as access modifiers, inheritance, and interfaces.
Some of the key differences between classes in JavaScript and TypeScript are:
-
Type annotations: TypeScript provides support for type annotations, which allows you to specify the types of class properties and method parameters. This makes it easier to catch errors early in the development process and helps ensure that your code is type-safe.
-
Access modifiers: TypeScript allows you to use access modifiers like public, private, and protected to control the visibility of class members. In JavaScript, all class members are public by default.
-
Inheritance: TypeScript supports class inheritance, which allows you to create a new class that is a modified version of an existing class. Inheritance is not as straightforward in JavaScript, which relies on prototypal inheritance.
-
Interfaces: TypeScript provides interfaces, which define a set of properties and methods that a class must implement. This allows you to write more modular and reusable code.
Overall, TypeScript’s classes provide a more robust and type-safe way to define object-oriented programming constructs compared to JavaScript’s classes.
Is TypeScript Object Oriented or functional?
TypeScript is a programming language that is primarily object-oriented but also supports functional programming features. It is a superset of JavaScript, so it inherits its object-oriented nature from JavaScript.
TypeScript supports object-oriented programming features such as classes, interfaces, inheritance, encapsulation, and polymorphism. These features allow developers to write modular and maintainable code, with a focus on creating reusable and extensible software.
In addition to its object-oriented features, TypeScript also supports functional programming concepts such as higher-order functions, lambdas, and closures. These features allow developers to write code that is more concise and expressive, and to take advantage of the benefits of functional programming such as immutability and pure functions.
Overall, TypeScript is a language that supports both object-oriented and functional programming paradigms, giving developers the flexibility to choose the approach that best suits their needs.
How to override base class method in TypeScript?
In TypeScript, you can override a method in a base class by defining a method with the same name in the derived class. The new method in the derived class will replace the implementation of the method in the base class.
Here’s an example:
typescriptclass BaseClass { public doSomething(): void { console.log("BaseClass.doSomething()"); } } class DerivedClass extends BaseClass { public doSomething(): void { console.log("DerivedClass.doSomething()"); } } const obj = new DerivedClass(); obj.doSomething(); // Output: "DerivedClass.doSomething()"
In this example, the DerivedClass
extends BaseClass
and overrides its doSomething()
method with a new implementation. When we create an instance of DerivedClass
and call the doSomething()
method, the output will be “DerivedClass.doSomething()”.
Note that if you want to call the implementation of the overridden method in the base class from the derived class, you can use the super
keyword. For example:
typescriptclass DerivedClass extends BaseClass { public doSomething(): void { super.doSomething(); // Call the implementation in the base class console.log("DerivedClass.doSomething()"); } }
In this example, the super.doSomething()
call will execute the implementation of the doSomething()
method in the base class, and then the derived class implementation will execute as well.
Images related to TypeScript Assignability between Parent method and Child class method is different from regular function’s assignability
Found 25 TypeScript Assignability between Parent method and Child class method is different from regular function’s assignability related images.

You can see some more information related to TypeScript Assignability between Parent method and Child class method is different from regular function’s assignability here
- Typescript method with Child/Parent arguments are not …
- Subclass method is not allowed (TS2425) if the parent class …
- How To Use Classes in TypeScript | DigitalOcean
- The Dart type system – Dart programming language
- TypeScript Doesn’t Turn JavaScript Into a Class-Based OOP Language
- Object-Oriented Programming in TypeScript – Bird Eats Bug
- Method Overriding in TypeScript? – Tutorialspoint
- Methods and Functions – Codecademy
- How to Override a Class method in TypeScript | bobbyhadz
- Advanced TypeScript 4.8 Concepts: Classes and Types
Comments
There are a total of 414 comments on this question.
- 59 comments are great
- 565 great comments
- 250 normal comments
- 158 bad comments
- 90 very bad comments
So you have finished reading the article on the topic TypeScript Assignability between Parent method and Child class method is different from regular function’s assignability. If you found this article useful, please share it with others. Thank you very much.