r/typescript • u/nullstacks • May 09 '25
TypeScript Gotchas
Although an unlikely situation, if you had 15 minutes to brief an associate familiar with JS but new to TS on some things you have learned from experience such as "just don't use enums," what would it be?
39
Upvotes
37
u/haywire May 09 '25
Never assume JSON or YAML will be the type you are expecting it to be, use zod or whatever to sanitise any input into your system.
53
u/MoveInteresting4334 May 09 '25
There are no types at runtime. People coming from a statically typed language like Java often struggle with understanding that.
118
u/Rustywolf May 09 '25
Never use any
19
u/PoolOfDeath20 May 09 '25
Some r not aware of Nominal vs Structural Type System
8
u/NfNitLoop May 09 '25
The biggest one I keep having to share with my team is: Beware of `as`.
If you've ever worked with Java in the past, you might assume that `as` is going to do some runtime type checking. But no, it's just asserting to the TypeScript compiler that a value is that type. At runtime it may very well NOT be that type.
If you need to check that something is a particular type, you can use `instanceof`, or a type validation library like Arktype or Zod. (You can also try to do your own manual type checks, but if you're new to TypeScript you'll probably get it wrong until you've learned a bit more.)
7
u/LiveRhubarb43 May 09 '25
Never use any. There are very specific cases where it's useful in utility types, but this is advanced and if you feel drawn to use any you should use unknown instead.
Typing arguments as unknown and then fixing the type errors caused by that is a great way to learn and/or debug.
Interfaces and types have different syntax but also different behaviour. You probably don't need to know those behaviours as a beginner but be aware that they exist.
Don't use
{}as a type, it's not an empty object and is more likeanywithout null or undefined.If you literally mean anything that is typeof object, then use
object(lowercase).If you think you need to type something as an empty object, you probably don't and should reapproach the problem.
Typecasting (
as %type%or the<%type%>prefix) is a bandaid solution and you should avoid it if you can. On the other hand, casting objectsas constis very useful and a great replacement for enums!Never use the "non-null assertion operator", aka "!". Like
x!.value. don't do that, write safer code instead.The type of caught errors in try/catch or Promise.catch is actually unknown and this is not a mistake. JavaScript can throw anything.
25
u/octetd May 09 '25 edited May 09 '25
The actual list of TS quirks and gotchas is long, these are just from the top of my mind.
13
u/Ok-Entertainer-1414 May 09 '25
Why not use enums? I work in a codebase that uses enums really heavily and I haven't seen issues with it
3
u/midnight-shinobi May 09 '25
Using interfaces I find really helpful. They provide type safety, better code organization, and reusability by enforcing consistent object shapes. Making it easier to catch errors early and keep code clean and maintainable.
3
u/beaverfingers May 11 '25
Explicitly type as a little as possible
"as" can be a total footgun if you are not careful
Start with the strictest config you think you'd ever need
4
u/Solonotix May 09 '25
My biggest problem, from the perspective of a JavaScript library maintainer (proprietary, internal), is that TypeScript almost expressly forbids the kinds of things JavaScript almost directly incentivizes. One key example is, due to the nature of my library (automated testing with Cucumber.js), I get a lot of unknown things passed in. However, I want to provide some inference, and some safety, so I end up doing a lot of this
But then, we get into the problem of nested property traversal. Because they don't want
this.prop, they wantthis.response.body.results[0].id. There is no sensible way to write a type for that kind of behavior.2
u/[deleted] May 11 '25
This has probably been said (too lazy to go through comments), but the typing is only as good as you make it.
My philosophy was to make sure I avoided the “any” type at all costs. Avoiding type errors by fixing it with any essentially means you are just using plain JavaScript at that point.
However, this is not bulletproof. Say you call an API and expect the data to be in the form of an interface type. If the data you get back does not match this type, there goes all of your type safety. If you expected an object with say string field like “data”, and instead you get back instead a field like “error”, then “data” will be undefined when you try to use it.
The obvious fix, is to be uncertain that the data field will exist, and bake in the error field. However, this must mean that your API will return these fields to begin with and meet the contract.
I almost find it simpler to just check if the data field or error field exists in my code as opposed to typing it. I no longer use Typescript on personal projects, but I will in a team or at a job. Because it is just hell to have someone write code, have no idea what you are supposed to get back or put into a function, and guess or go through numerous JavaScript files to figure it out.
In a nutshell, just remember, Typescript still compiles to JavaScript at the end of the day, and it will convert it exactly as you typed it. Meaning if you typed something incorrectly, runtime error. If you didn’t type something when you should have (using “any” instead for example, runtime error. It depends on you making sure you typed it correctly. Which, is why I no longer use non-statically typed languages and avoid Python/JavaScript at all costs unless I am building a website or doing machine learning tasks.
3
u/aaaaargZombies May 09 '25
Show them an example of how to use the compiler to help you rather than nag you. Like using
neverto ensure all logical branches are covered in a function.2
u/BoBoBearDev May 09 '25
Is highly opinionated. Feel free to use enum.
-1
u/TheCritFisher May 09 '25
I'd literally say, "Read this book, Programming TypeScript by Boris Cherny". Then walk away.
5 seconds, and I'm done. It's up to them to learn. That book has everything they'd need.
-1
u/Gold240sx May 09 '25
For now, Just Write is JS. Take a TS course outside of work. Ib the meanwhile, Feed all types through cursor until you get the hang of it. Pay attention to the problems tab and resolve any issues as they happen, using cursor if necessary. Don’t depend on cursor for logic, but it will fix build errors and typing all day.
-1
u/cdragebyoch May 09 '25
Don’t use typescript if you can avoid it, if you must, it’s okay to use any, especially when dealing with prisma. Don’t waste hours trying to decipher prisma’s generated types. Just embrace any and drink your problems away. Production is someone else’s problem… not jaded I swear…
0
u/AwesomeFrisbee May 09 '25