Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Idea for JSON-RPC 2.0 interface definitions using TypeScript
export namespace api {
export interface Request<T extends string> {
params: any;
result: any;
__name: {
[name in T]: T
}
}
export interface Notification<T extends string> extends Request<T> {
result: void;
}
type Filter<T, U> = T extends U ? T : never;
type MethodName<T extends Request<string>> = Filter<keyof T['__name'], string>;
export type Params<T extends Request<MethodName<T>>> = T['params'];
export type Result<T extends Request<MethodName<T>>> = T['result'];
export type Method<T extends Request<MethodName<T>>, method extends string = MethodName<T>> = {
[name in method]: (params: Params<T>) => Result<T>
};
}
export namespace evolve {
export type Solution = any;
export type Metrics = any;
/**
* The [[evolve.combine]] request...
*/
export interface combine extends api.Request<'combine'> {
method: 'evolve.combine';
params: {
/** Solutions to be combined */
solutions: Solution[];
};
result: {
solution: Solution;
};
}
export interface hello extends api.Notification<'hello'> {
method: 'evolve.hello';
params: {
};
}
interface m extends api.Method<evolve.combine> {};
interface m extends api.Method<evolve.hello> {};
export type Methods = Partial<m>;
}
class Combiner implements api.Method<evolve.hello>, api.Method<evolve.combine> {
combine(params: api.Params<evolve.combine>): api.Result<evolve.combine> {
return [];
}
_hello(params: api.Params<evolve.hello>): api.Result<evolve.hello> {
// error due to _
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.