types.d.cts 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // The rationale for having a separate `CountryCode` type instead of just a `string`:
  2. // https://github.com/catamphetamine/libphonenumber-js/issues/170#issuecomment-363156068
  3. export type CountryCode = 'AC' | 'AD' | 'AE' | 'AF' | 'AG' | 'AI' | 'AL' | 'AM' | 'AO' | 'AR' | 'AS' | 'AT' | 'AU' | 'AW' | 'AX' | 'AZ' | 'BA' | 'BB' | 'BD' | 'BE' | 'BF' | 'BG' | 'BH' | 'BI' | 'BJ' | 'BL' | 'BM' | 'BN' | 'BO' | 'BQ' | 'BR' | 'BS' | 'BT' | 'BW' | 'BY' | 'BZ' | 'CA' | 'CC' | 'CD' | 'CF' | 'CG' | 'CH' | 'CI' | 'CK' | 'CL' | 'CM' | 'CN' | 'CO' | 'CR' | 'CU' | 'CV' | 'CW' | 'CX' | 'CY' | 'CZ' | 'DE' | 'DJ' | 'DK' | 'DM' | 'DO' | 'DZ' | 'EC' | 'EE' | 'EG' | 'EH' | 'ER' | 'ES' | 'ET' | 'FI' | 'FJ' | 'FK' | 'FM' | 'FO' | 'FR' | 'GA' | 'GB' | 'GD' | 'GE' | 'GF' | 'GG' | 'GH' | 'GI' | 'GL' | 'GM' | 'GN' | 'GP' | 'GQ' | 'GR' | 'GT' | 'GU' | 'GW' | 'GY' | 'HK' | 'HN' | 'HR' | 'HT' | 'HU' | 'ID' | 'IE' | 'IL' | 'IM' | 'IN' | 'IO' | 'IQ' | 'IR' | 'IS' | 'IT' | 'JE' | 'JM' | 'JO' | 'JP' | 'KE' | 'KG' | 'KH' | 'KI' | 'KM' | 'KN' | 'KP' | 'KR' | 'KW' | 'KY' | 'KZ' | 'LA' | 'LB' | 'LC' | 'LI' | 'LK' | 'LR' | 'LS' | 'LT' | 'LU' | 'LV' | 'LY' | 'MA' | 'MC' | 'MD' | 'ME' | 'MF' | 'MG' | 'MH' | 'MK' | 'ML' | 'MM' | 'MN' | 'MO' | 'MP' | 'MQ' | 'MR' | 'MS' | 'MT' | 'MU' | 'MV' | 'MW' | 'MX' | 'MY' | 'MZ' | 'NA' | 'NC' | 'NE' | 'NF' | 'NG' | 'NI' | 'NL' | 'NO' | 'NP' | 'NR' | 'NU' | 'NZ' | 'OM' | 'PA' | 'PE' | 'PF' | 'PG' | 'PH' | 'PK' | 'PL' | 'PM' | 'PR' | 'PS' | 'PT' | 'PW' | 'PY' | 'QA' | 'RE' | 'RO' | 'RS' | 'RU' | 'RW' | 'SA' | 'SB' | 'SC' | 'SD' | 'SE' | 'SG' | 'SH' | 'SI' | 'SJ' | 'SK' | 'SL' | 'SM' | 'SN' | 'SO' | 'SR' | 'SS' | 'ST' | 'SV' | 'SX' | 'SY' | 'SZ' | 'TA' | 'TC' | 'TD' | 'TG' | 'TH' | 'TJ' | 'TK' | 'TL' | 'TM' | 'TN' | 'TO' | 'TR' | 'TT' | 'TV' | 'TW' | 'TZ' | 'UA' | 'UG' | 'US' | 'UY' | 'UZ' | 'VA' | 'VC' | 'VE' | 'VG' | 'VI' | 'VN' | 'VU' | 'WF' | 'WS' | 'XK' | 'YE' | 'YT' | 'ZA' | 'ZM' | 'ZW';
  4. // Seems like it doesn't work for some reason:
  5. // https://gitlab.com/catamphetamine/libphonenumber-js/-/issues/94
  6. //
  7. // import metadata from './metadata.min.json'
  8. // // Reads the list of ISO country codes from the JSON file.
  9. // // https://github.com/catamphetamine/libphonenumber-js/issues/405#issuecomment-1447027961
  10. // const getObjectKeys = Object.keys as <T>(object: T) => Array<keyof T>
  11. // const CountryCodes = getObjectKeys(metadata.countries)
  12. // // The `CountryCode` type is generated from the list of `CountryCodes`.
  13. // // https://github.com/catamphetamine/libphonenumber-js/issues/405
  14. // export type CountryCode = typeof CountryCodes[number];
  15. export type CountryCallingCodes = {
  16. [countryCallingCode: string]: CountryCode[];
  17. };
  18. export type Countries = {
  19. // Metadata here is a compressed one,
  20. // so a country's data is just an array of some properties
  21. // instead of a JSON object of shape:
  22. // {
  23. // phone_code: string,
  24. // idd_prefix: string,
  25. // national_number_pattern: string,
  26. // types: object,
  27. // examples: object,
  28. // formats: object[]?,
  29. // possible_lengths: number[],
  30. // ...
  31. // }
  32. //
  33. // `in` operator docs:
  34. // https://www.typescriptlang.org/docs/handbook/advanced-types.html#mapped-types
  35. // `country in CountryCode` means "for each and every CountryCode".
  36. [country in CountryCode]?: any[];
  37. };
  38. export type MetadataJson = {
  39. country_calling_codes: CountryCallingCodes;
  40. countries: Countries;
  41. };
  42. export type Examples = {
  43. // `in` operator docs:
  44. // https://www.typescriptlang.org/docs/handbook/advanced-types.html#mapped-types
  45. // `country in CountryCode` means "for each and every CountryCode".
  46. [country in CountryCode]: NationalNumber;
  47. };
  48. export type NumberFormat = 'NATIONAL' | 'INTERNATIONAL' | 'E.164' | 'RFC3966' | 'IDD';
  49. export type NumberType = undefined | 'PREMIUM_RATE' | 'TOLL_FREE' | 'SHARED_COST' | 'VOIP' | 'PERSONAL_NUMBER' | 'PAGER' | 'UAN' | 'VOICEMAIL' | 'FIXED_LINE_OR_MOBILE' | 'FIXED_LINE' | 'MOBILE';
  50. type Tagged<A, T> = A & { __tag?: T };
  51. type E164Number = Tagged<string, "E164Number">;
  52. type NationalNumber = Tagged<string, "NationalNumber">;
  53. type Extension = Tagged<string, "Extension">;
  54. type CarrierCode = Tagged<string, "CarrierCode">;
  55. type CountryCallingCode = Tagged<string, "CountryCallingCode">;
  56. type FormatExtension = (formattedNumber: string, extension: Extension, metadata: MetadataJson) => string
  57. type FormatNumberOptionsWithoutIDD = {
  58. v2?: boolean;
  59. formatExtension?: FormatExtension;
  60. };
  61. type FormatNumberOptions = {
  62. v2?: boolean;
  63. fromCountry?: CountryCode;
  64. humanReadable?: boolean;
  65. nationalPrefix?: boolean;
  66. formatExtension?: FormatExtension;
  67. };
  68. // // https://stackoverflow.com/a/67026991
  69. // type ArrayOfAtLeastOneCountryCode = [CountryCode, ...CountryCode[]];
  70. export class PhoneNumber {
  71. constructor(countryCallingCodeOrCountry: CountryCallingCode | CountryCode, nationalNumber: NationalNumber, metadata: MetadataJson);
  72. countryCallingCode: CountryCallingCode;
  73. country?: CountryCode;
  74. nationalNumber: NationalNumber;
  75. number: E164Number;
  76. carrierCode?: CarrierCode;
  77. ext?: Extension;
  78. setExt(ext: Extension): void;
  79. getPossibleCountries(): CountryCode[];
  80. isPossible(): boolean;
  81. isValid(): boolean;
  82. getType(): NumberType;
  83. format(format: NumberFormat, options?: FormatNumberOptions): string;
  84. formatNational(options?: FormatNumberOptionsWithoutIDD): string;
  85. formatInternational(options?: FormatNumberOptionsWithoutIDD): string;
  86. getURI(options?: FormatNumberOptionsWithoutIDD): string;
  87. isNonGeographic(): boolean;
  88. isEqual(phoneNumber: PhoneNumber): boolean;
  89. }
  90. export interface NumberFound {
  91. number: PhoneNumber;
  92. startsAt: number;
  93. endsAt: number;
  94. }
  95. export interface NumberFoundLegacy {
  96. country: CountryCode;
  97. phone: NationalNumber;
  98. ext?: Extension;
  99. startsAt: number;
  100. endsAt: number;
  101. }
  102. export class ParseError {
  103. message: string;
  104. }
  105. export interface NumberingPlan {
  106. leadingDigits(): string | undefined;
  107. possibleLengths(): number[];
  108. IDDPrefix(): string;
  109. defaultIDDPrefix(): string | undefined;
  110. }
  111. // The rationale for having a separate type for the result "enum" instead of just a `string`:
  112. // https://github.com/catamphetamine/libphonenumber-js/issues/170#issuecomment-363156068
  113. export type ValidatePhoneNumberLengthResult = 'INVALID_COUNTRY' | 'NOT_A_NUMBER' | 'TOO_SHORT' | 'TOO_LONG' | 'INVALID_LENGTH';