Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/ja/reference/compat/predicate/isSafeInteger.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,5 @@ isSafeInteger(NaN); // false

#### 戻り値

(`boolean`): 値が安全な整数の場合は `true`、そうでなければ `false` を返します。
(`value is number`): 値が安全な整数の場合は `true`、そうでなければ `false` を返します。
`true` を返す場合、TypeScript は `value` の型を `number` に絞り込みます。
3 changes: 2 additions & 1 deletion docs/ko/reference/compat/predicate/isSafeInteger.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,5 @@ isSafeInteger(NaN); // false

#### 반환 값

(`boolean`): 값이 안전한 정수이면 `true`, 아니면 `false`를 반환해요.
(`value is number`): 값이 안전한 정수이면 `true`, 아니면 `false`를 반환해요.
`true`를 반환할 때, TypeScript는 `value`의 타입을 `number`로 좁혀요.
3 changes: 2 additions & 1 deletion docs/reference/compat/predicate/isSafeInteger.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,5 @@ isSafeInteger(NaN); // false

#### Returns

(`boolean`): Returns `true` if the value is a safe integer, otherwise `false`.
(`value is number`) Returns `true` if the value is a safe integer, otherwise `false`.
When `true`, TypeScript narrows the type of `value` to `number`.
3 changes: 2 additions & 1 deletion docs/zh_hans/reference/compat/predicate/isSafeInteger.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,5 @@ isSafeInteger(NaN); // false

#### 返回值

(`boolean`): 如果值为安全整数则返回 `true`,否则返回 `false`。
(`value is number`): 如果值为安全整数则返回 `true`,否则返回 `false`。
当返回 `true` 时,TypeScript 会将 `value` 的类型缩小为 `number`。
14 changes: 12 additions & 2 deletions src/compat/predicate/isSafeInteger.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,17 @@ describe('isSafeInteger function', () => {
expect(func(symbol)).toBe(false);
});

it('should match the type of lodash', () => {
expectTypeOf(isSafeInteger).toEqualTypeOf<typeof isSafeIntegerLodash>();
it('should be compatible with lodash type', () => {
const lodashFn: typeof isSafeIntegerLodash = isSafeInteger;
expect(lodashFn(3)).toBe(true);
expect(lodashFn('3')).toBe(false);
});

it('should work as a type predicate', () => {
const value: unknown = 3;
if (isSafeInteger(value)) {
expectTypeOf(value).toEqualTypeOf<number>();
expect(typeof value).toBe('number');
}
});
});
7 changes: 4 additions & 3 deletions src/compat/predicate/isSafeInteger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@
* A safe integer is an integer that can be precisely represented as a `number` in JavaScript,
* without any other integer being rounded to it.
*
* This function can also serve as a type predicate in TypeScript, narrowing the type of the argument to `number`.
* This function also serves as a type predicate in TypeScript,
* narrowing the type of the argument to `number`.
*
* @param {unknown} value - The value to check
* @returns {boolean} `true` if `value` is an integer and between the safe values, otherwise `false`
* @returns {value is number} `true` if `value` is an integer and between the safe values, otherwise `false`
*
* @example
* isSafeInteger(3); // Returns: true
* isSafeInteger(Number.MIN_SAFE_INTEGER - 1); // Returns: false
* isSafeInteger(1n); // Returns: false
* isSafeInteger('1'); // Returns: false
*/
export function isSafeInteger(value: any): boolean {
export function isSafeInteger(value: unknown): value is number {
return Number.isSafeInteger(value);
}