DEV Community

Cover image for Advanced TypeScript Types cheat sheet (with examples)

Advanced TypeScript Types cheat sheet (with examples)

Ibrahima Ndaw on June 15, 2020

TypeScript is a typed language that allows you to specify the type of variables, function parameters, returned values, and object properties. Here...
Collapse
 
khrisl33t profile image
kHRISl33t

A really well-written article with good examples. Good job!

Would like to add one more thing I like to use and you might find it interesting:

type Maybe<T> = T | null;

function foo(bar: Maybe<string>) {
  if (!bar) {
    // handle if bar is null
  }
  // do stuff if value not null.
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
ionellupu profile image
Ionel Cristian Lupu

What are some really good real world examples for this? (3,4 examples).

Wouldn't the optional operator be enough?

function foo(bar?: string) {
  if (!bar) {
    // handle if bar is falsy
  }
  // do stuff if value not falsy.
}
Enter fullscreen mode Exit fullscreen mode

The only thing I can think of is when you really need the parameter to be exactly null. But I never have this use case in the projects I work on, so I think something is fishy :))

Collapse
 
michaeljota profile image
Michael De Abreu

The difference between the two, is that in Maybe the value is required, and in the second one is optional. So, you can have Maybe values and those values would be either defined or not, but would still be required. I have found this to be useful in React.

Collapse
 
khrisl33t profile image
kHRISl33t

Actually it's just personal preference to have null values instead of undefined.
The example I provided is actually not that great, because I tend to use this more with objects and their properties.

Collapse
 
chico1992 profile image
chico1992

You should explicitly check for bar===null as in your case the empty string will also make !bar true

Collapse
 
khrisl33t profile image
kHRISl33t

you are right, thanks :)

Collapse
 
cesarkohl profile image
Cesar Kohl

Great article! Please remember to use semicolons at the end of the lines.

Collapse
 
ibrahima92 profile image
Ibrahima Ndaw

The semicolon is not mandatory, I prefer to not use it. Thanks for reading BTW

Collapse
 
garretharp profile image
Garret

No semicolon makes it look so much cleaner:)

Collapse
 
randalvance profile image
Randal Vance Cunanan

Use prettier, you don't even have to type it yourself.

Collapse
 
cesarkohl profile image
Cesar Kohl

It's not mandatory if you wanna go against Google: google.github.io/styleguide/jsguid... . Please reconsider.

Thread Thread
 
garretharp profile image
Garret

Theres a million different style guides, use whatever you want. No one is forced to use a specific one.

Collapse
 
psiho profile image
Mirko Vukušić • Edited

:) so glad I don't have to see or type those anymore

Collapse
 
mutterpedro profile image
Pedro Mutter

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

Collapse
 
ibrahima92 profile image
Ibrahima Ndaw

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

Collapse
 
skye profile image
Skye Wu

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

function isImportantType(obj: any): boolean {
  return obj.id && obj.requiredFiled;
}
Collapse
 
toddmath profile image
Todd Matheson

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.

Thread Thread
 
skye profile image
Skye Wu

thanks for explain

Collapse
 
mutterpedro profile image
Pedro Mutter

But it wouldn't be a type guard though.

Thread Thread
 
skye profile image
Skye Wu

thanks for explain

Collapse
 
marcuzy profile image
Oleg Krasavin

Seems typescript is getting overcomplicated.

Collapse
 
luisaceituno profile image
Luis Aceituno

How so? These have been in it for a long time now. You are also not required to use them, since they're mostly syntactic sugar. It does make life easier a lot of times though!

Collapse
 
ibrahima92 profile image
Ibrahima Ndaw

Absolutely.

Collapse
 
bbehzadi profile image
bbehzadi

Good article, to the point and concise with good examples.

I wish I knew Partial<T> - that I just learnt here before. This is how I implemented it:

export type ObjectWithOptionalProps<T> = { [key in keyof T]?: T[key] };
Collapse
 
yurikaradzhov profile image
Yuri Karadzhov

Good article, nice to have everything combined in one place.

Collapse
 
ibrahima92 profile image
Ibrahima Ndaw

Thanks for reading too.

Collapse
 
tsimbalar profile image
Thibaud Desodt

Really concise summary / recap.

Will share that with my team, thanks a lot !

Collapse
 
ibrahima92 profile image
Ibrahima Ndaw

Great! I'm glad this article could help.

Collapse
 
welingtonveiga profile image
Welington Veiga • Edited

It is a list of "easy to get" examples of the most interesting TS typing features, it's quite handy having them packed like this, thank you!

Collapse
 
ibrahima92 profile image
Ibrahima Ndaw

Great! I'm glad you find value in this article.

Collapse
 
evgenyartemov profile image
Evgeny Artemov

OMG, what I see! The first well wrote and detailed explanation of the typescript types system!!! Thanks a lot!!!

Collapse
 
ibrahima92 profile image
Ibrahima Ndaw

Awesome! I'm glad you find value on it.

Collapse
 
richardburd profile image
Richard Burd

Thank you for writing this article Ibrahima! I have been looking to update my beginner's TypeScript cheat sheet with utility types and this is the best explanation I've seen so far!!

Collapse
 
utkarshyadav profile image
Utkarsh Yadav • Edited

Great! need this very much.

Collapse
 
yvettelau profile image
YvetteLau

Thanks for sharing!
I want to translate it into Chinese to help more developers, Can I get your authorization?

I will give credit at the top of the article.

Collapse
 
ibrahima92 profile image
Ibrahima Ndaw

Yes, you can translate it. Just make sure to give me credit.

Collapse
 
tcelestino profile image
Tiago Celestino

I saved in my Pocket and now I read and I can sau: great article. Thanks man!

Collapse
 
vishwamlr profile image
Vishwam Sirikonda

Very concise and useful, This is the best blog that I read in last one month. Thanks a lot

Collapse
 
zhuima profile image
追马

Thanks for sharing!

Collapse
 
kamalhossain profile image
Kamal Hossain

How to did you added hoverable link in markdown?
In TypeScript Guide (5 Part Series) ?