Skip to content

Commit f8ab316

Browse files
committed
fix(t-utils): let negotiators infer the return type from usage
1 parent 387742a commit f8ab316

File tree

1 file changed

+19
-14
lines changed

1 file changed

+19
-14
lines changed

packages/t-utils/source/negotiator.ts

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,37 @@
11
import { filterMatches } from "@fluent/langneg";
22

3-
export type LocaleNegotiator<Locale> = (
3+
const typedFilterMatches = filterMatches as <
4+
Requested extends string,
5+
Available extends string,
6+
>(
7+
requested: readonly Requested[],
8+
available: readonly Available[],
9+
strategy: "filtering" | "lookup" | "matching",
10+
) => Available[];
11+
12+
export type LocaleNegotiator<TypeParamLocale extends string = string> = <
13+
Locale extends TypeParamLocale = TypeParamLocale,
14+
>(
415
availableLocales: readonly Locale[],
516
) => Locale | undefined;
617

7-
export type LocaleNegotiators<Locale> =
18+
export type LocaleNegotiators<Locale extends string> =
819
| readonly [...(LocaleNegotiator<Locale> | false)[], Locale]
920
| readonly [];
1021

11-
type Algorithm = (
22+
type Algorithm = <Available extends string = string>(
1223
requestedLocales: readonly string[],
13-
availableLocales: readonly string[],
14-
) => string[];
24+
availableLocales: readonly Available[],
25+
) => Available[];
1526

1627
export const lookup: Algorithm = (requestedLocales, availableLocales) =>
17-
filterMatches(
18-
// @info `filterMatches` expects mutable arrays
19-
Array.from(requestedLocales),
20-
Array.from(availableLocales),
21-
"lookup",
22-
);
28+
typedFilterMatches(requestedLocales, availableLocales, "lookup");
2329

2430
export const negotiator: (
2531
requestedLocales: readonly string[],
2632
algorithm: Algorithm,
27-
) => LocaleNegotiator<string> =
28-
(requestedLocales, algorithm) => (availableLocales) =>
29-
algorithm(requestedLocales, availableLocales)[0];
33+
) => LocaleNegotiator = (requestedLocales, algorithm) => (availableLocales) =>
34+
algorithm(requestedLocales, availableLocales)[0];
3035

3136
export const browser = () =>
3237
negotiator(globalThis.navigator?.languages ?? [], lookup);

0 commit comments

Comments
 (0)