TypeScript 3.7 Features

· 1 min read Edit Post

Microsoft recently annouced the beta for Typescript 3.7. This has several useful features coming into the next version of TypeScript soon. My favourite is Optional Chaining.

Optional Chaining

Have you ever had to parse a huge object with several optional fields.

Instead of wrapping the access to these variables in lots of if statements of guarding operators, we can simply use the optional property accessor operator ?..

// Old
if (config && config.auth && config.auth.user) {
  return config.auth.user.userId
}
return undefined
// New
return config?.auth?.user?.userId

We can also access functions and indexes of arrays in the same way.

// Old
if (state && state.deals && state.deals.length > 0) {
  return deals[0]
}
return undefined
// New
return state?.deals?.[0]
// Old
if (fetch) {
  return fetch(`/api/user/${id}`)
}
// New
return fetch?.(`/api/user/${id}`)

Suggested

  • S.O.L.I.D. Principles — 16 May 2018

    Code examples of the SOLID principles.

    • software engineering
    • c#
  • Do you know the CSS box model? — 06 February 2020

    How well do you know the CSS Box Model and the differences between different box-sizing properties?

    • CSS
return home