Hacker News new | past | comments | ask | show | jobs | submit login

Disagree with you on 3. 'unknown' is handy to prevent 'any's from leaking into the codebase. It's very easy to accidentally assign an 'any' to another variable and then return it, or to accidentally read a property off of it before ensuring it exists.

Unknown is basically asking the dev to confirm the choice (ideally through a typeguard, but also with a simple cast).

And if you're using a typeguard, just define the argument as 'any' ex -

  type AType = {
    c: string
  }

  let a: unknown = {
    c: 'test',
  }

  if (isAType(a)) {
    console.log(a.c) // no type errors!
  }

  function isAType(a: any): a is AType {
    return typeof a.c == 'string'
  }



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

Search: