Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type error with nested conditional rendering array of blocks #134

Open
jam-fran opened this issue Jul 11, 2024 · 1 comment
Open

Type error with nested conditional rendering array of blocks #134

jam-fran opened this issue Jul 11, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@jam-fran
Copy link

jam-fran commented Jul 11, 2024

First off, thank you @raycharius for building such an insanely useful library. Can't imagine life without it!

I'm running into a type error when I'm attempting to render a conditional within a conditional. Here's a minimal reproduction:

import { HomeTab, Section, setIfTruthy } from 'slack-block-builder'

const arrayOfNumbers = [1, 2, 3]

export const ParentBlocks = () =>
  HomeTab().blocks(
    Section().text('Hello world'),
    setIfTruthy(true, ChildBlocks())
  )

const ChildBlocks = () => [
  Section().text('This is a child.'),
  setIfTruthy(
    true,
    arrayOfNumbers.map((number) => Section().text(number.toString()))
  ),
]

The error I'm getting is on the setIfTruthy(true, ChildBlocks()) call, and it reads Type 'SectionBuilder[]' is not assignable to type 'Undefinable<ViewBlockBuilder>'..

The issue seems to stem from the fact that the setIfTruthy conditional in ChildBlocks is returning an array of blocks.

If I update ChildBlocks to return one block instead, it works:

 const ChildBlocks = () => [
  Section().text('This is a child.'),
  setIfTruthy(true, Section().text('This works')),
] 

I've attempted wrapping the array in a BlockCollection, but that doesn't work, I believe because BlocksCollection builds its blocks.

Any tips on how to solve or work around this issue would be very much appreciated!

@jam-fran jam-fran added the bug Something isn't working label Jul 11, 2024
@jam-fran
Copy link
Author

jam-fran commented Jul 11, 2024

I suppose a related question that is causing me to run into a similar type error is: How do you render an array of an array of blocks? For example:

const arrayOfNumbers = [1, 2, 3, 4, 5]

export const Home = () => {
  return HomeTab()
    .blocks(
      Section().text('Hello world!'),
      arrayOfNumbers.map((n) => [
        // If I try to render an array of blocks here I get a type error. 
      ])
    )
    .buildToObject()
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant