Motivation
I would liked to use object as a function parameter. This allow the caller to clearly define the arguments with the field names specified making reviews easier.
Problem
However, this is not very nice to deal with if you use implements and extends. This is what I am doing now.
src/domain/ServiceInterface.ts
export interface ServiceInterface {
doesThings(args: {
awesomeFieldName: string;
isThisAwesomeFieldName?: string;
ohWaitMoreAwesomeFieldName?: string;
}): boolean;
src/domain/ComposedServiceInterface.ts
import { ServiceInterface } from "./domain/ServiceInterface";
export type ComposedServiceInterface = ServiceInterface & { hello: () => string };
src/implementations/ComposedServiceImplementation.ts
import { ComposedServiceInterface } from "./domain/ComposedServiceInterface";
export class ComposedServiceImplementation implements ComposedServiceInterface {
doesThings(args: {
awesomeFieldName: string;
isThisAwesomeFieldName?: string;
ohWaitMoreAwesomeFieldName?: string;
}): boolean {
return true;
}
}
Attempts
1. Using type / interface for the object parameter
src/domain/ServiceInterface.ts
export type DoesThingsParameter = {
awesomeFieldName: string;
isThisAwesomeFieldName?: string;
ohWaitMoreAwesomeFieldName?: string;
};
export interface ServiceInterface {
doesThings(args: DoesThingsParameter): boolean;
src/domain/ComposedServiceInterface.ts
import { ServiceInterface } from "./domain/ServiceInterface";
export type ComposedServiceInterface = ServiceInterface & { hello: () => string };
src/implementations/ComposedServiceImplementation
import { ComposedServiceInterface } from "./domain/ComposedServiceInterface";
import { DoesThingsParameter } from "./domain/ServiceInterface";
export class ComposedServiceImplementation implements ComposedServiceInterface {
doesThings(args: DoesThingsParameter): boolean {
return true;
}
}
Gripe: I don't like this implemetation because src/implementations/ComposedServiceImplementation should not need to import from src/domain/ServiceInterface since it only implements ComposedServiceInterface
2. Using Parameter Utility Type
ref: https://www.typescriptlang.org/docs/handbook/utility-types.html#parameterstype
I can't seem to get typescript to agree using a Class method e.g. Parameter<ComposedServiceInterface.doesThings>
Can someone offer any advice?