5 Hacks to Speed Up Your TypeScript Development

1. // @ts-ignore

Some pesky line of code causing your compilation problems? No worries, just throw // @ts-ignore on the line before and now your issues are all solved.

If you're feeling frisky, hack an entire file by adding /* tslint:disable */ to the top of it and now you don't need to worry about any TypeScript issues holding you back for that entire chunk of code.

2. the 'any' type

I don't like types. They're strict and restrictive and irritating and they get everywhere. So how do you eat your types and keep them too? Use the any type!

Say you want to create a new variable that'll probably be a string. Instead of typing it as such and restricting yourself from future changes, just type it as any and now you have free range to change things as necessary!

For example:

const myString : any = 'down with sand';

Want to change that to an integer lately? If you had strictly typed it, you'd have to update the code in two places. Using any, you don't have to worry about that! Genius!

3. Don't type variables

I know I just talked about using any to avoid strict typing, but I'm going to give you a pro tip: you don't have to define any typing at all!

That's right, even in TypeScript, you don't actually have to type any of your scripts! I'm a huge proponent of reducing code, and this goes right along with that best practice. Compare these two lines:

const myString : string = 'down with sand';
const myString = 'down with sand';

You can clearly see how the second line involves less complicated code than the first. Only a Java developer would prefer the former.

So next time your tempted to bloat your codebase with types, just don't. This one simple trick will save your bytes and bytes of code!

4. Don't define a function return type

If we don't have to type our variables, then why do we have to type our function returns? Answer: we do not! HAHA

It's a code smell to restrict your function to a single return type. Why? Because it's extra code!

Compare these two functions:

function sum (a: int, b: int) : int { return a + b; }
function sum (a, b) { return a + b; }

What's the difference between the two? An expert will happily tell you that the latter won't bind you into some pre-optimized state that can never be changed without breaking all your dependent programs. We don't want that now do we. Do we!?

5. Ensure 'strict' is always set to false in your tsconfig

There's one caveat to all of the above. You can't improve your code with these hot hacks if you've got 'strict' set to true in your tsconfig file.

That's why you should never, under any circumstances, ever, not even once, set strict to any value other than false.

Really, ask yourself, do you want to be strict? That's not fun, you Debbie Downer. Let people be free and let your code have the flexibility to live in a world not held down by arbitrary restrictions.

Conclusion

I hope these elite tricks help you in your job. TypeScript is here to stay for sure. But you don't have to let it control who you are or change your style. Follow these 5 simple hacks and you're set for instant success!

Photo by Sonny Ravesteijn on Unsplash