r/typescript • • Jan 28 '22

Typescript Cheat Sheets

Hey folks, I just discovered that Typescript has made 4 Cheat Sheets, guess what, for the 4 most bothering topics :

  • Control Flow Analysis

  • Interfaces

  • Types

  • Classes

Here is the link to theses gems

182 Upvotes

11 comments sorted by

3

u/minireset Jan 28 '22

Thank you for this great link. Very helpful!

2

u/Haaxor1689 Feb 04 '22 edited Feb 04 '22

Nicely put together but there are few mistakes and i correctly named sections.

Primitive types are string | number | boolean. 404 from the example is a number literal type because it's a number that only allows that one literal value just like you can have string literal type like type T = "January" or even boolean literal type type T = true.

I wouldn't say object literal types but only object types since literal types are what I described above.

Your example for tuple type is actually a tagged tupple type a somewhat recent addition to typescript that allows you to name the tuple elements. Plain tupple types looks like this type T = [Location, string].

Template union types is another weird name. You are combining two terms there. It is called template literal types (after JS template string syntax used for this feature) and union types are just what is used together with them.

Also it would be really helpful to list some of the most common utility types with usages since that's what you will most likely need to look up. Pick/Omit, Partial/Rquired, Exclude/Extract or Record are the most common ones. While ReturnType can be useful, you certainly don't use it as often as the other utility types.

Edit: After writing all this I just realized that these are official materials from TS web and that there are sheets other than the one for types. Still I stand by my arguments since I'm coming from the terms used in their full documentation and I'm not sure why they worded some of the points differently.

1

u/Longjumping-Brick-51 Feb 23 '25

Thank you soooo much

1

u/lingdocs Jan 28 '22

Oh sweet. I didn't know about the assert functions but found myself wishing they existed the other day.

1

u/vitriolix Jan 28 '22

very nice, I always love these

1

u/[deleted] Jan 28 '22

woahh never seen this, thanks !

1

u/freezeofseattle Feb 02 '22

super helpful, thanks!

0

u/[deleted] Jan 28 '22

[deleted]

4

u/playest Jan 28 '22

I believe it's a way for a second project to update the interface of a first project. For example, you could update the interface associated with Document because you own code add method to it.

2

u/NoInkling Jan 29 '22 edited Jan 29 '22

Overriding questionable types for built-ins. Augmentations/overrides for the global object. Augmentations/overrides for modules you don't control.

For example, Object.keys() is typed like this by default, which isn't very helpful:

interface ObjectConstructor {
    ...
    keys(o: object): string[];
}

I can come along and do this in my project:

interface ObjectConstructor {
    keys<T extends {}>(o: T): (keyof T)[]
}

(by the way I'm not necessarily implying that this example is a good use case, it could potentially cause issues with code that assumes the original version)

1

u/MrDilbert Jan 28 '22

Good point, I'd like to know as well. I prefer having all of the interface's properties declared in one place.