jackocnr / intl-tel-input

A JavaScript plugin for entering and validating international telephone numbers. React and Vue components also included.
https://intl-tel-input.com
MIT License
7.7k stars 1.95k forks source link

Typescript types definition support #433

Closed fdnhkj closed 8 years ago

fdnhkj commented 8 years ago

Hello!

This issue just to let you know I added Typescript definition file for this plugin in DefinitelyTyped repo (see merged PR).

intltelinput

I just did a PR to intl-tel-input repo to add this information to README.md .

jackocnr commented 8 years ago

Hi there! Thanks for putting this together. I don't understand your video example though. The second line doesn't make sense - it should start $.fn.intlTelInput, right?

fdnhkj commented 8 years ago

Indeed it doesn't look correct. I'll have a look later this week, thanks for your comment :)

fdnhkj commented 8 years ago

Fixed it - see merged PR.

Note

$.fn.intlTelInput static methods are not typed because of jquery typings. As $.fn has type any (jquery/jquery.d.ts#L958), we cannot define types for those methods.

jackocnr commented 8 years ago

Thanks! So now I'm wondering - without static method support, what exactly does this do? Presumably it doesn't support the public methods e.g. setNumber because they are called with the method name as a string e.g. $(".phone").intlTelInput("setNumber", "+44123");

fdnhkj commented 8 years ago

Defines intlTelInput method in JQuery instance

Without the definition file, intlTelInput method would not be available in JQuery instances. It would give following error :

capture d ecran 2016-06-25 a 08 22 06

Defines different signatures of the public API (parameters types, return type)

In that file, I defined the methods listed in the public API with correct param type and return type. If you call i.e. intlTelInput(options?: IntlTelInput.Options): JQueryDeferred<any>;, then you will only be able to use properties, functions defined in JQueryDeferred type.

capture d ecran 2016-06-25 a 08 06 22

Even if public methods are called with method name as a string, TypeScript allows you to have specialized overload signatures based on string constants. It allows you i.e. to specify correct return type depending on method name. That is what I did and what for example JQueryUI has also in its d.ts (see https://github.com/DefinitelyTyped/DefinitelyTyped/blob/741430a178043bfca40e3a343b8b6f7610104093/jqueryui/jqueryui.d.ts#L1009). So indeed we cannot restrict people from calling $('#phone').intlTelInput('xx');, but when they use the correct method name, they will have the correct return type.

capture d ecran 2016-06-25 a 08 34 13

Defines the options

When you will provide options object, it will have to be compliant (property name and correct type) with the Options interface defined in the d.ts file. For example, it will restrict you from doing this :

capture d ecran 2016-06-25 a 07 59 55 capture d ecran 2016-06-25 a 08 00 10

But will allow :

capture d ecran 2016-06-25 a 08 17 33

Defines intlTelInputUtils global variable and enums

I also defined intlTelInputUtils global var so TypeScript compiler will know what type it is; same for enums. Otherwise it will complain with such errors.

capture d ecran 2016-06-25 a 08 54 41 capture d ecran 2016-06-25 a 08 56 42
jackocnr commented 8 years ago

Very cool thank you!

romelgomez commented 5 years ago

First, sorry for up this old issue. I have small question about this types definitions.

After install npm i intl-tel-input --save npm i @types/intl-tel-input

I a new angular project, I trying import the lib, like this:

import { } from 'intl-tel-input';

And this error show up:

[ts] File '.... /sandbox/iti/node_modules/@types/intl-tel-input/index.d.ts' is not a module. [2306]

I think this maybe happen because is never exported, in base a similar issue that I find:

https://github.com/Microsoft/TypeScript/issues/11420#issuecomment-252113705

What I missing here?

mahermali commented 5 years ago

@romelgomez install Jquery type definitions helped me to overcome this issue npm install --save-dev @typings/jquery

jawadalnuaimi commented 4 years ago

Dear I tried use this plugin on oracle apex but only value in item inserted how I can get the extension also .

shrikantkalar023 commented 9 months ago

The newly added React component needs type definitions.