Skip to content

Commit fc963f6

Browse files
committed
(third) Add typings for Intl.ListFormat
1 parent 757690c commit fc963f6

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

experiments/stasm/third/example/example_list.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,20 @@ class Person {
2222

2323
// TODO(stasm): This is generic enough that it could be in impl/runtime.ts.
2424
class ListValue<T> extends RuntimeValue<Array<T>> {
25-
private opts: Record<string, string>; // ListFormatOptions
25+
private opts: Intl.ListFormatOptions;
2626

27-
constructor(value: Array<T>, opts: Record<string, string> = {}) {
27+
constructor(value: Array<T>, opts: Intl.ListFormatOptions = {}) {
2828
super(value);
2929
this.opts = opts;
3030
}
3131

3232
formatToString(ctx: FormattingContext): string {
3333
// TODO(stasm): Cache ListFormat.
34-
// @ts-ignore
3534
let lf = new Intl.ListFormat(ctx.locale, this.opts);
3635
return lf.format(this.value);
3736
}
3837

3938
*formatToParts(ctx: FormattingContext): IterableIterator<FormattedPart> {
40-
// @ts-ignore
4139
let lf = new Intl.ListFormat(ctx.locale, this.opts);
4240
yield* lf.formatToParts(this.value);
4341
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
declare namespace Intl {
2+
interface ListFormatOptions {
3+
// I added `string` to avoid having to validate the exact values of options.
4+
localeMatcher?: string | "best fit" | "lookup";
5+
type?: string | "conjunction" | "disjunction | unit";
6+
style?: string | "long" | "short" | "narrow";
7+
}
8+
9+
type ListFormatPartTypes = "literal" | "element";
10+
11+
interface ListFormatPart {
12+
type: ListFormatPartTypes;
13+
value: string;
14+
}
15+
16+
interface ListFormat {
17+
format(value?: Array<unknown>): string;
18+
formatToParts(value?: Array<unknown>): ListFormatPart[];
19+
}
20+
21+
var ListFormat: {
22+
new (locales?: string | string[], options?: ListFormatOptions): ListFormat;
23+
(locales?: string | string[], options?: ListFormatOptions): ListFormat;
24+
};
25+
}

0 commit comments

Comments
 (0)