From 2152b04a19d11d61f2d398cb18f78686fde747d7 Mon Sep 17 00:00:00 2001 From: Neil Rashbrook Date: Wed, 27 Aug 2025 22:09:24 +0100 Subject: [PATCH 1/2] Add signatures that enable TypeScript type guards This enables you to write `.find((e): e is Derived => e instanceof Derived)` and TypeScript will infer the type of the expression to be `Derived` instead of the type of the collection. (Similarly for the other methods.) --- lib/api.d.ts | 4 ++++ lib/collection/array.d.ts | 1 + 2 files changed, 5 insertions(+) diff --git a/lib/api.d.ts b/lib/api.d.ts index 376a2cc..126bbff 100644 --- a/lib/api.d.ts +++ b/lib/api.d.ts @@ -144,6 +144,7 @@ declare class Collection { * order, until it finds one where `filterFunc` returns true. If such an element is found, find * immediately returns that element value. Otherwise, find returns undefined. */ + find(filterFunc: (item: Item) => item is T): T; find(filterFunc: (item: Item) => boolean): Item; /** * Creates a new collection which contains only those items that meet a certain condition. @@ -154,6 +155,7 @@ declare class Collection { * * @param filterFunc A function that accepts up to three arguments. The filter method calls the `filterFunc` function one time for each element in the collection. */ + filterOnce(filterFunc: (item: Item) => item is T): Collection; filterOnce(filterFunc: (item: Item) => boolean): Collection; /** * Creates a new collection which contains only those items that meet a certain condition. @@ -174,6 +176,7 @@ declare class Collection { * * @param filterFunc A function that accepts up to three arguments. The filter method calls the `filterFunc` function one time for each element in the collection. */ + filterObservable(filterFunc: (item: Item) => item is T): ObservableFilteredCollection; filterObservable(filterFunc: (item: Item) => boolean): ObservableFilteredCollection; /** * Creates a new collection which contains only those items that meet a certain condition. @@ -187,6 +190,7 @@ declare class Collection { * * @param filterFunc A function that accepts up to three arguments. The filter method calls the `filterFunc` function one time for each element in the collection. */ + filter(filterFunc: (item: Item) => item is T): FilteredCollection; filter(filterFunc: (item: Item) => boolean): FilteredCollection; /** * Creates a new collection which contains other objects that are derived from the items in this collection. diff --git a/lib/collection/array.d.ts b/lib/collection/array.d.ts index 50adf52..4688cb7 100644 --- a/lib/collection/array.d.ts +++ b/lib/collection/array.d.ts @@ -140,6 +140,7 @@ declare class ArrayColl extends KeyValueCollection { * order, until it finds one where `filterFunc` returns true. If such an element is found, find * immediately returns that element valuehttps://github.com/microsoft/TypeScript/blob/main/src/lib/es2016.array.include.d.tsOtherwise, find returns undefined. */ + find(filterFunc: (item: Item) => item is T): T; find(filterFunc: (item: Item) => boolean): Item; /** * Returns the index of the first element in the collection where `filterFunc` is true, From 027ea6a634f7127b7f9d06d69369b2f092e553fc Mon Sep 17 00:00:00 2001 From: Neil Rashbrook Date: Tue, 31 Mar 2026 21:04:57 +0100 Subject: [PATCH 2/2] Add comments --- lib/api.d.ts | 8 ++++---- lib/collection/array.d.ts | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/api.d.ts b/lib/api.d.ts index 126bbff..23789ed 100644 --- a/lib/api.d.ts +++ b/lib/api.d.ts @@ -144,7 +144,7 @@ declare class Collection { * order, until it finds one where `filterFunc` returns true. If such an element is found, find * immediately returns that element value. Otherwise, find returns undefined. */ - find(filterFunc: (item: Item) => item is T): T; + find(filterFunc: (item: Item) => item is T): T; // Type guard version to allow TypeScript to narrow the type of the result find(filterFunc: (item: Item) => boolean): Item; /** * Creates a new collection which contains only those items that meet a certain condition. @@ -155,7 +155,7 @@ declare class Collection { * * @param filterFunc A function that accepts up to three arguments. The filter method calls the `filterFunc` function one time for each element in the collection. */ - filterOnce(filterFunc: (item: Item) => item is T): Collection; + filterOnce(filterFunc: (item: Item) => item is T): Collection; // Type guard version to allow TypeScript to narrow the type of the result filterOnce(filterFunc: (item: Item) => boolean): Collection; /** * Creates a new collection which contains only those items that meet a certain condition. @@ -176,7 +176,7 @@ declare class Collection { * * @param filterFunc A function that accepts up to three arguments. The filter method calls the `filterFunc` function one time for each element in the collection. */ - filterObservable(filterFunc: (item: Item) => item is T): ObservableFilteredCollection; + filterObservable(filterFunc: (item: Item) => item is T): ObservableFilteredCollection; // Type guard version to allow TypeScript to narrow the type of the result filterObservable(filterFunc: (item: Item) => boolean): ObservableFilteredCollection; /** * Creates a new collection which contains only those items that meet a certain condition. @@ -190,7 +190,7 @@ declare class Collection { * * @param filterFunc A function that accepts up to three arguments. The filter method calls the `filterFunc` function one time for each element in the collection. */ - filter(filterFunc: (item: Item) => item is T): FilteredCollection; + filter(filterFunc: (item: Item) => item is T): FilteredCollection; // Type guard version to allow TypeScript to narrow the type of the result filter(filterFunc: (item: Item) => boolean): FilteredCollection; /** * Creates a new collection which contains other objects that are derived from the items in this collection. diff --git a/lib/collection/array.d.ts b/lib/collection/array.d.ts index 4688cb7..3664ddb 100644 --- a/lib/collection/array.d.ts +++ b/lib/collection/array.d.ts @@ -140,7 +140,7 @@ declare class ArrayColl extends KeyValueCollection { * order, until it finds one where `filterFunc` returns true. If such an element is found, find * immediately returns that element valuehttps://github.com/microsoft/TypeScript/blob/main/src/lib/es2016.array.include.d.tsOtherwise, find returns undefined. */ - find(filterFunc: (item: Item) => item is T): T; + find(filterFunc: (item: Item) => item is T): T; // Type guard version to allow TypeScript to narrow the type of the result find(filterFunc: (item: Item) => boolean): Item; /** * Returns the index of the first element in the collection where `filterFunc` is true,