Skip to content

feat: add TypeScript definitions for utf16.js#30

Merged
ChALkeR merged 14 commits intomainfrom
copilot/fix-utf16-js-issue
Jan 22, 2026
Merged

feat: add TypeScript definitions for utf16.js#30
ChALkeR merged 14 commits intomainfrom
copilot/fix-utf16-js-issue

Conversation

Copy link
Contributor

Copilot AI commented Jan 22, 2026

Add TypeScript types for utf16.js

  • Understand the issue and explore the repository
  • Create utf16.d.ts file with TypeScript type definitions
  • Update package.json to include types for utf16.js export
  • Run linter to verify no issues (existing issues unrelated to changes)
  • Run tests to validate the changes (all 19 tests pass)
  • Request code review and address feedback
  • Run security checks (no alerts found)
  • Use Uint16ArrayBuffer type for consistency
  • Move Uint16ArrayBuffer to array.d.ts for better organization
  • Remove unrelated .gitignore change
  • Remove blank line for cleaner formatting
  • Remove duplicate comment
  • Sync documentation to README
  • Fix JSDoc formatting to match UTF-8 pattern throughout
  • Add documentation for non-even byte length validation

Summary

Successfully added complete TypeScript type definitions for utf16.js module following the same pattern as utf8.d.ts. The implementation includes:

  • Four exported functions with proper overloads for different format parameters
  • Type-safe handling of 'uint16', 'uint8-le', and 'uint8-be' formats
  • Uint16ArrayBuffer type definition in array.d.ts (next to Uint8ArrayBuffer) to prevent SharedArrayBuffer acceptance
  • Complete JSDoc documentation with consistent formatting matching the existing codebase style
  • Documentation for byte length validation (throws on non-even byte length for uint8-le/uint8-be formats)
  • Updated package.json exports to include the types field
  • Updated README.md with detailed API documentation for UTF-16 functions matching the UTF-8 section format
  • All tests passing and no security vulnerabilities detected
Original prompt

This section details on the original issue you should resolve

<issue_title>Types: utf16.js</issue_title>
<issue_description></issue_description>

Comments on the Issue (you are @copilot in this section)


💡 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 AI and others added 4 commits January 22, 2026 10:32
Co-authored-by: ChALkeR <291301+ChALkeR@users.noreply.github.com>
Co-authored-by: ChALkeR <291301+ChALkeR@users.noreply.github.com>
Co-authored-by: ChALkeR <291301+ChALkeR@users.noreply.github.com>
Co-authored-by: ChALkeR <291301+ChALkeR@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix utf16.js issue Add TypeScript definitions for utf16.js Jan 22, 2026
@ChALkeR ChALkeR changed the title Add TypeScript definitions for utf16.js feat: add TypeScript definitions for utf16.js Jan 22, 2026
Copy link
Collaborator

@ChALkeR ChALkeR left a comment

Choose a reason for hiding this comment

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

@copilot Use type Uint16ArrayBuffer = ReturnType<typeof Uint16Array.from>; for consistency with Uint8ArrayBuffer

We don't test these on SharedArrayBuffer as input yet

Co-authored-by: ChALkeR <291301+ChALkeR@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Jan 22, 2026

@copilot Use type Uint16ArrayBuffer = ReturnType<typeof Uint16Array.from>; for consistency with Uint8ArrayBuffer

We don't test these on SharedArrayBuffer as input yet

Updated to use Uint16ArrayBuffer type following the same pattern as Uint8ArrayBuffer (8639a02)

Co-authored-by: ChALkeR <291301+ChALkeR@users.noreply.github.com>
Co-authored-by: ChALkeR <291301+ChALkeR@users.noreply.github.com>
array.d.ts Outdated
Comment on lines 18 to 22

// >= TypeScript 5.9 made Uint16Array templated with <> and defaulted to ArrayBufferLike
// which would incorrectly accept SharedArrayBuffer instances.
// < TypeScript 5.7 doesn't support templates for Uint16Array.
// So this type is defined as a workaround to evaluate to Uint16Array<ArrayBuffer> on all versions of TypeScript.
Copy link
Collaborator

Choose a reason for hiding this comment

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

@copilot

