How to use Google libphonenumber in Typescript?
Asked Answered
M

4

10

I want to use Google libphonenumber in my angular project using Typescript. I have searched a lot on the internet and found a lot of stuff but could not find anything that could serve my purpose.

Most of the content available shows the code in JavaScript. If I use the same code in typescript, it shows a lot of errors like cannot find name require or module not found. Please tell me how/where/what to write the code.

Also, plz tell me which package to install as there are many - libphonenumber, google-libphonenumber, angular-libphonenumber

Milky answered 15/3, 2018 at 7:38 Comment(0)
O
17

When dealing with CommonJS libraries, in TypeScript just like this google-libphonenumber, I'd like to suggest 2 ways about it (Tested by me and works well).

Initially, I'd like to suggest to install from NPM just like this: npm install --save google-libphonenumber.

Then, here we go both ways of using it:

1st Method

Just import it directly

import libphonenumber from 'google-libphonenumber';
class Something {
    constructor() {//Just example, you can chose any method
        const phoneUtil = libphonenumber.PhoneNumberUtil.getInstance();
        console.log( phoneUtil.getSupportedRegions() );//It should works and give you some output
    }
}

2nd Method

You still can make the power of Typescript typing or just use the existing one by: npm install --save-dev @types/google-libphonenumber.

Since you said that you using Angular, so you can declare that typing just installed at src/tsconfig.app.json ( I am using Angular Version 7 ). Here is an example I have made:

{
  ...
  "compilerOptions": {
    ...
    "types": [
      "google-libphonenumber"
    ]
  },
  ...
}

Then you can just import it like usual, in Typescript "typings" way like follow:

import { PhoneNumberUtil } from 'google-libphonenumber';

class Something {
    constructor() {//Just example, you can chose any method
        const phoneUtil: PhoneNumberUtil = PhoneNumberUtil.getInstance();
        console.log( phoneUtil.getSupportedRegions() );//It should works and give you some output
    }
}
Octet answered 31/5, 2019 at 15:11 Comment(0)
M
3

you may either go with libphonenumber or google-libphonenumber as both of this library having a good number of installs also google-libphonenumber seems to be more powerful

Mayflower answered 15/3, 2018 at 7:48 Comment(2)
Thanks @SantoshSingh... do you have any idea about how to import google-libphonenumber into project and how to use it in project. I don't know where should I write this code : var phoneUtil = require('google-libphonenumber').PhoneNumberUtil.getInstance();Milky
Install npmjs.com/package/@types/google-libphonenumber and import * as modName from 'google-libphonenumber'Mayflower
H
0

Use ng2-tel-input . The only lib stable with angular 7 . ngx-tel-input has more features . But it has issues with ang7 .

Downloads from here

Exemple of use :

  <input type="text"
ng2TelInput
[ng2TelInputOptions]="{initialCountry: 'UK'}"
(hasError)="hasError($event)"
(countryChange)="onCountryChange($event)" />
Hangman answered 18/12, 2019 at 16:54 Comment(0)
B
0

You can also add the @types/google-libphonenumber package as a dev dependency without having to install the google-libphonenumber package too. This is achieved by using a root index.d.ts file with triple-slash directives and module declarations

/// <reference types="google-libphonenumber" />

declare module "google-libphonenumber";

I tend to do this with google libraries in general -- bonus: no imports required to consume these types in your codebase

  • Example from a current project
/// <reference types="gtag.js" />
/// <reference types="google.analytics" />
/// <reference types="google.maps" />

declare module 'google.maps';
declare module 'gtag.js';
declare module 'google.analytics';
Brasilin answered 17/9, 2021 at 4:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.