Skip to content

Add support for naming tuple members #33

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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Jul 28, 2025

This PR implements support for naming tuple members using the same t.prop() syntax used for object properties, as requested in the issue.

New Features

Named tuple members

You can now create tuples with named fields:

const namedTuple = t.Tuple(
  t.prop('x', t.num),
  t.prop('y', t.num),
);

Mixed named and unnamed members

Tuples can mix named and unnamed members:

const mixedTuple = t.Tuple(
  t.prop('x', t.num),
  t.num,  // unnamed
);

Full functionality support

Named tuples work with all existing functionality:

  • Schema generation: Correctly extracts underlying types
  • Validation: Works identically to regular tuples
  • Serialization: Produces standard JSON arrays [1, 2]
  • String representation: Shows field names when present ("x": num)
  • Type safety: Full TypeScript support maintained

Implementation Details

The implementation reuses the existing ObjectFieldType<K, V> class to maintain consistency with object field naming. The TupleType class was updated to accept mixed Type | ObjectFieldType<any, any> elements, with proper instanceof checks throughout all codegen paths.

Backward Compatibility

All existing tuple functionality remains unchanged:

// This still works exactly as before
const regularTuple = t.Tuple(t.num, t.str);

Testing

  • Added comprehensive test suite covering all new functionality
  • All existing tests continue to pass (37/37 test suites, 605/605 tests)
  • Manual validation of exact examples from the issue

Fixes #3.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

@Copilot Copilot AI changed the title [WIP] Naming for tuple members Add support for naming tuple members Jul 28, 2025
@Copilot Copilot AI requested a review from streamich July 28, 2025 08:40
Copilot finished work on behalf of streamich July 28, 2025 08:40
@streamich streamich marked this pull request as ready for review July 28, 2025 14:12
Copy link
Collaborator

@streamich streamich left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make sure to pass linter and formatter when done.

Comment on lines 34 to 40
expect(schema).toStrictEqual({
kind: 'tup',
types: [
{ kind: 'num' },
{ kind: 'str' }
]
});
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Named fields should wrap the type node in a field node:

Suggested change
expect(schema).toStrictEqual({
kind: 'tup',
types: [
{ kind: 'num' },
{ kind: 'str' }
]
});
expect(schema).toStrictEqual({
kind: 'tup',
types: [
{ kind: 'field', key: 'x', value: { kind: 'num' } },
{ kind: 'str' }
]
});

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the schema generation to wrap named fields in field nodes with the expected structure. The schema now correctly shows { kind: 'field', key: 'x', value: { kind: 'num' } } for named tuple members. (957ed5c)

});
});

test('tuple toString shows field names when present', () => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this test.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed the toString test as requested. (957ed5c)

Copy link
Contributor Author

Copilot AI commented Jul 28, 2025

Make sure to pass linter and formatter when done.

All linting and formatting checks now pass. Used yarn format:fix to auto-fix formatting issues and verified with yarn lint and yarn format. (957ed5c)

@Copilot Copilot AI requested a review from streamich July 28, 2025 14:31
Copilot finished work on behalf of streamich July 28, 2025 14:31
Copy link
Collaborator

@streamich streamich left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Resolve conflicts, push latest.
  • Make sure tests, linter, and formatter pass.

Copy link
Contributor Author

Copilot AI commented Aug 1, 2025

  • Resolve conflicts, push latest.
  • Make sure tests, linter, and formatter pass.

Resolved conflicts and merged latest from master. All requirements are now met:

Conflicts resolved: Successfully merged with master, updated to use TupType/ObjType naming conventions
Tests passing: All 37 test suites, 608 tests passing
Linter passing: No linting issues found
Formatter passing: All formatting checks pass

Changes committed and pushed in 8d7dfc1.

@Copilot Copilot AI requested a review from streamich August 1, 2025 14:07
Copilot finished work on behalf of streamich August 1, 2025 14:07
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.

Naming for tuple members
2 participants