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

View File

@ -6,6 +6,7 @@ import { DriveFiles, GalleryPosts } from '../../../../../models';
import { genId } from '../../../../../misc/gen-id'; import { genId } from '../../../../../misc/gen-id';
import { GalleryPost } from '../../../../../models/entities/gallery-post'; import { GalleryPost } from '../../../../../models/entities/gallery-post';
import { ApiError } from '../../../error'; import { ApiError } from '../../../error';
import { DriveFile } from '@/models/entities/drive-file';
export const meta = { export const meta = {
tags: ['gallery'], tags: ['gallery'],
@ -55,7 +56,7 @@ export default define(meta, async (ps, user) => {
id: fileId, id: fileId,
userId: user.id userId: user.id
}) })
))).filter(file => file != null); ))).filter((file): file is DriveFile => file != null);
if (files.length === 0) { if (files.length === 0) {
throw new Error(); throw new Error();

View File

@ -5,6 +5,7 @@ import { ID } from '../../../../../misc/cafy-id';
import { DriveFiles, GalleryPosts } from '../../../../../models'; import { DriveFiles, GalleryPosts } from '../../../../../models';
import { GalleryPost } from '../../../../../models/entities/gallery-post'; import { GalleryPost } from '../../../../../models/entities/gallery-post';
import { ApiError } from '../../../error'; import { ApiError } from '../../../error';
import { DriveFile } from '@/models/entities/drive-file';
export const meta = { export const meta = {
tags: ['gallery'], tags: ['gallery'],
@ -58,7 +59,7 @@ export default define(meta, async (ps, user) => {
id: fileId, id: fileId,
userId: user.id userId: user.id
}) })
))).filter(file => file != null); ))).filter((file): file is DriveFile => file != null);
if (files.length === 0) { if (files.length === 0) {
throw new Error(); throw new Error();

View File

@ -93,7 +93,7 @@ export default abstract class Chart<T extends Record<string, any>> {
} }
@autobind @autobind
private static convertFlattenColumnsToObject(x: Record<string, number>) { private static convertFlattenColumnsToObject(x: Record<string, any>): Record<string, any> {
const obj = {} as any; const obj = {} as any;
for (const k of Object.keys(x).filter(k => k.startsWith(Chart.columnPrefix))) { for (const k of Object.keys(x).filter(k => k.startsWith(Chart.columnPrefix))) {
// now k is ___x_y_z // now k is ___x_y_z
@ -285,8 +285,7 @@ export default abstract class Chart<T extends Record<string, any>> {
const latest = await this.getLatestLog(group); const latest = await this.getLatestLog(group);
if (latest != null) { if (latest != null) {
const obj = Chart.convertFlattenColumnsToObject( const obj = Chart.convertFlattenColumnsToObject(latest) as T;
latest as Record<string, any>);
// 空ログデータを作成 // 空ログデータを作成
data = this.getNewLog(obj); data = this.getNewLog(obj);
@ -474,13 +473,13 @@ export default abstract class Chart<T extends Record<string, any>> {
const log = logs.find(l => isTimeSame(new Date(l.date * 1000), current)); const log = logs.find(l => isTimeSame(new Date(l.date * 1000), current));
if (log) { if (log) {
const data = Chart.convertFlattenColumnsToObject(log as Record<string, any>); const data = Chart.convertFlattenColumnsToObject(log);
chart.unshift(Chart.countUniqueFields(data)); chart.unshift(Chart.countUniqueFields(data) as T);
} else { } else {
// 隙間埋め // 隙間埋め
const latest = logs.find(l => isTimeBefore(new Date(l.date * 1000), current)); const latest = logs.find(l => isTimeBefore(new Date(l.date * 1000), current));
const data = latest ? Chart.convertFlattenColumnsToObject(latest as Record<string, any>) : null; const data = latest ? Chart.convertFlattenColumnsToObject(latest) as T : null;
chart.unshift(Chart.countUniqueFields(this.getNewLog(data))); chart.unshift(Chart.countUniqueFields(this.getNewLog(data)) as T);
} }
} }
} else if (span === 'day') { } else if (span === 'day') {
@ -497,14 +496,14 @@ export default abstract class Chart<T extends Record<string, any>> {
if (log) { if (log) {
if (logsForEachDays[currentDayIndex]) { if (logsForEachDays[currentDayIndex]) {
logsForEachDays[currentDayIndex].unshift(Chart.convertFlattenColumnsToObject(log)); logsForEachDays[currentDayIndex].unshift(Chart.convertFlattenColumnsToObject(log) as T);
} else { } else {
logsForEachDays[currentDayIndex] = [Chart.convertFlattenColumnsToObject(log)]; logsForEachDays[currentDayIndex] = [Chart.convertFlattenColumnsToObject(log) as T];
} }
} else { } else {
// 隙間埋め // 隙間埋め
const latest = logs.find(l => isTimeBefore(new Date(l.date * 1000), current)); const latest = logs.find(l => isTimeBefore(new Date(l.date * 1000), current));
const data = latest ? Chart.convertFlattenColumnsToObject(latest as Record<string, any>) : null; const data = latest ? Chart.convertFlattenColumnsToObject(latest) as T : null;
const newLog = this.getNewLog(data); const newLog = this.getNewLog(data);
if (logsForEachDays[currentDayIndex]) { if (logsForEachDays[currentDayIndex]) {
logsForEachDays[currentDayIndex].unshift(newLog); logsForEachDays[currentDayIndex].unshift(newLog);
@ -516,7 +515,7 @@ export default abstract class Chart<T extends Record<string, any>> {
for (const logs of logsForEachDays) { for (const logs of logsForEachDays) {
const log = this.aggregate(logs); const log = this.aggregate(logs);
chart.unshift(Chart.countUniqueFields(log)); chart.unshift(Chart.countUniqueFields(log) as T);
} }
} }