Skip to content

Add support for tuples - #1910

Open
iObject wants to merge 6 commits into
v1from
1634-add-support-for-tuples
Open

iObject wants to merge 6 commits into
v1from
1634-add-support-for-tuples

Conversation

@iObject

@iObject iObject commented Oct 14, 2026 •

Copy link
Copy Markdown
Collaborator

Adds tuple types to BrighterScript (#1634): syntax, AST, diagnostics, transpilation, documentation, and editor support.

What this adds

A tuple type is an array with a fixed number of positions, each with its own type. The syntax matches TypeScript's and is accepted anywhere a type is expected: parameters, return types, typed assignments, typecasts, type statements, class fields, interface members, inline interfaces, and typed function types.

type StringIntegerPair = [string, integer]

function getPair() as [string, integer]
    return ["hello", 42]
end function

sub main(pair as StringIntegerPair)
    first as [string, integer] = getPair()
    second = parseJson(text) as [string, integer]
end sub

Supported shapes:

  • fixed: [string, integer], including the empty tuple []
  • optional trailing elements: [float, float, float?]
  • a rest element: [string, ...integer[]]
  • nested tuples, arrays of tuples ([integer, integer][]), and unions inside or around a tuple
  • element types may be groupings, typed function types, keyword types, or multidimensional arrays
  • elements separated by commas and/or newlines, with trailing and leading commas and comments allowed

New diagnostics (all errors):

  • tuple-required-element-after-optional: [string?, integer]
  • tuple-rest-element-must-be-last: [...string[], integer]
  • tuple-rest-element-cannot-be-optional: [...string[]?]

In .brs files a tuple type gets the usual bs-feature-not-supported warning, anchored at the [.

Typing

A tuple is typed as an array of its element types, so [string, integer] is Array<string or integer>. A rest element contributes its item type (through type aliases), so [string, ...integer[]] and [string, ...Nums] (where type Nums = integer[]) are both Array<string or integer>. Type names inside a tuple are resolved and validated like any other type reference, so [Person, Missing] reports Cannot find name 'Missing'.

Transpilation

A tuple is an ordinary roArray at runtime, so tuple types transpile to dynamic, the same as typed arrays. This also applies to typedefs (type Pair = dynamic), matching how typed arrays and inline interfaces are already emitted there.

Editor support

Hover, go-to-definition, and completions work on type names inside a tuple. Completions previously only treated a position as a type when the token was adjacent to as, so a name after or, [, or , offered runtime variables instead of type names. That was already broken for union types; completions now also use util.isInTypeExpression, which fixes unions and tuples together.

Implementation notes

  • Parser: tupleType() / tupleTypeElement() are called from getTypeExpressionPart when a type starts with [. consumeAsTokenAndTypeExpression now accepts [ after as, which is the one change that enables tuples in every as position. Before this PR [ there was a parse error, so there is no conflict with array literals or indexing (both covered by tests).
  • AST: TupleTypeExpression holds TupleTypeElementExpressions; each element wraps a TypeExpression plus optional ... and ? tokens. util.getTypeExpressionName renders them, so TypeExpression.getName() returns the written form. An element's typeExpression is optional because the parser omits it in BrightScript mode, where a custom type name is skipped with its own diagnostic.
  • Error recovery: elements only continue after a comma, so an unclosed multi-line tuple does not consume end sub and the following functions as type names. Each ordering diagnostic is reported once and always has a location.
  • Typed-assignment probe: detecting x as <type> = value speculatively parses the type and backtracks, but previously kept any diagnostics from that attempt, so errors inside the type were reported twice. The probe now drops them. This also fixes the same latent duplication for inline interfaces.

Tests

src/parser/tests/expression/TupleTypeExpression.spec.ts (33 tests): every shape and position above, each diagnostic with its exact location, .brs mode, error recovery and malformed elements, cloning (including an element with no type in .brs mode), typing of fixed and rest elements, typedefs, transpile output, type names resolving inside tuples, and hover / go-to-definition / completions on a type name inside a tuple. CompletionsProcessor.spec.ts gains a union + tuple completions test. Full suite: 5242 passing.

Docs

New docs/tuples.md (syntax, optional and rest elements, the three errors, transpilation, limitations) and an entry in docs/readme.md.

Limitations

  • Indexing a tuple (pair[0]) yields the union of its element types rather than the type at that position, and a tuple type is not checked for length or per-position compatibility against array literals or other tuples.
  • A rest element's type is not checked to be an array.
  • Elements cannot be labeled, tuples cannot be readonly, and array literals are never inferred to be tuples (x = ["a", 1] is Array<string or integer>).
  • A rest element must be last; leading and middle rest elements ([...string[], integer]) are not supported.
  • A tuple typecast followed by an index must be parenthesized: (data as [string, integer])[0], the same as with typed arrays.

- Add TupleTypeExpression and TupleTypeElementExpression, parsed wherever a
  type is expected when it starts with `[`: `[string, integer]`, optional
  elements (`integer?`), a rest element (`...integer[]`), `[]`, nested
  tuples, and arrays of tuples (`[integer, integer][]`).
- Flag a required element after an optional one, a rest element that is
  not last, and an optional rest element.
- Tuple types transpile to `dynamic` (in typedefs too, the same as typed
  arrays) and are a BrighterScript-only feature.
- Drop diagnostics produced by the speculative typed-assignment probe, so
  errors inside the type are not reported twice.
- Add docs/tuples.md and a readme entry.
@iObject iObject linked an issue Oct 14, 2026 that may be closed by this pull request
@iObject
iObject changed the base branch from master to v1 October 14, 2026 13:45
@iObject
iObject requested a balanced review from Copilot October 14, 2026 13:45
@iObject iObject added the create-package create a temporary npm package on every commit label Oct 14, 2026

This comment was marked as outdated.

@iObject iObject changed the title 1634 add support for tuples Add support for tuples Oct 14, 2026
@rokucommunity-bot

Copy link
Copy Markdown
Contributor

Hey there! I just built a new temporary npm package based on 50406f5. You can download it here or install it by running the following command:

npm install https://github.com/rokucommunity/brighterscript/releases/download/v0.0.0-packages/brighterscript-1.0.0-alpha.57-1634-add-support-for-tuples.20261014134749.tgz

`[string, ...integer[]]` resolved to `Array<string or Array<integer>>`;
the rest element's array type is now unwrapped (through type aliases) to
its item type, giving `Array<string or integer>`.
@rokucommunity-bot

Copy link
Copy Markdown
Contributor

Hey there! I just built a new temporary npm package based on c75143b. You can download it here or install it by running the following command:

npm install https://github.com/rokucommunity/brighterscript/releases/download/v0.0.0-packages/brighterscript-1.0.0-alpha.57-1634-add-support-for-tuples.20261014140215.tgz

@iObject
iObject requested a balanced review from Copilot October 14, 2026 14:06
@iObject iObject removed the create-package create a temporary npm package on every commit label Oct 14, 2026

This comment was marked as outdated.

The parser omits it in BrightScript mode, where a custom type name is
skipped with its own diagnostic, so the field and constructor option now
say so. Add a test that clones such an element.
Completions treated a position as a type only when the token was adjacent
to `as`, so a name after `or`, `[`, or `,` (`string or Per|son`,
`[Per|son, integer]`) offered runtime variables instead of types. Use the
AST (util.isInTypeExpression) as well, which fixes unions and tuples.
@iObject

iObject commented Oct 16, 2026

Copy link
Copy Markdown
Collaborator Author

@copilot resolve the merge conflicts in this pull request

…ples

# Conflicts:
#	src/util.ts

Co-authored-by: iObject <2446955+iObject@users.noreply.github.com>

Copilot AI commented Oct 16, 2026

Copy link
Copy Markdown
Contributor

@copilot resolve the merge conflicts in this pull request

Merged the updated v1 base and resolved the conflict in src/util.ts. Merge commit: ebd17dfa.

@iObject
iObject marked this pull request as ready for review October 16, 2026 19:53

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add support for tuples

3 participants