5 ms·
I write TypeScript every day, and sometimes it can be pretty useful. Like, if you have an interface... interface MyInterface { someValue?: string;
by TranquilMarmot 6y ago
I write TypeScript every day, and sometimes it can be pretty useful.
Like, if you have an interface...
interface MyInterface {
someValue?: string;
/** Let's say this only exists if `someValue` exists */
someOtherValue?: string;
}
And then in some code later...
const myFunction = (someParam: MyInterface) => {
if (someParam.someValue) {
myOtherFunction(someParam);
}
}
// let's say this function is only EVER called if `someValue` exists
// we know more than the type checker in this case
const myOtherFunction = (someParam: MyInterface) => {
console.log(someParam.someValue!.length);
console.log(someParam.someOtherValue!.length);
}
There are definitely more "correct" ways to do this (i.e. using `Partial` or re-casting to another type) but sometimes there are edge cases that aren't worth doing things more correctly (like, if this is only happening in one spot)
Lots of folks make the mistake of assuming the TypeScript is strongly typed, but it's not really... I like to think of its type system as "squishy" in that you can very very easily fake things or trick it into compiling. I think of the types more as comments and hints about the code and not there to ensure program correctness.
This is actually one of my favorite features of TypeScript; it's not as strict as, say, Java or Scala, and it lets you get away with some things without having to rework all of your types. This can be a bit of a footgun, BUT if you're also writing unit tests and integration tests then you'll have all of your bases covered.
- dmitryminkovsky 6y ago> but sometimes there are edge cases that aren't worth doing things more correctly (like, if this is only happening in one spot) Yep! > Lots of folks make the mistake of assuming the TypeScript is strongly typed, but it's not really... I like to think of its type system as "squishy" Totally! That’s what makes it so nice. Great way to describe it.