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

chore: Add utility mapped type for Java-like enforcement of method parameter… #2212

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

ptesavol
Copy link
Collaborator

@ptesavol ptesavol commented Dec 15, 2023

Add Java-like type checking of method parameter types when implementing an interface, and when defining a callback.

Usage of strict interface implementation: class MyClass implements strictly<MyClass, MyInterface> { ... }

Results: shows readable compiler errors in IDE and compiler if the method signatures do not match exactly.

/**

Checks that the implementation of an interface has

the public methods defined in the interface with the exactly same

parameter types as the interface.

@param C - the class that implements the interface

@param I - the interface

@returns - an interface that is otherwise the same as I, but the methods

whose parameters do not match between C and I are marked with a type that

causes a compiler error when used in combination with the 'implements' keyword.

@example ts class MyClass implements strictly<MyClass, MyInterface> { ... }
*/

Usage of strictly typed callbacks:

/**

  • Ensures that the callback passed as an argument has exactly the same
  • parameter types as in the type definition. This utility type is
  • to be used in the definitions of functions that take callbacks as arguments.
  • No action from the callers of the function is required.
  • @param C - placeholder parameter for the type of the callback function passed as an argument
  • to the function call. NOTE: the function needs to be converted to a generic in order to use strictCb.
  • The compiler will automatically infer parameter C from the context when the function is called, and
  • the caller of the function does not need to know that the function is generic.
  • @param I - F the usual type definition of the callback function.
  • @returns - strict callback function type that causes a compiler error if the parameters
  • of the callback passed as an argument do not match exactly the parameters of the callback type definition
  • @example ts fetchValue<C>(callback: strictCb<C, (result: string) => void>): void { callback('some value') }
    */

@github-actions github-actions bot added dht Related to DHT package utils labels Dec 15, 2023
Base automatically changed from streamr-1.0 to main December 27, 2023 08:31
@harbu harbu changed the title Add utility mapped type for Java-like enforcement of method parameter… chore: Add utility mapped type for Java-like enforcement of method parameter… Jan 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dht Related to DHT package utils
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant