Lemmy Today
  • Communities
  • Create Post
  • Create Community
  • heart
    Support Lemmy
  • search
    Search
  • Login
  • Sign Up
SwordInStone@lemmy.world to Programming@programming.dev · 4 months ago

"How types make hard problems easy" (or at least reduce cognitive load over time)

mayhul.com

external-link
message-square
12
link
fedilink
51
external-link

"How types make hard problems easy" (or at least reduce cognitive load over time)

mayhul.com

SwordInStone@lemmy.world to Programming@programming.dev · 4 months ago
message-square
12
link
fedilink
How types make hard problems easy
mayhul.com
external-link
A deep dive into how I use types to the fullest
  • traches@sh.itjust.works
    link
    fedilink
    English
    arrow-up
    2
    ·
    edit-2
    4 months ago

    I’ve tried to use that NonEmptyArray type in the past and it was a real pain in the ass getting the type checker to believe that no, for realsies this array is not empty I just checked the length two lines ago. Is there some trick I don’t know or has it gotten smarter about that in recent updates?

    • SwordInStone@lemmy.worldOP
      link
      fedilink
      arrow-up
      4
      ·
      4 months ago

      Have you actually implemented a custom type guard or just asserted size?

      • traches@sh.itjust.works
        link
        fedilink
        English
        arrow-up
        1
        ·
        4 months ago

        This is what I’m talking about:

        Code for copy-pasting:

        type NonEmptyArray<T> = [T, ...T[]];
        
        function neverEmpty<T>(array: T[]): NonEmptyArray<T> | null {  
            if (array.length === 0) return null
        
            return array
        }
        
        • SwordInStone@lemmy.worldOP
          link
          fedilink
          arrow-up
          5
          ·
          4 months ago
          type NonEmptyArray<T> = [T, ...T[]];
          
          function isNonEmptyArray<T>(arr: T[]): arr is NonEmptyArray<T> {
              return arr.length > 0;
          }
          
          function neverEmpty<T>(array: T[]): NonEmptyArray<T> | null {  
              if (!isNonEmptyArray(array)) return null
          
              return array
          }
          
          • traches@sh.itjust.works
            link
            fedilink
            English
            arrow-up
            4
            ·
            4 months ago

            Hey cool, I learned something. Thanks!

            • SwordInStone@lemmy.worldOP
              link
              fedilink
              arrow-up
              1
              ·
              4 months ago

              <3

              more context: https://stackoverflow.com/questions/56006111/is-it-possible-to-define-a-non-empty-array-type-in-typescript

Programming@programming.dev

programming@programming.dev

Subscribe from Remote Instance

Create a post
You are not logged in. However you can subscribe from another Fediverse account, for example Lemmy or Mastodon. To do this, paste the following into the search field of your instance: !programming@programming.dev

Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!

Cross posting is strongly encouraged in the instance. If you feel your post or another person’s post makes sense in another community cross post into it.

Hope you enjoy the instance!

Rules

Rules

  • Follow the programming.dev instance rules
  • Keep content related to programming in some way
  • If you’re posting long videos try to add in some form of tldr for those who don’t want to watch videos

Wormhole

Follow the wormhole through a path of communities !webdev@programming.dev



Visibility: Public
globe

This community can be federated to other instances and be posted/commented in by their users.

  • 129 users / day
  • 1.06K users / week
  • 2.95K users / month
  • 7.15K users / 6 months
  • 74 local subscribers
  • 20.1K subscribers
  • 1.95K Posts
  • 29.9K Comments
  • Modlog
  • mods:
  • snowe@programming.dev
  • Ategon@programming.dev
  • MaungaHikoi@lemmy.nz
  • BE: 0.19.11
  • Modlog
  • Legal
  • Instances
  • Docs
  • Code
  • join-lemmy.org