Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🌱 Setup TS to know about .filter(Boolean) #1416

Merged
merged 1 commit into from
Sep 28, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ const useApplicationFormData = ({
// Fetch data
const { tagCategories } = useFetchTagCategories();
const tags = useMemo(
() => tagCategories.flatMap((tc) => tc.tags).filter(Boolean) as Tag[],
() => tagCategories.flatMap((tc) => tc.tags).filter(Boolean),
[tagCategories]
);

Expand Down
6 changes: 3 additions & 3 deletions client/src/app/utils/model-utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@
export const toRefs = <RefLike extends Ref>(
source: Iterable<RefLike>
): Array<Ref> | undefined =>
!source ? undefined : ([...source].map(toRef).filter(Boolean) as Ref[]);
!source ? undefined : [...source].map(toRef).filter(Boolean);

/**
* Take an array of source items that look like a `Ref`, find the first one that matches
Expand Down Expand Up @@ -271,11 +271,11 @@
): Array<Ref> | undefined =>
!matchValues
? undefined
: (matchValues
: matchValues

Check warning on line 274 in client/src/app/utils/model-utils.tsx

View check run for this annotation

Codecov / codecov/patch

client/src/app/utils/model-utils.tsx#L274

Added line #L274 was not covered by tests
.map((toMatch) =>
!toMatch
? undefined
: items.find((item) => matchOperator(itemMatchFn(item), toMatch))
)
.map<Ref | undefined>(toRef)
.filter(Boolean) as Ref[]);
.filter(Boolean);
28 changes: 28 additions & 0 deletions client/types/array-filter-Boolean.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Fixes https://github.com/microsoft/TypeScript/issues/16655 for `Array.prototype.filter()`
* For example, using the fix the type of `bar` is `string[]` in the below snippet as it should be.
*
* const foo: (string | null | undefined)[] = [];
* const bar = foo.filter(Boolean);
*
* For related definitions, see https://github.com/microsoft/TypeScript/blob/master/src/lib/es5.d.ts
*
* Original licenses apply, see
* - https://github.com/microsoft/TypeScript/blob/master/LICENSE.txt
* - https://stackoverflow.com/help/licensing
*/

/** See https://stackoverflow.com/a/51390763/1470607 */
type Falsy = false | 0 | "" | null | undefined;

interface Array<T> {
/**
* Returns the elements of an array that meet the condition specified in a callback function.
* @param predicate A function that accepts up to three arguments. The filter method calls the predicate function one time for each element in the array.
* @param thisArg An object to which the this keyword can refer in the predicate function. If thisArg is omitted, undefined is used as the this value.
*/
filter<S extends T>(
predicate: BooleanConstructor,
thisArg?: any
): Exclude<S, Falsy>[];
}
Loading