question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

✨ Introducing `ts-belt` v4 (release candidate)

See original GitHub issue

hello there! 👋

I have been working on the new version of ts-belt with support for Async* modules for a quite long time, and now I feel it’s a great time to publish (at least) a release candidate version. It’s also an excellent opportunity to gather feedback from you 😃 The bad news is, the docs for these modules are missing at the moment (I’m working on it!), but let me describe the essentials of each module:

Installation

yarn add @mobily/ts-belt@next

AsyncData

AsyncData contains a variant type for representing the different states in which a value can be during an asynchronous data load.

There are four possible states: Init, Loading, Reloading, and Complete.

type AsyncData<T> = Init | Loading | Reloading<T> | Complete<T>
import { AD } from '@mobily/ts-belt'

Variant constructors:

  • AD.Init
  • AD.Loading
  • AD.Reloading(value)
  • AD.Complete(value)
  • AD.makeInit()
  • AD.makeLoading()
  • AD.makeReloading(value)
  • AD.makeComplete(value)

Functions:

  • AD.isInit
  • AD.isLoading
  • AD.isReloading
  • AD.isComplete
  • AD.isBusy
  • AD.isIdle
  • AD.isEmpty
  • AD.isNotEmpty
  • AD.toBusy
  • AD.toIdle
  • AD.getValue
  • AD.getWithDefault
  • AD.getReloading
  • AD.getComplete
  • AD.map
  • AD.mapWithDefault
  • AD.flatMap
  • AD.tapInit
  • AD.tapLoading
  • AD.tapReloading
  • AD.tapComplete
  • AD.tapEmpty
  • AD.tapNotEmpty
  • AD.all
  • AD.fold

Example: https://codesandbox.io/s/cool-star-6m87kk?file=/src/App.tsx

AsyncDataResult

AsyncDataResult is basically an alias of AsyncData<Result<Ok, Error>>. This variant type can be used to represent the different states in which a data value can exist while being loaded asynchronously, with the possibility of either success or failure.

type AsyncDataResult<A, B> = AsyncData<Result<A, B>>
import { ADR } from '@mobily/ts-belt'

Variant constructors:

  • ADR.Init
  • ADR.Loading
  • ADR.ReloadingOk(value)
  • ADR.ReloadingError(error)
  • ADR.CompleteOk(value)
  • ADR.CompleteError(error)
  • ADR.makeInit()
  • ADR.makeLoading()
  • ADR.makeReloadingOk(value)
  • ADR.makeReloadinError(error)
  • ADR.makeCompleteOk(value)
  • ADR.makeCompleteError(error)

Functions:

  • ADR.isOk
  • ADR.isError
  • ADR.isReloadingOk
  • ADR.isReloadingError
  • ADR.isCompleteOk
  • ADR.isCompleteError
  • ADR.getOk
  • ADR.getReloadingOk
  • ADR.getCompleteOk
  • ADR.getError
  • ADR.getReloadingError
  • ADR.getCompleteError
  • ADR.map
  • ADR.mapError
  • ADR.flatMap
  • ADR.tap
  • ADR.fold
  • ADR.foldOk
  • ADR.toAsyncData

Example: https://codesandbox.io/s/brave-cloud-ov30h7?file=/src/App.tsx

AsyncOption

Same as Option but for handling asynchronous operations.

type AsyncOption<T> = Promise<Option<T>>
import { AO } from '@mobily/ts-belt'

Variant constructors:

  • AO.make(promise)
  • AO.resolve(value)
  • AO.reject()

Functions:

  • AO.filter
  • AO.map
  • AO.flatMap
  • AO.fold
  • AO.mapWithDefault
  • AO.match
  • AO.toNullable
  • AO.toUndefined
  • AO.toResult
  • AO.getWithDefault
  • AO.isNone
  • AO.isSome
  • AO.tap
  • AO.contains
  • AO.flatten

AsyncResult

Same as Result but for handling asynchronous operations.

type AsyncResult<A, B> = Promise<Result<A, B>>
import { AR } from '@mobily/ts-belt'

Variant constructors:

  • AR.make(promise)
  • AR.resolve(value)
  • AR.reject(error)

Functions:

  • AR.flatMap
  • AR.fold
  • AR.map
  • AR.mapWithDefault
  • AR.getWithDefault
  • AR.filter
  • AR.match
  • AR.toNullable
  • AR.toOption
  • AR.toUndefined
  • AR.isOk
  • AR.isError
  • AR.tap
  • AR.tapError
  • AR.handleError
  • AR.mapError
  • AR.catchError
  • AR.recover
  • AR.flip
  • AR.flatten

Minor changes

  • ✨ added A.sample (gets a random element from provided array)
  • ✨ added O.all (transforms an array of Option(s) into a single Option data type)
  • ✨ added R.all (transforms an array of Result(s) into a single Result data type)
  • ✨ added R.filter (returns Ok(value) if result is Ok(value) and the result of predicateFn is truthy, otherwise, returns Error)
  • 🐛 fixed up the groupBy signature

Breaking changes

ts-belt@v4 does not support Flow, due to a lack of proper features in flowgen, sorry about that!

Feel free to post your thoughts, any kind of feedback would be greatly appreciated! 💪

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:16
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
Nodoniskocommented, Nov 14, 2022

Looks good, but I am starting to get lost in all that shortcuts AD, ADR, AO… Did you thinked about shipping also “non-shortcut” version of these?

2reactions
lulldevcommented, Nov 14, 2022

How about alternative match/with from ts-pattern out of box? something like fold but for handling all or each states?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Issues · mobily/ts-belt - GitHub
mobily / ts-belt Public. Sponsor ... Issues: mobily/ts-belt. ✨ Introducing ts-belt v4 (release candidate). #51 opened on Nov 2 by mobily. Open 8....
Read more >
Marcin Dziewulski (@__marcin_) / Twitter
Introducing `ts-belt` v4 (release candidate) · Issue #51 · mobily/ts-belt. hello there! 👋 I have been working on the new version of ts-belt...
Read more >
Introduction | ts-belt - GitHub Pages
ts-belt combines all of the good things you can find in other similar libraries: the developer friendly data-first approach, good documentation, great ...
Read more >
Announcing TypeScript 4.6 RC - Microsoft Developer Blogs
Today we're excited to announce our Release Candidate (RC) of TypeScript 4.6! Between now and the stable release of TypeScript 4.6, we expect ......
Read more >
ANNOUNCING THE GRAYLOG V4.3 RELEASE CANDIDATE
If those fail, Graylog aborts the startup without running any further actions, e.g. migrations, periodicals, etc. This change introduces two ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found