What"s the difference between "extends" and "implements" in TypeScript

Cover Image for What"s the difference between "extends" and "implements" in TypeScript
Matheus Mello
Matheus Mello
published a few days ago. updated a few hours ago

What's the Difference Between 'extends' and 'implements' in TypeScript?

So you're writing some TypeScript code and you come across the keywords 'extends' and 'implements'. 🧐 What do they actually mean and how do they affect your code? Let's dive into it!

Understanding the Basics

In TypeScript, both 'extends' and 'implements' are used to establish relationships between classes. However, they are used in different contexts and have distinct purposes.

Extends

The 'extends' keyword is used to create a subclass or derive a new class from an existing class. This means that the subclass inherits the properties and methods of the superclass, and can also introduce new features or override existing ones.

In the given example:

class Child extends Person {}

The class 'Child' extends the class 'Person', meaning that 'Child' is a subclass of 'Person'. This relationship implies that the 'Child' class inherits the properties and methods defined in the 'Person' class.

Implements

On the other hand, the 'implements' keyword is used to make a class comply with a particular contract or interface. An interface defines a list of properties and methods that a class must implement.

In the given example:

class Man implements Person {}

The class 'Man' implements the 'Person' interface. This means that the 'Man' class must provide an implementation for all the properties and methods defined in the 'Person' interface. If any of the required properties or methods are missing, TypeScript will throw an error.

Common Issues and Solutions

Inheriting and Extending Functionality

One common issue is understanding when to use 'extends' to inherit functionality and when to use 'implements' to implement a contract. Here's a simple guideline:

  • Use 'extends' when you want to create a new class based on an existing class and also add or modify functionality specific to the subclass.

  • Use 'implements' when you want to guarantee that a class provides an implementation for all the properties and methods defined in an interface.

Compelling Call-to-Action: Join the TypeScript Community!

Now that you understand the difference between 'extends' and 'implements' in TypeScript, it's time to take your skills to the next level and join the vibrant TypeScript community! 🎉

  • Connect with other TypeScript enthusiasts on forums, social media, and developer conferences.

  • Explore more TypeScript features and best practices through documentation and tutorials.

  • Share your own experiences and solutions with the community by writing blog posts or creating open-source projects.

Remember, learning is a journey, and the TypeScript community is here to support you every step of the way!

So go ahead, start leveraging the power of 'extends' and 'implements' in your TypeScript code, and let your creativity soar! 💪

Happy coding! 👩‍💻👨‍💻


More Stories

Cover Image for How can I echo a newline in a batch file?

How can I echo a newline in a batch file?

updated a few hours ago
batch-filenewlinewindows

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Matheus Mello
Matheus Mello
Cover Image for How do I run Redis on Windows?

How do I run Redis on Windows?

updated a few hours ago
rediswindows

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Matheus Mello
Matheus Mello
Cover Image for Best way to strip punctuation from a string

Best way to strip punctuation from a string

updated a few hours ago
punctuationpythonstring

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Matheus Mello
Matheus Mello
Cover Image for Purge or recreate a Ruby on Rails database

Purge or recreate a Ruby on Rails database

updated a few hours ago
rakeruby-on-railsruby-on-rails-3

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my

Matheus Mello
Matheus Mello