Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

This looks really nice. Do you know if type narrowing is a form of this as well? TypeScript has this pretty neat feature where if you have a variable x of union type A | B and you're in a code path where you've checked if x is of type A then the type is automatically narrowed to A in that code path without you doing any casting etc.

    function num(x: number | Array<string>): number {
      if (typeof (x) == 'number') {
        return x + 10;
      } else {
        return x.length;
      }
    }
    console.log(num(5)) // 15
    console.log(num(["A", "B", "C"])) // 3
I think they just refer to it as type narrowing but it feels kind of similar in concept, so I'm wondering if it's a limited form of refinement types.


Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: