我想在客户端和服务器之间共享一些端点类型:
type EndpointParams = {
'/user': { userId: number },
'/post': { postId: number },
};
在服务器上,我想定义端点控制器:
interface Endpoint<T extends keyof EndpointParams> {
path: T;
handleRequest(params: EndpointParams[T]): void;
}
class UserEndpoint implements Endpoint<'/user'> {
path = '/user' as const;
handleRequest({ userId }) {}
}
但是,我得到了Binding element 'userId' implicitly has an 'any' type.
。 TS为什么不自动找出userId
是一个数字?它不应该从界面上得到它吗?
其他一些相关问题:
as const
?没有它,我得到Type 'string' is not assignable to type '"/user"'.
path
设为静态? AFAIK接口不支持静态。