feat(isDigit): add isDigit type#6
Conversation
WalkthroughThe pull request introduces a set of TypeScript utility types for checking whether a given type represents a digit string. These types are implemented across three files in the Changes
Poem
Finishing Touches
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (4)
src/string/isDigit/algo.ts (2)
3-7: Consider adding JSDoc comments for the _IsDigit type.While the implementation is correct, adding documentation would help explain the purpose of this internal utility type and its expected behavior.
+/** + * Internal utility type that checks if a type parameter extends a numeric string pattern. + * @template T - The type to check + * @returns {boolean} - True if T extends `${number}`, false otherwise + * @internal + */ export type _IsDigit<T> = [T] extends [ `${number}`, ] ? true : false
9-14: Consider renaming IsDigit_Back and adding documentation.The "_Back" suffix is unclear. If this is an alternative implementation, consider a more descriptive name like "IsDigitStrict" since it adds string literal validation.
+/** + * Strict type guard that checks if a type parameter is both a string literal + * and matches a numeric string pattern. + * @template T - The type to check + * @returns {boolean} - True if T is a string literal and extends `${number}`, false otherwise + */ -export type IsDigit_Back<T> = +export type IsDigitStrict<T> = IsStringLiteral<T> extends true ? [T] extends [`${number}`] ? true : false : falsesrc/string/isDigit/index.test.ts (2)
12-44: Add tests for additional numeric string patterns.The test suite is thorough but could be expanded to cover more edge cases:
- Scientific notation (e.g., "1e5")
- Negative numbers (e.g., "-123")
- Leading/trailing whitespace
describe("_IsDigit type tests", () => { it("should return true for string literals that match numeric patterns", () => { type T1 = _IsDigit<"0"> type T2 = _IsDigit<"123"> type T3 = _IsDigit<"001"> type T4 = _IsDigit<"1.2"> + type T5 = _IsDigit<"-123"> + type T6 = _IsDigit<"1e5"> expectTypeOf<T1>().toEqualTypeOf<true>() expectTypeOf<T2>().toEqualTypeOf<true>() expectTypeOf<T3>().toEqualTypeOf<true>() expectTypeOf<T4>().toEqualTypeOf<true>() + expectTypeOf<T5>().toEqualTypeOf<true>() + expectTypeOf<T6>().toEqualTypeOf<true>() }) it("should return false for string literals that are not numeric", () => { type T1 = _IsDigit<""> type T2 = _IsDigit<"abc"> type T3 = _IsDigit<"1a2"> type T4 = _IsDigit<"one"> + type T5 = _IsDigit<" 123 "> expectTypeOf<T1>().toEqualTypeOf<false>() expectTypeOf<T2>().toEqualTypeOf<false>() expectTypeOf<T3>().toEqualTypeOf<false>() expectTypeOf<T4>().toEqualTypeOf<false>() + expectTypeOf<T5>().toEqualTypeOf<false>() })
76-87: Improve comments for union type tests.The current comments about IsStringLiteral are repeated and could be more precise about the expected behavior with unions.
- // If IsStringLiteral is strictly checking for a single literal type, - // then a union of two string literals is not "a single literal." + // Test that a union containing any non-digit string literal evaluates to false type T1 = IsDigit_Back<"123" | "abc" | "234"> expectTypeOf<T1>().toEqualTypeOf<false>() }) it("should return true when given a union of string literals (all digits)", () => { - // If IsStringLiteral is strictly checking for a single literal type, - // then a union of two string literals is not "a single literal." + // Test that a union of only digit string literals evaluates to true type T1 = IsDigit_Back<"123" | "345" | "1214"> expectTypeOf<T1>().toEqualTypeOf<true>() })
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
src/string/isDigit/algo.ts(1 hunks)src/string/isDigit/index.test.ts(1 hunks)src/string/isDigit/index.ts(1 hunks)
| /** | ||
| * A type that returns true if a param is a stringified number, and false otherwise. | ||
| * | ||
| * @template Str - The string literal type to reverse. | ||
| * @returns {string} - Returns the reversed string. | ||
| */ |
There was a problem hiding this comment.
Fix incorrect JSDoc documentation.
The current documentation has incorrect template parameter description and return type. It appears to be copied from a string reversal utility.
/**
* A type that returns true if a param is a stringified number, and false otherwise.
*
- * @template Str - The string literal type to reverse.
- * @returns {string} - Returns the reversed string.
+ * @template T - The string type to check.
+ * @returns {boolean} - Returns true if T represents a valid numeric string, false otherwise.
+ * @example
+ * type T1 = IsDigit<"123"> // true
+ * type T2 = IsDigit<"abc"> // false
+ * type T3 = IsDigit<"1.2"> // true
+ * type T4 = IsDigit<""> // false
*/📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| /** | |
| * A type that returns true if a param is a stringified number, and false otherwise. | |
| * | |
| * @template Str - The string literal type to reverse. | |
| * @returns {string} - Returns the reversed string. | |
| */ | |
| /** | |
| * A type that returns true if a param is a stringified number, and false otherwise. | |
| * | |
| * @template T - The string type to check. | |
| * @returns {boolean} - Returns true if T represents a valid numeric string, false otherwise. | |
| * @example | |
| * type T1 = IsDigit<"123"> // true | |
| * type T2 = IsDigit<"abc"> // false | |
| * type T3 = IsDigit<"1.2"> // true | |
| * type T4 = IsDigit<""> // false | |
| */ |
Summary by CodeRabbit
New Features
IsDigitfor compile-time validation of numeric string typesTests