jian's typescript best practices Cheat Sheet

A cheat sheet in English by mynameisjian

Best practices for typescripts

8,928 views 1,860 downloads 11 Dec 2018 History
3.7 avg · 0 ratings

General Types

✘ use String
✘ use Number
✘ use Object
✘ use Boolean

Function Overloads

✘ put more general overloads before more specific ones
✔︎ sort overloads more general signatures after more specific signatures
✘ write several overloads that differ only in trailing parameters
✔︎ use optional parameters whenever possible
✘ write overloads that differ by type in only one argument position
✔︎ use union types whenever possible

Strict config­uration

"­for­ceC­ons­ist­ent­Cas­ing­InF­ile­Nam­es": true
"­str­ict­": true
"­no-­nul­l-k­eyw­ord­": true
"­noI­mpl­ici­tRe­tur­ns": true
"­noU­nus­edL­oca­ls": true

Managing non existing values

{ "­no-­nul­l-k­eyw­ord­": true }
type Option­al<­T> = T | undefined

Callback Types

✘ use the return type any
✔︎ use the return type void
✘ use optional parameters in callbacks unless you really mean it
✔︎ write callback parameters as non-op­tional
✔︎ write callback parameters as non-op­tional

Overloads and Callbacks

✘ write separate overloads that differ only on callback arity
✔︎ write a single overload using the maximum arity

Comments

No comments yet.

Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.