Property removals in TypeScript

A quick note about how to easily go from a broader type to a narrower one in TypeScript.

Suppose you have the following two interfaces:

interface ABC {
  a: number;
  b: string;
  c: boolean;
}

interface AB {
  a: number;
  b: string;
}

We want to convert a var from ABC to AB. Although ABC meets all the constraints of AB, the additional excess properties sometimes break things and TypeScript will, sometimes, complain about them.

So we could just do this:

declare var myVar: ABC;
delete myVar.c;

But that’s not really desirable because, assuming TypeScript even lets you do that, you’re mutating an existing typed object. myVar is still typed as ABC, even though it no longer has a c property.

I used to frequently write code that cloned myVar, deleted the extra property, and then typecast the clone to a new interface. But with the introduction of destructuring and spread operator support in TypeScript 2.1, it’s actually quite simple now:

let { c, ...newVar } = myVar

Tada! newVar is created without the c property, and TypeScript recognizes it.

LGBT Wedding Cakes

This makes me uncomfortable. As much as I would prefer that bakeries not discriminate, I don’t like using the law to compel them to, both from a political perspective and from a legal one.

Politically, I think it’s a mistake. The argment for allowing gay marriage has long been that gay marriage has almost zero impact on straight persons. If you don’t support gay marriage, then don’t marry someone of the same sex. And many, if not all, conservatives understand that — especially in more libertarian areas. Gay marriage doesn’t change the rules for straight marriage. It doesn’t compel the clergy to officiate at gay weddings or live with a gay roommate or even be polite to gay people. But … now you can be compelled to bake a gay wedding cake. And that complicates the libertarian case for LGBT rights.

It also comes across as spiteful. Unless you think unhappy bakery owners make your wedding cake more delicious. Salty tears and such.

More …

Life Choices

Sharks in suits
“So if this law thing doesn’t work out, I might give professional dancing a shot.”