2
codecrow
56d

when using typescript, how often do you use 'any' versus the actual type? and for objects, do you use models or do you use 'any' and parse the objects?

Comments
  • 0
    I used plenty of any when I first learned it
  • 0
    @volttide i feel it's wrong. but it's soo easy to use. also, how often to you specify the type on a method ?
  • 1
    @codecrow any and unknown is easy to use, but later on when the project goes big, I felt that the technical debt was too much, so now, I always spend time to create proper classes, interfaces and types.
  • 5
    Only for Record<string, any> when the occasion calls for it. Which is rare.

    But just don’t. Don’t use any, if you can avoid it, and definitely not because you’re too lazy to create proper models.
  • 6
    Using any often basically defeats the purpose of using typescript in the first place. I only use any if I absolutely have to.
  • 3
    Never. I hate typescript so I avoid it but the few times I use it I turn off implicit any because what's the point otherwise.
  • 1
    I use it in test data when I'm too lazy to specify Record<string, any>, for example when returning JSON data in a mock call. If I don't cast the object to any it'll clog up the type system and start complaining about missing properties

    Other than that, never
  • 1
    If I don't know the type before hand, I use 'unknown' or generic type parameters.

    'any' opens the floodgates to all sorts of avoidable bugs.
  • 5
    Only when bridging js apps that legitimately have no means of resolving the type until runtime.

    Or when I have an algorithm that can take a typed argument, but doesn't care what the type of that argument is after the fact. Builders will run into this one occasion. Same as reducing to Object in C#.

    This will remain the case until Typescript gains support for higher kinded types.
  • 0
    i'm currently working on changes to a 10 years old TypeScript app.

    i use var.
    meaning that I avoid using the typescript unless some "build" error forces me to.

    because i don't need extra complications on top of how shitty the app itself is.
  • 2
    I’m currently building a blueprint editor for a project of mine with TypeScript and so far the only instances where I’ve needed “any” were due to the highly generic structures that I have. For instance NodeOutput<T> where it can output literally anything so it makes sense to use “any” in that case. But out of all the TS projects I’ve been working on I can safely say that it’s very rare to NEED “any”.
Add Comment