Skip to content

Migrate /numbers to TS part 2#853

Open
quietbits wants to merge 12 commits intotypescript-migrationfrom
tsm-numbers-2
Open

Migrate /numbers to TS part 2#853
quietbits wants to merge 12 commits intotypescript-migrationfrom
tsm-numbers-2

Conversation

@quietbits
Copy link
Contributor

@quietbits quietbits commented Feb 13, 2026

  • Migrated index, sc_int, and xdr_large_int files to TS.
  • Added tests for the above files, and migrated old ones to keep the same test cases (i256_test.js, scint_test.js, and scval_test.js)
  • Added temporary type declarations for src/index.js
  • Some cleanup

@quietbits quietbits linked an issue Feb 13, 2026 that may be closed by this pull request
5 tasks
@github-actions
Copy link

Size Change: +8.67 kB (+0.25%)

Total Size: 3.52 MB

Filename Size Change
dist/stellar-base.js 2.6 MB +6.17 kB (+0.24%)
dist/stellar-base.min.js 924 kB +2.5 kB (+0.27%)

compressed-size-action

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 continues the migration of the /numbers directory to TypeScript, completing "part 2" of the migration. The changes convert three key files (xdr_large_int.js, sc_int.js, and index.js) to TypeScript, along with their associated tests, while maintaining complete test coverage by migrating existing test cases from JavaScript to TypeScript.

Changes:

  • Migrated src/numbers/xdr_large_int.js, src/numbers/sc_int.js, and src/numbers/index.js to TypeScript with proper type annotations
  • Created comprehensive TypeScript test files (xdr_large_int.test.ts, sc_int.test.ts, index.test.ts) that preserve all test cases from the removed JS test files
  • Added type declaration files in type_validation/numbers/ for the migrated modules
  • Updated type declarations in types/stellar__js-xdr/index.d.ts to include the slice method and reordered union types for consistency
  • Created src/index.d.ts as a temporary type declaration file during the migration process
  • Updated documentation comments to use new ScInt() consistently instead of just ScInt()
  • Added proper .js file extensions to imports as required for ES modules

Reviewed changes

Copilot reviewed 14 out of 23 changed files in this pull request and generated no comments.

Show a summary per file
File Description
types/stellar__js-xdr/index.d.ts Added slice method declaration to LargeInt and reordered union types for consistency
type_validation/numbers/xdr_large_int.d.ts New type declarations for XdrLargeInt class
type_validation/numbers/sc_int.d.ts New type declarations for ScInt class
type_validation/numbers/index.d.ts New type declarations for numbers index module exports
type_validation/numbers/{uint256,uint128,int256,int128}.d.ts Updated constructor parameter types to use consistent ordering
test/unit/scval_test.js Deleted - tests migrated to TypeScript
test/unit/scint_test.js Deleted - tests migrated to TypeScript
test/unit/i256_test.js Deleted - tests migrated to TypeScript
test/unit/numbers/xdr_large_int.test.ts New comprehensive TypeScript tests for XdrLargeInt
test/unit/numbers/sc_int.test.ts New comprehensive TypeScript tests for ScInt, includes migrated tests
test/unit/numbers/index.test.ts New TypeScript tests for scValToBigInt and related functions
test/unit/numbers/{uint256,uint128,int256,int128}.test.ts Updated imports to use .js extensions and added migrated tests from i256_test.js
src/numbers/xdr_large_int.ts Migrated from JS with full type annotations and improved type safety
src/numbers/sc_int.ts Migrated from JS with proper type annotations and updated documentation
src/numbers/index.ts Migrated from JS with type-safe scValToBigInt implementation
src/index.d.ts Temporary type declarations to support ongoing migration
Comments suppressed due to low confidence (1)

src/numbers/xdr_large_int.ts:269

  • The isType method on line 269 should have an explicit return type annotation. It should be static isType(type: string): boolean to make the return type explicit and enable better type narrowing. This is especially important for type guard functions.

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

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

Copilot reviewed 14 out of 23 changed files in this pull request and generated no new comments.

Comments suppressed due to low confidence (3)

src/numbers/sc_int.ts:84

  • The constructor creates bigValue by converting the input to bigint on line 79, but line 90 (outside the changed region) still passes the original value to nearestBigIntSize instead of bigValue. This means string inputs like "123" will have their bit length calculated as the string length (3 characters) rather than the numeric value's bit length, potentially causing incorrect type selection. While this appears to be a pre-existing issue from the JS code, the TypeScript migration provides an opportunity to fix it by using bigValue instead.
    src/numbers/sc_int.ts:116
  • The nearestBigIntSize function accepts bigint | number | string as input, but on line 114 calls bigI.toString(2) to convert to binary string. While bigint.toString(radix) and number.toString(radix) support the radix parameter, string.toString() does not accept parameters and will ignore the argument. For string inputs like "12345", this would return "12345" instead of a binary representation, leading to incorrect bit length calculation. The function should either convert string inputs to bigint first, or the type signature should be bigint | number only.
    src/numbers/sc_int.ts:116
  • The nearestBigIntSize function parameter type is bigint | number | string, but the function calls bigI.toString(2) at line 114. While bigint and number have a toString(radix) method, string does not have this signature - it only has toString() without parameters. When a string is passed, calling toString(2) will ignore the argument and return the string itself, which would then be used to calculate bit length incorrectly. The function should either convert the input to bigint first, or the type signature should be narrowed to exclude string.

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

@quietbits quietbits requested a review from Ryang-21 February 13, 2026 18:38
Copy link
Contributor

@Ryang-21 Ryang-21 left a comment

Choose a reason for hiding this comment

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

Left a couple comments about type mismatches between the manual and generated type declarations

const signed = value < 0;
constructor(
value: bigint | number | string,
opts?: { type?: string; [key: string]: unknown },
Copy link
Contributor

Choose a reason for hiding this comment

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

If we want parity with the manual declarations we should create a type called ScIntType that contains the possible option types

Copy link
Contributor

Choose a reason for hiding this comment

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

If we go this route we will need to add duration and timepoint

* {@link XdrLargeInt.isType})
* @param values - a list of integer-like values interpreted in big-endian order
*/
constructor(type: string, values: XdrLargeIntValues) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Same thing here the manual type declaration has this type: ScIntType

@@ -0,0 +1,374 @@
/* eslint-disable @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-explicit-any */

import { describe, it, expect } from "vitest";
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we just include tests for scValToBigInt here?

Comment on lines 23 to 35
@@ -28,7 +29,7 @@ declare module "@stellar/js-xdr" {
}

export class UnsignedHyper extends LargeInt {
constructor(values: Array<number | bigint | string>);
constructor(values: Array<bigint | number | string>);
static defineIntBoundaries(): void;
static MIN_VALUE: UnsignedHyper;
static MAX_VALUE: UnsignedHyper;
Copy link
Contributor

Choose a reason for hiding this comment

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

The js-xdr implementation looks like it spreads the args parameter

  /**
   * @param {Array<Number|BigInt|String>} parts - Slices to encode
   */
  constructor(...args) {
    super(args);
  }

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.

Migrate /numbers files to TS

2 participants