20
bartmr
5y

When someone does Typescript stuff using the "any" keyword

Comments
  • 0
  • 0
    @kescherRant "any" disables typechecking for any flow the values goes throught. The correct form would be to use "unknown", where you can evaluate if it's truthy or not, but not access specific properties. Its useful for when you don't want to type all properties of an object right away.
  • 6
    If you use 'any' in TS you might as well skip TS all together.
  • 5
  • 1
    @bartmr @olback But one of the great things about ts is that you can go fast while prototyping and not worry about types, then add types in later when you’re ready.
  • 1
    @bartmr What about when you're defining an interface for an API response that could return any type of object for one of its properties?
  • 1
    @bartmr I know.

    I meant the why as in "why wohld anyone even do that in the first place"
  • 0
    @Zagill You can do Union types. like:

    type APIResponse {
    --dynamicStuff: ({
    ------type: Enum.isNumber,
    ------data: number
    ----} | {
    ------type: Enum.isString,
    ------data: string
    --})
    }

    Anything you use in the source code, you can type it.
  • 1
    When you set excellent set of tslint rules so nobody could fu*k up the code! Feels great but also find this this all over the place after a while:

     */tslint:disable-line no-any */
  • 2
    Worse. Casting as any.
  • 0
    @bartmr That looks so gross. Things like that are why I avoid TS.
  • 1
    @AlgoRythm Actually, is really useful, sometimes even for React props. Like, if you have an input type for an InputBox, when you set the type prop to "password", you start to allow props related to password stuff, and when it isn't a password type, Typescript warns you those password related props are no longer useful.
  • 1
    @AlgoRythm Also, I'm a super lazy developer, so I mostly do structure upfront, something humorously to https://github.com/EnterpriseQualit..., and then I like my programs to almost be written by the auto complete.
Add Comment