Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What to use for data-only objects in TypeScript: Class or Interface?

I have a bunch of data-only "classes" (in .NET world we call them POCO objects) that does not have any methods or even constructors. Examples are Customer, Product, User entities, you name it...

Originally I started using typescript classes but now I'm thinking that declaring them as interface might be better. From performance standpoint, and not only... It's just that in C# we're used to use interfaces for different thing, and for "POCO" (Plain-old-clr-object, or "data-only" object) we use just a class (sometimes even struct).

What is a proper way to declare them in TypeScript?

Note that I mostly understand (I think) technical differences between class and interface (i.e. that interface is a compile-time construct), but I'm trying to find out which one fits this case semantically.

P.S.: I've seen similar questions (like this) but none of them adress this specific issue clearly and definitely, so please don't close this as 'possible duplicate' or 'opinion-based' (cause it isn't) :)

like image 687
Titan Avatar asked Dec 09 '16 20:12

Titan


People also ask

Should I use interface or class in TypeScript?

When should we use classes and interfaces? If you want to create and pass a type-checked class object, you should use TypeScript classes. If you need to work without creating an object, an interface is best for you.

What should be the type for object in TypeScript?

In TypeScript, object is the type of all non-primitive values (primitive values are undefined , null , booleans, numbers, bigints, strings). With this type, we can't access any properties of a value.

Is object a data type in TypeScript?

The TypeScript object type represents any value that is not a primitive value. The Object type, however, describes functionality that available on all objects. The empty type {} refers to an object that has no property on its own.

Are TypeScript interfaces objects?

TypeScript Interface TypeTypeScript allows you to specifically type an object using an interface that can be reused by multiple objects. To create an interface, use the interface keyword followed by the interface name and the typed object.


2 Answers

Interface and it's not even close.

People start writing TypeScript and they suddenly think they have to use classes for some reason. But they don't. Classes are an ES6 feature and they work fine, but if it's just data, it's just data.

A major problem with using classes is that they won't serialize/deserialize like you expect over the wire, so things like instanceof checks won't work.

One rule of thumb is that if there's not internal state associated with some methods, and there's no need for traditional OO polymorphism, don't use a class. This even extends to static classes -- use namespace / module instead.

like image 107
Ryan Cavanaugh Avatar answered Oct 12 '22 04:10

Ryan Cavanaugh


Use classes with parameter properties:

// Immutable data object
class Person {
  constructor(readonly firstName: String, readonly lastName: String) {}
}

// Mutable data object
class Person {
  constructor(public firstName: String, public lastName: String) {}
}
like image 41
mrts Avatar answered Oct 12 '22 03:10

mrts