Classes in TypeScript: Properties and Special Cases

In our quest to model classes in TypeScript, we’ve so far managed to model the type hierarchy, scalar DataType values, and enums. The big piece that remains, however, is representing what’s actually inside of the class: it’s properties.

After all, what it means for a JSON-LD literal to have "@type" equal to "Person" is that certain properties — e.g. "birthPlace" or "birthDate", among others — can be expected to be present on the literal. More than their potential presence, defines a meaning for these properties, and the range of types their values could hold.

The easy case: Simple Properties

You can download the entire vocabulary specification of, most of which describes properties on these classes. For each property, will tell us it’s domain (what classes have this property) and range (what types can its values be). For example, the name property specification shows that it is available on the class Thing, and has type Text. One might represent this knowledge as follows:

interface ThingBase {
  "name": Text;

Linked Data, it turns out, is a bit richer than that, allowing us to express situations where a property has multiple values. In JSON-LD, this is represented by an array as the value of the property. Therefore:

interface ThingBase {
  "name": Text | Text[];

Multiple Property Types

Often times, however, the range of a particular property is any one of a number of types. For example, the property image on Thing can be an ImageObject or URL. Note, also, that nothing in JSON-LD necessitates that all potential values of image have the same type.

In other words, if we want to represent image on ThingBase, we have:

interface ThingBase {
  "name": Text | Text[];
  "image": ImageObject | URL | (ImageObject | URL)[];

Properties are Optional

In JSON-LD, all properties are optional. In practice cares about "@type" being defined for all classes, but does not otherwise define any other properties as being required. This is sometimes complicated as specific search engines require some set of properties on a class.

interface ThingBase {
  "name"?: Text | Text[];
  "image"?: ImageObject | URL | (ImageObject | URL)[];

Properties Can Supersede Others in the Vocabulary

As matures, it’s vocabulary changes. Not all of these changes will be additive (adding a new type, or a new type on an existing property). Some will involve adding a new type or property intended to replace another.

For example, area was a property on BroadcastService describing a Place the service applies to. Turns out, a lot of other businesses also apply to a specific area. serviceArea replaced area, and instead of applying to BroadcastService, it applied to its parent, Service. In addition, serviceArea can also apply to Organization and ContactPoint (something area never did). In addition to being just a Place, serviceArea can be an AdministrativeArea or an arbitrary GeoShape.

Later on, serviceArea was replaced by areaServed, which also included a freeform Text as a possible value, and applied to a few more objects.

When a property replaces another, it supersedes it (inversely, the other property is superseded by the new one). These changes keep existing JSON-LD backwards-compatible. A property p2 superseding p1 will generally imply:

