Lemmings.world
  • 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
    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.

  • 203 users / day
  • 503 users / week
  • 2.59K users / month
  • 6.99K users / 6 months
  • 71 local subscribers
  • 20.1K subscribers
  • 2.16K Posts
  • 33.2K Comments
  • Modlog
  • mods:
  • snowe@programming.dev
  • Ategon@programming.dev
  • MaungaHikoi@lemmy.nz
  • BE: 0.19.11
  • Modlog
  • Instances
  • Docs
  • Code
  • join-lemmy.org