Suggested change
// >= TypeScript 5.9 made Uint16Array templated with <> and defaulted to ArrayBufferLike
// which would incorrectly accept SharedArrayBuffer instances.
// < TypeScript 5.7 doesn't support templates for Uint16Array.
// So this type is defined as a workaround to evaluate to Uint16Array<ArrayBuffer> on all versions of TypeScript.

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 blank line between type definitions (099ae6b)

Co-authored-by: ChALkeR <291301+ChALkeR@users.noreply.github.com>
Copilot AI requested a review from ChALkeR January 22, 2026 13:06
Co-authored-by: ChALkeR <291301+ChALkeR@users.noreply.github.com>
/**
* Encode a string to UTF-16 bytes (strict mode)
*
* Throws on invalid Unicode (unpaired surrogates)
Copy link
Collaborator

Choose a reason for hiding this comment

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

@copilot sync these docs into README.md

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added detailed UTF-16 API documentation to README.md matching the UTF-8 section format (93ccc67)

Co-authored-by: ChALkeR <291301+ChALkeR@users.noreply.github.com>
utf16.d.ts Outdated
* Replaces invalid Unicode (unpaired surrogates) with replacement codepoints `U+FFFD`
* per [WHATWG Encoding](https://encoding.spec.whatwg.org/) specification.
*
* _Such replacement is a non-injective function, is irreversible and causes collisions._
Copy link
Collaborator

Choose a reason for hiding this comment

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

@copilot this misses a newline. Ensure this is synced with README

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed JSDoc formatting to use backslash continuation matching UTF-8 pattern and README (7761ebe)

Co-authored-by: ChALkeR <291301+ChALkeR@users.noreply.github.com>
Copilot AI requested a review from ChALkeR January 22, 2026 13:25
utf16.d.ts Outdated
* import { utf16fromStringLoose, utf16toStringLoose } from '@exodus/bytes/utf16.js'
* ```
*
* _These methods by design encode/decode BOM (codepoint `U+FEFF` Byte Order Mark) as-is._
Copy link
Collaborator

Choose a reason for hiding this comment

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

@copilot and here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed module-level JSDoc formatting with backslash continuation (d149023)

Co-authored-by: ChALkeR <291301+ChALkeR@users.noreply.github.com>
Copilot AI requested a review from ChALkeR January 22, 2026 13:28

Replaces invalid UTF-16 byte sequences with replacement codepoints `U+FFFD`
per [WHATWG Encoding](https://encoding.spec.whatwg.org/) specification.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
Throws on non-even byte length.

@copilot here and in README, for utf16toStringLoose

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added note about throwing on non-even byte length to both utf16toString and utf16toStringLoose in utf16.d.ts and README.md (8aef664)

Co-authored-by: ChALkeR <291301+ChALkeR@users.noreply.github.com>
Copilot AI requested a review from ChALkeR January 22, 2026 13:31
@ChALkeR ChALkeR marked this pull request as ready for review January 22, 2026 13:34
@ChALkeR ChALkeR requested a review from Copilot January 22, 2026 13:34
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adds TypeScript type definitions for the utf16.js module, enabling type-safe usage of UTF-16 encoding/decoding functions. The implementation follows the same pattern as the existing utf8.d.ts file and includes comprehensive JSDoc documentation.

Changes:

  • Added TypeScript definitions with proper function overloads for format parameter type safety
  • Introduced Uint16ArrayBuffer type in array.d.ts for consistency with Uint8ArrayBuffer
  • Updated package.json to expose types in the exports map and files array
  • Enhanced README.md with detailed API documentation matching the UTF-8 section format

Reviewed changes

Copilot reviewed 2 out of 4 changed files in this pull request and generated no comments.

File Description
utf16.d.ts New TypeScript definitions file with type-safe function overloads for utf16fromString, utf16fromStringLoose, utf16toString, and utf16toStringLoose, including comprehensive JSDoc documentation
array.d.ts Added Uint16ArrayBuffer type definition next to Uint8ArrayBuffer for type safety
package.json Added utf16.d.ts to files array and types field to utf16.js exports configuration
README.md Added detailed API documentation for UTF-16 functions with descriptions, behavior notes, and format parameters

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@ChALkeR ChALkeR merged commit c6bf7fe into main Jan 22, 2026
36 checks passed
@ChALkeR ChALkeR deleted the copilot/fix-utf16-js-issue branch January 22, 2026 20:32
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.

Types: utf16.js

2 participants