I have some endpoint types that I want to share between the client and server:
type EndpointParams = {
'/user': { userId: number },
'/post': { postId: number },
};
On the server, I want to define endpoint controllers:
interface Endpoint<T extends keyof EndpointParams> {
path: T;
handleRequest(params: EndpointParams[T]): void;
}
class UserEndpoint implements Endpoint<'/user'> {
path = '/user' as const;
handleRequest({ userId }) {}
}
However, I get the Binding element 'userId' implicitly has an 'any' type.. Why doesn't TS automatically figure out that userId is a number? Shouldn't it get that from the interface?
Some other related questions:
- Is it possible to omit the
as const? Without it, I getType 'string' is not assignable to type '"/user"'. - Is it possible to infer the generic parameter like with functions?
- Is it possible to make
pathstatic? AFAIK interfaces don't support static.