Conversation
|
Size Change: +7.01 kB (+0.2%) Total Size: 3.54 MB
|
There was a problem hiding this comment.
Pull request overview
This PR migrates the memo.js file to TypeScript, converting it to src/memo.ts and updating the associated test file from JavaScript/Mocha to TypeScript/Vitest. The migration adds comprehensive type definitions including a generic Memo class with type-safe memo types and values.
Changes:
- Migrated
src/memo.jstosrc/memo.tswith full TypeScript type annotations and generic types - Converted test file from
test/unit/memo_test.js(Mocha/Chai) totest/unit/memo.test.ts(Vitest) - Generated type definition file
type_validation/memo.d.ts
Reviewed changes
Copilot reviewed 3 out of 4 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/memo.ts | Migrated to TypeScript with generic types, updated XDR union handling from .arm() to .switch(), added type annotations for all methods |
| test/unit/memo_test.js | Removed (replaced by TypeScript version) |
| test/unit/memo.test.ts | New TypeScript test file using Vitest framework, maintains all test coverage from original file |
| type_validation/memo.d.ts | Generated TypeScript type definitions matching the implementation |
Comments suppressed due to low confidence (2)
src/memo.ts:262
- The double conversion from string to UnsignedHyper and back to string before creating Uint64 appears redundant. This can be simplified to
xdr.Memo.memoId(xdr.Uint64.fromString(this._value as string))which matches the pattern used elsewhere in the codebase (see src/keypair.js:146, src/muxed_account.js:104, src/util/decode_encode_muxed_account.ts:69). The current implementation performs unnecessary conversions that impact performance.
src/memo.ts:291 - The switch statement is missing an explicit case for
xdr.MemoType.memoNone(). While the current implementation works by falling through to the default case and checking ifobject.value()is undefined, it would be clearer and more maintainable to add an explicit case:case xdr.MemoType.memoNone(): return Memo.none();before the default case. This makes the intent explicit and matches the pattern used in similar code throughout the codebase.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
src/memo.ts
Outdated
| export namespace MemoType { | ||
| export type None = typeof MemoNone; | ||
| export type ID = typeof MemoID; | ||
| export type Text = typeof MemoText; | ||
| export type Hash = typeof MemoHash; | ||
| export type Return = typeof MemoReturn; | ||
| } |
There was a problem hiding this comment.
I'm not sure if this would generate namespace output we currently have, but we could do it this way without using namespace. It sounds like using TS namespace is not recommended.
export type MemoTypeNone = typeof MemoNone;
export type MemoTypeID = typeof MemoID;
export type MemoTypeText = typeof MemoText;
export type MemoTypeHash = typeof MemoHash;
export type MemoTypeReturn = typeof MemoReturn;
src/memo.ts
Outdated
| export type MemoType = | ||
| | MemoType.Hash | ||
| | MemoType.ID | ||
| | MemoType.None | ||
| | MemoType.Return | ||
| | MemoType.Text; |
There was a problem hiding this comment.
The this would be:
export type MemoType =
| MemoTypeNone
| MemoTypeID
| MemoTypeText
| MemoTypeHash
| MemoTypeReturn;
src/memo.ts
Outdated
| type MemoTypeToValue<T extends MemoType> = T extends MemoType.None | ||
| ? null | ||
| : T extends MemoType.ID | ||
| ? string | ||
| : T extends MemoType.Text | ||
| ? Buffer | string | ||
| : T extends MemoType.Hash | ||
| ? Buffer | ||
| : T extends MemoType.Return | ||
| ? Buffer | ||
| : MemoValue; |
There was a problem hiding this comment.
Then we could make this part easier to read as well.
type MemoValueMap = {
[MemoNone]: null;
[MemoID]: string;
[MemoText]: Buffer | string;
[MemoHash]: Buffer;
[MemoReturn]: Buffer;
};
type MemoTypeToValue<T extends MemoType> = MemoValueMap[T];
No description provided.