TypeScript Version: 4.2.0-dev.20201221
Search Terms:NumberFormat.formatToParts, compact
Expected behavior:
part: 'compact' is a valid part type, as per EMCA 402, section 12.1.7 (PartitionNotationSubPattern), step 4. - c. - iv. - 2.:
iv. Else if p is equal to "compactSymbol", then
- [...]
- Append a new Record { [[Type]]: "compact", [[Value]]: compactSymbol } as the last element of result.
Note that the MDN documentation for Intl.NumberFormat.prototype.formatToParts() doesn't mention this part type either; see mdn/content#516.
Actual behavior:
The Intl.NumberFormatPartTypes type should include "compact".
Related Issues:
Code
constcompactFormat=Intl.NumberFormat('en',{notation: 'compact',compactDisplay: 'short',style: 'decimal',maximumFractionDigits: 3,})// formatValue(42174812) => {formatted: '42.175', magnitude: 'M'}functionformatValue(value: number): {formatted: string,magnitude: string}{letmagnitude=''return{formatted: compactFormat.formatToParts(value).reduce((formatted,part)=>{switch(part.type){case'compact':
magnitude=magnitude+part.valuebreakdefault:
formatted=formatted+part.value}returnformatted},''),magnitude: magnitude}}console.log(formatValue(42174812))Output
"use strict";constcompactFormat=Intl.NumberFormat('en',{notation: 'compact',compactDisplay: 'short',style: 'decimal',maximumFractionDigits: 3,});// formatValue(42174812) => {formatted: '42.175', magnitude: 'M'}functionformatValue(value){letmagnitude='';return{formatted: compactFormat.formatToParts(value).reduce((formatted,part)=>{switch(part.type){case'compact':
magnitude=magnitude+part.value;break;default:
formatted=formatted+part.value;}returnformatted;},''),magnitude: magnitude};}console.log(formatValue(42174812));Compiler Options
{
"compilerOptions": {
"noImplicitAny": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"strictPropertyInitialization": true,
"strictBindCallApply": true,
"noImplicitThis": true,
"noImplicitReturns": true,
"alwaysStrict": true,
"esModuleInterop": true,
"declaration": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"moduleResolution": 2,
"target": "Latest",
"module": "ESNext"
}
}Playground Link:Provided
TypeScript Version: 4.2.0-dev.20201221
Search Terms:
NumberFormat.formatToParts,compactExpected behavior:
part: 'compact'is a valid part type, as per EMCA 402, section 12.1.7 (PartitionNotationSubPattern), step 4. - c. - iv. - 2.:Note that the MDN documentation for
Intl.NumberFormat.prototype.formatToParts()doesn't mention this part type either; see mdn/content#516.Actual behavior:
The
Intl.NumberFormatPartTypestype should include"compact".Related Issues:
Code
Output
Compiler Options
{ "compilerOptions": { "noImplicitAny": true, "strictNullChecks": true, "strictFunctionTypes": true, "strictPropertyInitialization": true, "strictBindCallApply": true, "noImplicitThis": true, "noImplicitReturns": true, "alwaysStrict": true, "esModuleInterop": true, "declaration": true, "experimentalDecorators": true, "emitDecoratorMetadata": true, "moduleResolution": 2, "target": "Latest", "module": "ESNext" } }Playground Link:Provided