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

what'd
what w
what does
how to pronounce what
what is called
what's
what is love
what means sign

I would like to know what man and child have in common and how they differ.

class Person {
  name: string;
  age: number;
}
class child extends Person {}
class man implements Person {}

What, what definition: 1. used to ask for information about people or things: 2. used in questions that show you are…. Learn more. WhatsApp Messenger: More than 2 billion people in over 180 countries use WhatsApp to stay in touch with friends and family, anytime and anywhere. WhatsApp is free and offers simple, secure, reliable messaging and calling, available on phones all over the world.

In typescript (and some other OO languages) you have classes and interfaces.

An interface has no implementation, it's just a "contract" of what members/method this type has. For example:

interface Point {
    x: number;
    y: number;
    distance(other: Point): number;
}

Instances who implement this Point interface must have two members of type number: x and y, and one method distance which receives another Point instance and returns a number. The interface doesn't implement any of those.

Classes are the implementations:

class PointImplementation implements Point {
    public x: number;
    public y: number;

    constructor(x: number, y: number) {
        this.x = x;
        this.y = y;
    }

    public distance(other: Point): number {
        return Math.sqrt(Math.pow(this.x - other.x, 2) + Math.pow(this.y - other.y, 2));
    }
}

(code in playground)

In your example you treat your Person class once as a class when you extend it and once as an interface when you implement it. Your code:

class Person {
    name: string;
    age: number;
}
class Child  extends Person {}

class Man implements Person {}

Has a compilation error saying:

Class 'Man' incorrectly implements interface 'Person'. Property 'name' is missing in type 'Man'.

And that's because interfaces lack implementation. So if you implement a class then you only take its "contract" without the implementation, so you'll need to do this:

class NoErrorMan implements Person {
    name: string;
    age: number;
}

(code in playground)

Bottom line is that in most cases you want to extend another class and not to implement it.

what, Quickly send and receive WhatsApp messages right from your computer. WhatsApp Messenger: More than 2 billion people in over 180 countries use WhatsApp to stay in touch with friends and family, anytime and anywhere. WhatsApp is free and offers simple, secure, reliable messaging and calling, available on phones all over the world.

Great Answer from @nitzan-tomer! Helped me a lot... I extended his demo a bit with:

IPoint interface;
Point implements IPoint;
Point3D extends Point;

And how they behave in functions expecting an IPoint type.

So what I've learned so far and been using as a thumb-rule: If you're using classes and methods expecting generic types, use interfaces as the expected types. And make sure the parent or base-class uses that interface. That way you can use all subclasses in those as far as they implement the interface.

Here the extended demo

WHAT, azərbaycan � Afrikaans � Bahasa Indonesia � Melayu � catal� � čeština � dansk � Deutsch � eesti � English � espa�ol � fran�ais � Gaeilge � hrvatski � italiano � Kiswahili� Seen a font in use and want to know what it is? Upload an image to WhatTheFont to find the closest matches in our database.

  1. Interface extends interface with shape
  2. Interface extends class with shape
  3. Class implements interface should implements all fields provided by the interface
  4. Class implements class with shape
  5. Class extends class with all fields

extends focus on inherit and implements focus on constraint whether interfaces or classes.

WhatsApp Web, Usage Note: When what is the subject of a clause, it takes a singular verb if the word or phrase that completes the sentence (the complement) is singular, as in I � The Seasons Calculator shows the times and dates of Vernal (Spring) & Autumnal (Fall) equinoxes and Summer and Winter solstices all over the world.

WhatsApp, Shira: Let's start with TikTok. What are the reasonable concerns about it? Kevin: Because TikTok is owned by a Chinese company, ByteDance, it� Find out what your public IPv4 and IPv6 address is revealing about you! My IP address information shows your location; city, region, country, ISP and location on a map.

What, What the Experts Say. Microaggressions are the everyday indignities and insults that members of marginalized groups endure in their routine� AT&T 5G. Now Nationwide. We're committed to keeping you connected. Get 24/7 support & manage your account online. Explore unlimited data plans, Internet service, & AT&T TV.

What to Do About TikTok, What We Do. We are Defining Possible. Northrop Grumman is a pioneering company. We design, develop, build and support some of the world’s most advanced products, from cutting-edge aircraft and next-generation spacecraft to unrivaled cyber security systems and all-seeing radars.

You've Been Called Out for a Microaggression. What Do You Do?, Contains articles about a number of conspiracies, with a focus on the Sept. 11 attacks on the World Trade Center, and the ensuing "War on Terrorism".

Comments
  • Nicely explained here : stackoverflow.com/a/35990799/452213 . Especially, the example : typescriptlang.org/play/…
  • when you say "extends-it gets all from its parent", does it apply to private members? For instance class Person {private name: string} class man extends Person{gender: string;} does man have the property name?
  • Private are there as well. Just inaccessible by TS. Make them protected and you can use them. In case of "implements" just the public part makes sense. Hope it helps a bit
  • Excellent answer. Just not sure by your comment for "private is there but not accessible by TS". Do you mean private properties are copied in that newly created child object? And in the case of implements, only public properties are copied?
  • Also, I got around one more point. If this is the definition of extends. Then please if you could explain this stackoverflow.com/questions/60390454/…
  • This answer is simpler to understand.
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - From Review
  • @aronisstav I only posted an extended demo of what I found a good answer that already helped me. But maybe someone else would find the work I did extending the demo useful. That's all. Comments are not really meant for putting a code-block, so that's why I find it better understandable in an Answer-Post. So what's the problem with it?
  • Your answer was (automatically?) flagged due to length and content, appeared in my review queue and I gave merit to the reasons presented in the flag. Its main contribution (explaining that you extended the demo) would be better as a comment. With the added paragraph perhaps it is indeed more useful.
  • @andzep your extended demo example is really helpful.