Skip to content

Commit db49c6c

Browse files
authored
Add fractionalSecond part type to DateTimeFormat().formatToParts (microsoft#49569)
* Add fractionalSecond part type to DateTimeFormat().formatToParts This change adds the `fractionalSecond` part type as a valid part to be returned from Intl.DateTimeFormat().formatToParts(). Fixes microsoft#48882 * fixup
1 parent 529ba99 commit db49c6c

File tree

6 files changed

+62
-1
lines changed

6 files changed

+62
-1
lines changed

src/lib/es2017.intl.d.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
declare namespace Intl {
2-
type DateTimeFormatPartTypes = "day" | "dayPeriod" | "era" | "hour" | "literal" | "minute" | "month" | "second" | "timeZoneName" | "weekday" | "year";
2+
3+
interface DateTimeFormatPartTypesRegistry {
4+
day: any
5+
dayPeriod: any
6+
era: any
7+
hour: any
8+
literal: any
9+
minute: any
10+
month: any
11+
second: any
12+
timeZoneName: any
13+
weekday: any
14+
year: any
15+
}
16+
17+
type DateTimeFormatPartTypes = keyof DateTimeFormatPartTypesRegistry;
318

419
interface DateTimeFormatPart {
520
type: DateTimeFormatPartTypes;

src/lib/es2021.intl.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
declare namespace Intl {
22

3+
interface DateTimeFormatPartTypesRegistry {
4+
fractionalSecond: any
5+
}
6+
37
interface DateTimeFormatOptions {
48
formatMatcher?: "basic" | "best fit" | "best fit" | undefined;
59
dateStyle?: "full" | "long" | "medium" | "short" | undefined;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
//// [formatToPartsFractionalSecond.ts]
2+
new Intl.DateTimeFormat().formatToParts().find((val) => val.type === 'fractionalSecond')
3+
4+
//// [formatToPartsFractionalSecond.js]
5+
new Intl.DateTimeFormat().formatToParts().find((val) => val.type === 'fractionalSecond');
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
=== tests/cases/compiler/formatToPartsFractionalSecond.ts ===
2+
new Intl.DateTimeFormat().formatToParts().find((val) => val.type === 'fractionalSecond')
3+
>new Intl.DateTimeFormat().formatToParts().find : Symbol(Array.find, Decl(lib.es2015.core.d.ts, --, --), Decl(lib.es2015.core.d.ts, --, --))
4+
>new Intl.DateTimeFormat().formatToParts : Symbol(Intl.DateTimeFormat.formatToParts, Decl(lib.es2017.intl.d.ts, --, --))
5+
>Intl.DateTimeFormat : Symbol(Intl.DateTimeFormat, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2017.intl.d.ts, --, --), Decl(lib.es2021.intl.d.ts, --, --))
6+
>Intl : Symbol(Intl, Decl(lib.es5.d.ts, --, --), Decl(lib.es2017.intl.d.ts, --, --), Decl(lib.es2018.intl.d.ts, --, --), Decl(lib.es2020.bigint.d.ts, --, --), Decl(lib.es2020.intl.d.ts, --, --) ... and 3 more)
7+
>DateTimeFormat : Symbol(Intl.DateTimeFormat, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2017.intl.d.ts, --, --), Decl(lib.es2021.intl.d.ts, --, --))
8+
>formatToParts : Symbol(Intl.DateTimeFormat.formatToParts, Decl(lib.es2017.intl.d.ts, --, --))
9+
>find : Symbol(Array.find, Decl(lib.es2015.core.d.ts, --, --), Decl(lib.es2015.core.d.ts, --, --))
10+
>val : Symbol(val, Decl(formatToPartsFractionalSecond.ts, 0, 48))
11+
>val.type : Symbol(Intl.DateTimeFormatPart.type, Decl(lib.es2017.intl.d.ts, --, --))
12+
>val : Symbol(val, Decl(formatToPartsFractionalSecond.ts, 0, 48))
13+
>type : Symbol(Intl.DateTimeFormatPart.type, Decl(lib.es2017.intl.d.ts, --, --))
14+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
=== tests/cases/compiler/formatToPartsFractionalSecond.ts ===
2+
new Intl.DateTimeFormat().formatToParts().find((val) => val.type === 'fractionalSecond')
3+
>new Intl.DateTimeFormat().formatToParts().find((val) => val.type === 'fractionalSecond') : Intl.DateTimeFormatPart
4+
>new Intl.DateTimeFormat().formatToParts().find : { <S extends Intl.DateTimeFormatPart>(predicate: (this: void, value: Intl.DateTimeFormatPart, index: number, obj: Intl.DateTimeFormatPart[]) => value is S, thisArg?: any): S; (predicate: (value: Intl.DateTimeFormatPart, index: number, obj: Intl.DateTimeFormatPart[]) => unknown, thisArg?: any): Intl.DateTimeFormatPart; }
5+
>new Intl.DateTimeFormat().formatToParts() : Intl.DateTimeFormatPart[]
6+
>new Intl.DateTimeFormat().formatToParts : (date?: number | Date) => Intl.DateTimeFormatPart[]
7+
>new Intl.DateTimeFormat() : Intl.DateTimeFormat
8+
>Intl.DateTimeFormat : { (locales?: string | string[], options?: Intl.DateTimeFormatOptions): Intl.DateTimeFormat; new (locales?: string | string[], options?: Intl.DateTimeFormatOptions): Intl.DateTimeFormat; supportedLocalesOf(locales: string | string[], options?: Intl.DateTimeFormatOptions): string[]; readonly prototype: Intl.DateTimeFormat; }
9+
>Intl : typeof Intl
10+
>DateTimeFormat : { (locales?: string | string[], options?: Intl.DateTimeFormatOptions): Intl.DateTimeFormat; new (locales?: string | string[], options?: Intl.DateTimeFormatOptions): Intl.DateTimeFormat; supportedLocalesOf(locales: string | string[], options?: Intl.DateTimeFormatOptions): string[]; readonly prototype: Intl.DateTimeFormat; }
11+
>formatToParts : (date?: number | Date) => Intl.DateTimeFormatPart[]
12+
>find : { <S extends Intl.DateTimeFormatPart>(predicate: (this: void, value: Intl.DateTimeFormatPart, index: number, obj: Intl.DateTimeFormatPart[]) => value is S, thisArg?: any): S; (predicate: (value: Intl.DateTimeFormatPart, index: number, obj: Intl.DateTimeFormatPart[]) => unknown, thisArg?: any): Intl.DateTimeFormatPart; }
13+
>(val) => val.type === 'fractionalSecond' : (this: void, val: Intl.DateTimeFormatPart) => boolean
14+
>val : Intl.DateTimeFormatPart
15+
>val.type === 'fractionalSecond' : boolean
16+
>val.type : keyof Intl.DateTimeFormatPartTypesRegistry
17+
>val : Intl.DateTimeFormatPart
18+
>type : keyof Intl.DateTimeFormatPartTypesRegistry
19+
>'fractionalSecond' : "fractionalSecond"
20+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// @target: esnext
2+
3+
new Intl.DateTimeFormat().formatToParts().find((val) => val.type === 'fractionalSecond')

0 commit comments

Comments
 (0)