improve types

This commit is contained in:
syuilo
2021-05-28 09:34:42 +09:00
parent 466c083233
commit db3724cf33
4 changed files with 19 additions and 15 deletions

View File

@@ -5,6 +5,8 @@ import { ApiError } from './error';
import { SchemaType } from '@/misc/schema';
import { AccessToken } from '../../models/entities/access-token';
type NonOptional<T> = T extends undefined ? never : T;
type SimpleUserInfo = {
id: ILocalUser['id'];
host: ILocalUser['host'];
@@ -17,11 +19,12 @@ type SimpleUserInfo = {
isSilenced: ILocalUser['isSilenced'];
};
// TODO: defaultが設定されている場合はその型も考慮する
type Params<T extends IEndpointMeta> = {
[P in keyof T['params']]: NonNullable<T['params']>[P]['transform'] extends Function
? ReturnType<NonNullable<T['params']>[P]['transform']>
: ReturnType<NonNullable<T['params']>[P]['validator']['get']>[0];
: NonNullable<T['params']>[P]['default'] extends null | number | string
? NonOptional<ReturnType<NonNullable<T['params']>[P]['validator']['get']>[0]>
: ReturnType<NonNullable<T['params']>[P]['validator']['get']>[0];
};
export type Response = Record<string, any> | void;