Skip to content

Idea: introduce tuple types #2196

Answered by Yurchishin
mweststrate asked this question in General
Discussion options

You must be logged in to vote
const tuple = (...Types) => types.refinement(
  `[${Types.map(type => type.name).join(',')}]`,
  types.array(types.union(...Types)), 
  snapshot => {
    if (snapshot.length !== Types.length) return false
    const invalidValue = snapshot
      .findIndex((value, index) => Types[index].is(value) === false)

    return invalidValue === -1
  },
  value => {
    if (value.length !== Types.length) return 'Invalid number of items in tuple'
    return 'Invalid types in tuple'
  }
)

Replies: 10 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by coolsoftwaretyler
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Possible enhancement help/PR welcome Help/Pull request from contributors to fix the issue is welcome level: intermediate
9 participants
Converted from issue

This discussion was converted from issue #805 on July 14, 2024 02:03.