Skip navigation
In TypeScript, there are at least 4 ways to implement immutability.
0:00 / 1:56

In TypeScript, there are at least 4 ways to implement immutability.

Cory House

@housecor166K followers

Visit on X
280 likesDownloadOriginal post

13,603 views

In TypeScript, there are at least 4 ways to implement immutability. Here's how I choose between them.

10 Comments

Comments from the original post on X (Twitter)

  • Georg Unterholzner's profile picture

    @georg_dev

    Very well explained, Cory!

    2 likes

  • lazy_coder's profile picture

    @real_lazy_coder

    Question: At what point in the development cycle do you begin to worry about immutability? For solo or very small teams focused on deploying asap should this be a priority?

    2 likes

  • Tanguy Krotoff's profile picture

    @tkrotoff

    Fully-typed and fully-tested deepFreeze:

    1 like

  • 𝕃𝕏𝔼's profile picture

    @lxe

    we need a 5th way

  • Cory House's profile picture

    @housecor

    I’m likely forgetting at least two

    1 like

  • aiiiden 💫💖's profile picture

    @aiiiden0

    @ezdubs_bot english to japanese

  • Nitesh ₿⚡️'s profile picture

    @nitesh_btc

    Only in JavaScript, we declare something as const but then add an `as const` at the end of it to say, please remain immutable.

  • Oliver's profile picture

    @jsdotoliver

    So how would you choose between the typescript versions? Mainly when to use `as const` vs `readonly/ReadOnly`

  • Cory House's profile picture

    @housecor

    readonly is the only granular option. Otherwise I generally prefer “as const”

    2 likes

  • Cory House's profile picture

    @housecor

    Full course with a 20% discount: