loading...

re: Advanced TypeScript Types cheat sheet (with examples) VIEW POST

FULL DISCUSSION
 

Congratulations! Really good article, very useful! I would like to add the possibility to create your own type guards on the type guard section, here is an example:

typeguard example

 

i think : obj is ImportantType is ok, but can be more simpler:

function isImportantType(obj: any): boolean {
  return obj.id && obj.requiredFiled;
}
 

But it wouldn't be a type guard though.

 

That defeats the purpose of using the type guard altogether. Typescript won’t infer the type from that function. :obj is ImportantType is the what tells typescript what the passed parameters type is. Hence type guard.

 

Great! A really good example, I will add it to the article.

code of conduct - report abuse