Boolean Index Signature in TypeScript
miroslavpetrik.medium.com·3h·
Discuss: Hacker News
Flag this post

6 min read2 days ago

Press enter or click to view image in full size

Photo by Joshua Hoehne on Unsplash

Imagine that we are building a helper function cond to pick a truthy condition:

function cond(...cases) {  for (const condition of cases) {    if (condition.hasOwnProperty(true)) {      return condition[true]    }  }}let a = 11, b = 7;const result = cond(   {[a < b]: "A is less than B"},   {[a > b]: "A is greater than B"},   {[a === b]: "A is equal to B"},);// result === "A is greater than B"

Even though this is a valid JavaScript, it won’t compile in TypeScript. Instead we get the following 3 errors:

function cond(...cases...

Similar Posts

Loading similar posts...