Lemdro.id
  • Communities
  • Create Post
  • 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
fedilink
43
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
fedilink
How types make hard problems easy
mayhul.com
external-link
A deep dive into how I use types to the fullest
  • SwordInStone@lemmy.worldOP
    link
    fedilink
    arrow-up
    3
    ·
    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
          3
          ·
          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.

  • 78 users / day
  • 1.05K users / week
  • 2.94K users / month
  • 6.92K users / 6 months
  • 38 local subscribers
  • 20K subscribers
  • 2.12K Posts
  • 32.8K Comments
  • Modlog
  • mods:
  • snowe@programming.dev
  • Ategon@programming.dev
  • MaungaHikoi@lemmy.nz
  • UI: 0.19.8
  • BE: 0.19.9
  • Modlog
  • Legal
  • Instances
  • Docs
  • Code
  • join-lemmy.org