Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Add Typescript types #17

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

alex1290
Copy link

@alex1290 alex1290 commented Jun 6, 2022

This is still a draft.

Hi
Do you have the plan to add the Typescript types?
I hope this library can be used in Typescript withourt adding additional type declaretion by user.

Please advise.

@mikuso
Copy link
Owner

mikuso commented Jun 7, 2022

Hi alex,

This is something I would be interested in adding.

I will review this at my next opportunity. Thanks.

@alex1290 alex1290 force-pushed the feature_add_typescript_types branch from 527eefb to 313ac68 Compare June 7, 2022 13:43
@alex1290 alex1290 force-pushed the feature_add_typescript_types branch from 313ac68 to cde09bb Compare June 7, 2022 14:00
@dannysood
Copy link

Hi @mikuso , @alex1290 . Is there an update on this?

@mikuso
Copy link
Owner

mikuso commented Sep 9, 2022

To be honest I'm not a typescript developer and I don't feel comfortable incorporating this until I feel confident that I can support it long-term; I wouldn't want to bring in type definitions for a single release and then have them become incorrect in later releases due to neglect. Additionally, I don't know how to test these type definitions for completeness or correctness.

While I would love to natively support typescript types, it presents a challenge that I'm not quite ready for right now. One option might be for me to rewrite the whole module in TS for a 2.0.0 release.

In the meantime, if someone wants to take on the task of writing and maintaining the typescript types until then, it would probably be best to contribute these to the DefinitelyTyped project instead.

You may also have luck in making a request for this on their requests boards.

@liketurbo
Copy link

@alex1290 Did you try to push it into DefinitelyTyped?

@liketurbo
Copy link

liketurbo commented Feb 3, 2023

To be honest I'm not a typescript developer and I don't feel comfortable incorporating this until I feel confident that I can support it long-term; I wouldn't want to bring in type definitions for a single release and then have them become incorrect in later releases due to neglect. Additionally, I don't know how to test these type definitions for completeness or correctness.

While I would love to natively support typescript types, it presents a challenge that I'm not quite ready for right now. One option might be for me to rewrite the whole module in TS for a 2.0.0 release.

In the meantime, if someone wants to take on the task of writing and maintaining the typescript types until then, it would probably be best to contribute these to the DefinitelyTyped project instead.

You may also have luck in making a request for this on their requests boards.

I can help with whole rewrite into TS

@liketurbo
Copy link

To be honest I'm not a typescript developer and I don't feel comfortable incorporating this until I feel confident that I can support it long-term; I wouldn't want to bring in type definitions for a single release and then have them become incorrect in later releases due to neglect. Additionally, I don't know how to test these type definitions for completeness or correctness.
While I would love to natively support typescript types, it presents a challenge that I'm not quite ready for right now. One option might be for me to rewrite the whole module in TS for a 2.0.0 release.
In the meantime, if someone wants to take on the task of writing and maintaining the typescript types until then, it would probably be best to contribute these to the DefinitelyTyped project instead.
You may also have luck in making a request for this on their requests boards.

I can help with whole rewrite into TS

If you provide contribution info

@alex1290
Copy link
Author

@alex1290 Did you try to push it into DefinitelyTyped?

No. I do not have enough time to complete it after that.
I would be grateful if you can complete it and push it into DefinitelyTyped

@mikuso Got it. Some fuction I didn't use in my project, so I don't have confidenice to define the whole type.
I'm looking foward 2.0.0 releases. Thanks!

@ease
Copy link

ease commented Oct 26, 2023

@mikuso any chance of reconsidering moving to TypeScript or at least have some basic support? Your library is probably the best in its category but it just misses such an important feature which makes dev's life much easier.

@mikuso
Copy link
Owner

mikuso commented Oct 26, 2023

Hi @ease

Thanks for the kind words.

I believe that the most important TS feature people are looking for with this library is to get some assurances that they have structured their OCPP payloads correctly, ensuring that calls are valid, and making it easier to handle the responses. Strict mode already ensures that the calls and responses will be valid for the protocol you're using, but you'd be right to say that it would be even better if it connected the dots to allow for compile-time checking too.

I did make some significant headway on a port to typescript (see the types branch), but I wasn't really satisfied with the way it was going...

I think that the main difficulty I'm facing with a typescript version is that the current API (specifically the call and handle methods) were not really written with typescript in mind from the start, and it requires some significant typescript acrobatics to let the compiler know which protocol you are using in order to provide the correct feedback to the developer at compile-time. For example, the RPCClient can potentially "change shape" depending on which server it connects to, thanks to its ability to dynamically agree a protocol with the server at runtime. Similarly, the RPCServer's client event can't easily communicate to the event listener what "shape" of RPCClient they're being given, as at compile-time we can only be sure that it's one of the protocols provided in the server constructor - if any. One (ugly) solution might be to emit different events for different clients - although this only solves half the problem. A better solution might be to provide a full paint-by-numbers framework where the developer only needs to implement the methods for all their supported protocols and then can ignore client handling altogether - but this would be a significant change.

Unfortunately, the whole idea of strict mode and validation came as an afterthought, as I had originally intended for this module to also be used for other non-OCPP purposes; I didn't want the module to necessarily be aware of which protocol it was talking, so none of this really crossed my mind at the time when I created it. Ultimately, the module's structure lacks a lot of the formalities that you'd expect from a library that ought to be more aware of its most common use-case.

I think that a proper (useful) TS re-write would require making some significant changes to the API to make it more TS-friendly, but I'm struggling to justify the time commitment to do this at the moment.

@pschoffer
Copy link

I started an alternative approach to this - #68 feel free to close my PR if you want to return to this work in the future - @mikuso

@madpilot
Copy link

madpilot commented Feb 12, 2024

I have a basically complete index.d.ts file (including all the required acrobatics).

I'm happy to try putting it in to DefinatelyTyped, but we could also look at simply including it in the lib, if anyone is interested?

See here: #70

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants