Hacker News new | past | comments | ask | show | jobs | submit login

Oh wow, I did not know that this existed. Definitely gonna need a linter to make sure it's never used.

Do you know why it was added? Ive used an annotation that disables type checking on a couple unit tests, but this seems much harder to notice




Definitely not a TS expert but I think it’s for cases where you know something “better” than it is known to the type system.

An example that pops to mind is how React’s createContext() typings for some reason require a non-bill default value, but you may not have such a value at the time the call is made. Personally I think it’s totally legit to just do createContext<T>(null!). I don’t think this leads to errors that won’t get quickly noticed.

But if you get carried away, it’ll bite you for sure, especially in code that has more going on than define a React context.


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.


> 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.




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: