Orbi Camp
  • Communities
  • Create Post
  • Create Community
  • heart
    Support Lemmy
  • search
    Search
  • Login
  • Sign Up
SwordInStone@lemmy.world to Programming@programming.dev · 1 year ago

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

mayhul.com

external-link
message-square
12
fedilink
47
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 · 1 year ago
message-square
12
fedilink
  • traches@sh.itjust.works
    link
    fedilink
    English
    arrow-up
    1
    ·
    1 year 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
      ·
      1 year 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
        ·
        1 year ago

        Hey cool, I learned something. Thanks!

        • SwordInStone@lemmy.worldOP
          link
          fedilink
          arrow-up
          1
          ·
          1 year 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: [email protected]

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 [email protected]



Visibility: Public
globe

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

  • 197 users / day
  • 1.69K users / week
  • 3.27K users / month
  • 7.7K users / 6 months
  • 1 local subscriber
  • 24.1K subscribers
  • 1.13K Posts
  • 12.5K Comments
  • Modlog
  • mods:
  • snowe@programming.dev
  • Ategon@programming.dev
  • MaungaHikoi@lemmy.nz
  • UlrikHD@programming.dev
  • BE: 0.19.8
  • Modlog
  • Instances
  • Docs
  • Code
  • join-lemmy.org