6

Someone could explain me what is the difference between class and object in typescript.

class greeter{
  Name: string;
  Sayhello(){
    console.log("hello")
  }
}

Before I using this

var greeter = {
    Name : "",
    Sayhello: function sayhello(){
        console.log("hello");
    }
}
5
  • 5
    Please take the time to make the code in the question properly formatted, including note that both TypeScript and JavaScript are case-sensitive languages. If you can't be bothered to make the question clear and correct, why should other people be bothered to answer it clearly and correctly? Commented May 30, 2016 at 20:25
  • Okay, the answer you've received answers the question at the beginning of the question, so I've removed the off-topic one at the end. (Note: SO is a very active place, please don't ask-and-run. Stick around so you can clarify your question in response to queries.) Commented May 30, 2016 at 20:41
  • In example 1: (new greeter).Name is a string Commented May 30, 2016 at 22:57
  • In example 2: greeter.Name is a string Commented May 30, 2016 at 22:57
  • They are not the same Commented May 30, 2016 at 22:57

2 Answers 2

8

It is up to you. Both of these are valid and idiomatic TypeScript:

export class Greeter {

    name: '';

    sayHello() {
        console.log('hello');
    }
}

and

export const greeter = {

    name : '',

    sayHello: () => {
        console.log('hello');
    }
}

// if you need just the type of greeter for some reason
export type Greeter = typof greeter; 

If you don't have a need for the class, don't use them.

But you may find benefits of classes if you want to:

  • manage your dependencies with Dependency Injection
  • use multiple instances
  • use polymorphism

If you have multiple instances, using classes or prototype constructor functions, allow you to share method implementations across all instances.

Even if you are in a purely functional paradigm, using prototype constructor functions or classes can be useful for creating monads.

If you only have one instance, and do not need for a constructor, an object is is probably fine.

Sign up to request clarification or add additional context in comments.

4 Comments

Your answer implies something I'm sure you know isn't true: That they do the same thing. They don't, you might want to clarify what each of them does (as I think the OP is confused about that).
"export the type definition" - can you clarify that point? Variable declarations have a type too so you could write a definition that represents it.
No. My answer does not imply they do the same thing. Just the apposite, it implies that if you need instances, DI, Tests, exported Types, polymorphism you use one -- if you don't you use the other.
@DavidSherret yes you are right you can do export var greeter and get the explicit type bound to the literal object. What you can't do is have an abstract type, say another 'kind' of greeter that implements the greeter type.
7

There are many differences. On a basic level, a class is an object that can be used to create other objects with a specific shape and functionality. It provides syntactic sugar for functionality that would be a lot more work to accomplish with plain objects and functions.

You should take some time to read about what classes are in the TypeScript Handbook because answering your question in detail would be equivalent to writing a few chapters of a book—especially when tailoring it for TypeScript.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.