Skip to content


Passive data structure in TypeScript

I really like this kind of construct in C/C++

struct Point
{
  double x;
  double y;
};

as a way to have a structured dictionary that should have nice auto-completion.

After learning the TypeScript way to do it in a course I forgot it, before finding it back so I’ll save it here for safekeeping ^^ Basically, the TypeScript solution to create a PDS / “plain old data structure” / “POD-structs” (not really sure how to call those…) is to hijack the interface, like this:

interface Point
{
  number x;
  number y;
}

This way when you declare

let mypoint: Point = {x: 1, y: 5};

you’ll then have autocompletion to access mypoint.x and mypoint.y. And since TypeScript’s interfaces completely vanish during compilation, that “misuse” won’t have any consequence.

Thanks to this article with many strange data structures for pointing me to the solution 🙂

Posted in JavaScript / TypeScript / Node.js.


0 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.



Some HTML is OK

or, reply to this post via trackback.

Sorry about the CAPTCHA that requires JS. If you really don't want to enable JS and still want to comment, you can send me your comment via e-mail and I'll post it for you.

Please solve the CAPTCHA below in order to fight spamWordPress CAPTCHA