  1. p2 is available on all types p1 was available on. (p2‘s domain is strictly wider).
    This includes (a) additional types in the domain, or (b) the domain changing to a parent class, for example.
  2. p2 includes all possible types of p1 (p2‘s range is strictly wider).

Typically, new data will be written with p2, but the intention is that any old data written using p1 continues to be valid.

In TypeScript, we can use the @deprecated JSDoc annotation to recommend using a new property instead. We can go further and simply skip all deprecated properties (properties that are superseded by one or more properties) if we wanted to.

The story of area, serviceArea, and areaServed can be partially summarized as follows:

interface BroadcastServiceBase extends OrganizationBase {
  /** @deprecated Superseded by serviceArea */
  "area"?: Place | Place[];

interface OrganizationBase {
  /** @deprecated Superseded by areaServed *
  "serviceArea"?: AdministrativeArea | GeoShape | Place |
                  (AdministrativeArea | GeoShape | Place)[];

  "areaServed"?: AdministrativeArea | GeoShape | Place | Text |
                 (AdministrativeArea | GeoShape | Place | Text)[];

Things Fall Apart

Multiple Types

"@type" is just another property (albeit it has speical meaning).

JSON-LD permits a node to have multiple "@type"s as well, and search engines are happy to accept multiple types (at least for some nodes). In practice, a node having two types means that it can have properties on both types. For example, this is valid:

  "@type": ["Organization", "Person"],
  "birthDate": "1980-01-01",
  "foundingDate": "2000-01-01"

In TypeScript, discriminating a union on an array seems to be hard, and it becomes a bit clunky to define. For now, our TypeScript definitions will not allow multiple @type values.

Sub-Properties takes advantage of the RDF concept of a sub-property:

If a property P is a subproperty of property P’, then all pairs of resources which are related by P are also related by P’

RDF Schema 1.1

Simply put, a sub-property is a more specific version of a property.

For example, image exists on Thing, but has two sub-properties: logo, which exists on Brand, Organization, and a few other types, and photo, which exists on a Place.

One thing I expected is not to be able to specify a super-property on a node whose type has the sub-property available. I.e., if I’m describing a Brand, it’s logo will sufficiently describe image, thereby serving no meaning to specify image.

That’s not quite true, though, a sub-property implying a property still leaves room for the property itself to be available (an Organization can have multiple images, one of which is its logo).

And while that should be true (by the RDF specification), turns even that isn’t true in Some sub-properties have more general types than their super-properties, e.g. photo can be a Photograph, but it’s super-property, image cannot.

So here, we simply punt.

Special Cases

Reading documentation, you might expect as I did that there are two distinct hierarchies of data: Thing (aka classes/node types) and DataType (aka values/scalars/primitives). That’s definitely not true in JSON-LD in general, where many values are untyped to begin with, specified using an "@id" reference, or a string. implies it imposes a tighter requirement, and describes these hierarchies dis-jointly, but that turns out not to be true.

Turns out, some types, like Distance are in the Thing hierarchy, but expect string values (in the case of Distance, those would take the form "5 in" or "2.3 cm", etc.).

We might consider having our typings include string (or Text?) for all of our classes. To encourage semantically specifying properties, however, I decided to only allow string on a subset of our nodes.

type Distance = DistanceLeaf | string;

Conclusion is a vocabulary designed in an inherently human way. This sometimes have repercussions of being thoughtful. Yet, just as often, it means that the semantics have evolved in a way that is inconsistent. The result is often dissatisfying: relations that are defined but don’t hold in practice, objects that are described with textual comments but have no formal relations specifying them, distances that are described as nodes, and many others. These inconsistencies often lead to hacks when trying to represent the vocabulary in TypeScript.

Yet, it’s important not to lose track of why modeling in TypeScript to begin with. The lack of tooling around (specifically in IDEs when writing out a specific piece of data), is precisely the need we’re filling in. But ultimately, adding structure to an ontology that is largely decided by a loose set of guidelines will be lossy.

The question remains: is the trade-off worth it?

For my purposes, schema-dts has helped me tremendously over the past several months. DataType in TypeScript: Structural Typing Doesn’t Cut It has a concept of a DataType, things like Text, Number, Date, etc. In JSON-LD, we represent these as strings or numbers, rather than array or object literals. This data could describe the name of a Person, a check-in date and time for a LodgingReservation, a URL of a Corporation, publication date of an Article, etc. As we’ll see, the DataType hierarchy is far richer than TypeScript’s type system can accommodate. In this article, we’ll go over the DataType hierarchy and explore how much type checking we can provide.

We saw in the first installment how TypeScript’s type system makes expressing JSON-LD describing class structure very elegant. The story got slightly more clouded when we introduced Enumerations. Data Types

Let’s take a look at the full DataType tree according

Boolean’s look quite similar to enums, with and as it’s two possible IRI values (depending on @context, those can of course be represented as relative IRIs instead) or their HTTPS equivalents.

Number and descendants are just JSON / JavaScript numbers. Float indicates the JSON number will have a floating point precision, whereas Integer tells us to expect a whole number. On its own right, JavaScript does not distinguish floats and integers as separate types, and neither does TypeScript. While TypeScript supports the idea of literal types, specifying a type as all possible integers or all possible floating point numbers isn’t expressible.

Continue reading “ DataType in TypeScript: Structural Typing Doesn’t Cut It” Enumerations in TypeScript

Last time, we talked about modeling the class hierarchy in TypeScript. We ended up with an elegant, recursive solution that treats any type Thing as a "@type"-discriminated union of ThingLeaf and all the direct sub-classes of the type. The next challenge in the journey of building TypeScript typings for the vocabulary is modeling Enumerations.

Learning from Examples

Let’s look at a few examples from the website to get a better sense of what Enumerations look like.

First up, I looked at PaymentStatusType, which can take any one of these values: PaymentAutomaticallyApplied, PaymentComplete, PaymentDeclined, PaymentDue, or PaymentPastDue. PaymentStatusType is used in the paymentStatus property on the Invoice class.

Here’s an excerpt from an example of an invoice:

    "@context": "",
    "@type": "Invoice",
    // ...
    "paymentStatus": "",
    "referencesOrder": [
      // ...

Here, the value of an Enumeration appears as an absolute IRI.

Looking at other examples, however, such as GamePlayMode which appears in playMode on VideoGame shows up differently:

Continue reading “ Enumerations in TypeScript”

Modeling Schema with TypeScript: The Power and Limitations of the TypeScript Type System

Recently, I published schema-dts (npm, GitHub), an open source library that models JSON-LD in TypeScript. A big reason I wanted to do this project is because I knew some TypeScript type system features, such as discriminated type unions, powerful type inference, nullability checking, and type intersections, present an opportunity to both model what JSON-LD looks like, while also providing ergonomic completions to the developer.

In a series of posts, I’ll go over some of the Structured Data concepts that lent themselves well to TypeScript’s type system, and those concepts that didn’t. First up: the type hierarchy of JSON-LD Schema, and how can be represented in TypeScript.

Note: I’ll be describing JSON-LD in general in very broad strokes and will spend more time discussing how JSON-LD looks like in particular. For those who are familiar with the JSON-LD spec, you’ll see I took a few liberties. This is because schema-dts makes a few assumptions, such as the @context being a known constant, etc. schema-dts also foregoes some features, such as specifying multiple types of a node object, etc.

Modeling the class structure with the TypeScript Type System JSON-LD node objects are always typed (that is, they have a @type property that points to some IRI–a string–describing it). Given a @type you know all the properties that are defined on a particular object. Object types inherit from each other. For example, Thing in has a property called name, and Person is a subclass of Thing that defines additional properties such as birthDate, and inherits all the properties of Thing such as name. Thing has other sub-classes, like Organization, with it’s own properties, like logo.

Let’s use this minimal example to try a few approaches:

1. Modeling each with inheritance

interface Thing {
  "name": string;
interface Person extends Thing {
  "@type": "Person";
  "birthDate": string;
interface Organization extends
    Thing {
  "@type": "Organization";
  "logo": string;

If we had a const something: Thing , then we could assign it to a Thing, Person, or Organization. So that’s a start! But there are a few problems:

Continue reading “Modeling Schema with TypeScript: The Power and Limitations of the TypeScript Type System”

About those Side-effects in Observables, an Angular Use Case

When testing a codebase in Angular Ivy, I ran into a bunch of test failures I wasn’t seeing before. ExpressionChangedAfterItHasBeenCheckedErrors were being thrown around. In debugging these failures, I found that many of them are the results of side-effects in Observables and Observable pipe operations. I happened to describe these earlier in my piece on Observables, Side-effects, and Subscriptions.

Consider this minimal reproduction:

  selector: 'widget-editor',
  templateUrl: 'widget_editor.html'
export class WidgetEditor {
  constructor(private readonly service: WidgetService) {}

  widgetName = '';
  widgetConfig$ = this.service.getWidget('my_widget').pipe(
    map(widgetDetails => {
      this.widgetName =;
      return widgetDetails.config;
Continue reading “About those Side-effects in Observables, an Angular Use Case”
%d bloggers like this: