Skip to content

Commit

Permalink
fix: correctly narrow return type of isArray for {} arguments (#470)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlCalzone committed Sep 7, 2022
1 parent c6b11e4 commit ba3f76b
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 24 deletions.
41 changes: 22 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,35 +13,38 @@ Function documentation available [here](https://alcalzone.github.io/shared-utils
<!--
Placeholder for the next version (at the beginning of the line):
### __WORK IN PROGRESS__
-->
-->
### __WORK IN PROGRESS__
* Fix: correctly narrow return type of isArray for `{}` arguments

### 4.0.4 (2022-09-07)
* Fix: narrowed type for `isArray` is inferred correctly for `readonly` arrays

* Fix: narrowed type for `isArray` is inferred correctly for `readonly` arrays

### 4.0.3 (2022-08-27)
* Update `isArray` to be compatible with TS 4.8 changes

* Update `isArray` to be compatible with TS 4.8 changes

### 4.0.1 (2021-11-15)
`SortedQueue`: Fixed an issue where inserting an item before the first one would cause the queue to lose track of items

`SortedQueue`: Fixed an issue where inserting an item before the first one would cause the queue to lose track of items

### 4.0.0 (2021-06-19)
* Node.js 12+ is now required

* Node.js 12+ is now required

### 3.0.4 (2021-04-24)
* Fix compatibility of `wait()` with Electron if `unref` is `true`
* Dependency updates

* Dependency updates

### 3.0.3 (2021-03-09)
#### Fixes
* Fixed compatibility with TypeScript 4.2

* Fixed compatibility with TypeScript 4.2

### 3.0.2 (2021-01-16)
#### Fixes
* The argument to `resolve` of `DeferredPromise` is no longer optional, except for `Promise<void>`

* The argument to `resolve` of `DeferredPromise` is no longer optional, except for `Promise<void>`

### 3.0.1 (2020-12-05)
#### Fixes
* The typeguard `isObject` no longer narrows the type of the argument to `object`

* The typeguard `isObject` no longer narrows the type of the argument to `object`

### 3.0.0 (2020-08-16)
#### Breaking changes
* Renamed the following types:
Expand All @@ -66,7 +69,7 @@ Function documentation available [here](https://alcalzone.github.io/shared-utils

#### Fixes
* The type `CallbackAPIReturnType` now works with `strictNullChecks`.
* The type `Promisify` is no longer experimental and no longer messes up the inferred signature argument names

* The type `Promisify` is no longer experimental and no longer messes up the inferred signature argument names

### 2.3.0 (2020-06-08)
* Added optional `unref` parameter to `async` -> `wait(ms, [unref])`
2 changes: 1 addition & 1 deletion build/typeguards/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
export declare function isObject<T>(it: T): it is T & Record<string, unknown>;
declare type IfAny<T, Y, N> = 0 extends (1 & T) ? Y : N;
declare type IsAny<T> = IfAny<T, true, never>;
declare type ExtractArray<T> = true extends IsAny<T> ? unknown[] : T extends readonly unknown[] ? T : unknown extends T ? (T & unknown[]) : never;
declare type ExtractArray<T> = true extends IsAny<T> ? unknown[] : T extends readonly unknown[] ? T : {} extends T ? (T & unknown[]) : never;
/**
* Tests whether the given variable is really an Array
* @param it The variable to test
Expand Down
8 changes: 4 additions & 4 deletions src/typeguards/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ type IfAny<T, Y, N> = 0 extends (1 & T) ? Y : N;
type IsAny<T> = IfAny<T, true, never>;

type ExtractArray<T> =
true extends IsAny<T> ? unknown[]
: T extends readonly unknown[] ? T
: unknown extends T ? (T & unknown[])
: never;
true extends IsAny<T> ? unknown[]
: T extends readonly unknown[] ? T
: {} extends T ? (T & unknown[])
: never;

/**
* Tests whether the given variable is really an Array
Expand Down
7 changes: 7 additions & 0 deletions src/typeguards/typeguards.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ describe("lib/typeguards =>", () => {
const _number = _unknown as number;
const _numberArray = _unknown as number[];
const _readonlyNumberArray = _unknown as readonly number[];
const _nonNullish = _unknown as {};

if (isArray(_any)) {
assertTrue<Equals<typeof _any, any>>();
Expand Down Expand Up @@ -131,6 +132,12 @@ describe("lib/typeguards =>", () => {
_readonlyNumberArray;
// ^?
}

if (isArray(_nonNullish)) {
assertTrue<Equals<typeof _nonNullish, unknown[]>>();
_nonNullish;
// ^?
}
});
});
});

0 comments on commit ba3f76b

Please sign in to comment.