There is no point using TypeScript if you don’t care about types

Kyrylo Reznykov
Dec 29, 2019 · 3 min read
const cat: Cat = (dog as any);

After a year of production experience with TypeScript, I am pretty confident to say that it is a better tool than plain JavaScript and I am ready to use it on my next project. However, I have one condition, The project must forbid all implicit and explicit “any” type usages and type assertions right from the start.


You can’t trust TpeScript types if you allow “any” type in your codebase.

Let's start from an absolute evil — any type. I think it is a huge mistake that by default typescript compiler config has a “strict” rule set to “false”. As a result, a lot of people unfamiliar with technology will leave it as is because of the power of defaults. But for a new project, it is a terrible idea since it lessens TypeScript advantages (that actually what happened with our project as well). To understand what I mean, you can check examples below:

const b: any = 4;
const z: string = b; // no compile time errors
// some time later somewhere in the code
z.toLowerCase(); // runtime error!
const a: number = 10;
const b: any = 4;
const z: string = a + b;
// z is actually type of number
// but compiler now believes that it is a string

So no matter how good your type definitions in general. Even a couple of “any” types in your codebase can cause distrust in the whole type system.


You can’t trust TpeScript types if you allow type assertion.

Another TypeScript feature that easily can make your type system full of lies is type assertions. I believe that all type assertions in production code should be banned with tslint rules except “as const” assertion. That is why:

const catFromApi: unknown = {
meow: () => console.log("meow")
};
// Less than 10 characters to lie TS compiler..
const cat: Dog = catFromApi as Dog;

If you forbid type assertion syntax, but there is a case, when you need to assert your type to help TS compiler, use type guards:

function isCat (obj: unknown): obj is Cat {
if (typeof obj !== 'object' || obj === null) {
return false;
}

// now we with TS compiler know for sure that it is an object.
const
possibleCat: Partial<Cat> = obj; // no errors
return typeof possibleCat.meow === 'function';
}
if (isCat(catFromApi)) {
const cat: Cat = catFromApi;
} else {
throw new Error("Invalid api data, cat is expected.");
}

Yes, much more code, but much fewer possibilities for a mistake...

interface Cat {
meow: Function,
name: string
}
const cat1: Cat = {
meow: () => "meow",
name: 'daisy',
hoof: '' // that line will produce compile error
}
const cat2: Cat = (<Cat> {
meow: () => "meow",
name: 'daisy',
hoof: '' // that line will NOT produce compile error
})

Your teammates have to learn documentation to figure out how to deal with the constraints mentioned above.

That is the most important part. People in JS world without experience in statically typed languages like Java got used to not care about types in general. And a lot of them treat types as an optional thing. As result in every complex situation where we need some generic types, type guards or conditional types, they might try to do type assertion or define “any” without trying to understand what is going on. Creating a potential to spread “lying types” in your codebase.

So having such strict compiler config initially may have some effect on the speed of your team if they are not comfortable with advanced types. But the initial effort to improve knowledge is worth the benefit of a sound type system. And in the long run, your codebase will be less prone to bugs, especially if you don’t have 100% of quality unit test coverage.

JavaScript in Plain English

Learn the web's most important programming language.

Kyrylo Reznykov

Written by

I am a skillful developer who has experience in mobile development, enterprise java applications, and ultra-modern UI interfaces with offline-first design.

JavaScript in Plain English

Learn the web's most important programming language.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade