improve types
This commit is contained in:
parent
466c083233
commit
db3724cf33
4 changed files with 19 additions and 15 deletions
|
@ -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;
|
||||
|
|
|
@ -6,6 +6,7 @@ import { DriveFiles, GalleryPosts } from '../../../../../models';
|
|||
import { genId } from '../../../../../misc/gen-id';
|
||||
import { GalleryPost } from '../../../../../models/entities/gallery-post';
|
||||
import { ApiError } from '../../../error';
|
||||
import { DriveFile } from '@/models/entities/drive-file';
|
||||
|
||||
export const meta = {
|
||||
tags: ['gallery'],
|
||||
|
@ -55,7 +56,7 @@ export default define(meta, async (ps, user) => {
|
|||
id: fileId,
|
||||
userId: user.id
|
||||
})
|
||||
))).filter(file => file != null);
|
||||
))).filter((file): file is DriveFile => file != null);
|
||||
|
||||
if (files.length === 0) {
|
||||
throw new Error();
|
||||
|
|
|
@ -5,6 +5,7 @@ import { ID } from '../../../../../misc/cafy-id';
|
|||
import { DriveFiles, GalleryPosts } from '../../../../../models';
|
||||
import { GalleryPost } from '../../../../../models/entities/gallery-post';
|
||||
import { ApiError } from '../../../error';
|
||||
import { DriveFile } from '@/models/entities/drive-file';
|
||||
|
||||
export const meta = {
|
||||
tags: ['gallery'],
|
||||
|
@ -58,7 +59,7 @@ export default define(meta, async (ps, user) => {
|
|||
id: fileId,
|
||||
userId: user.id
|
||||
})
|
||||
))).filter(file => file != null);
|
||||
))).filter((file): file is DriveFile => file != null);
|
||||
|
||||
if (files.length === 0) {
|
||||
throw new Error();
|
||||
|
|
|
@ -93,7 +93,7 @@ export default abstract class Chart<T extends Record<string, any>> {
|
|||
}
|
||||
|
||||
@autobind
|
||||
private static convertFlattenColumnsToObject(x: Record<string, number>) {
|
||||
private static convertFlattenColumnsToObject(x: Record<string, any>): Record<string, any> {
|
||||
const obj = {} as any;
|
||||
for (const k of Object.keys(x).filter(k => k.startsWith(Chart.columnPrefix))) {
|
||||
// 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);
|
||||
|
||||
if (latest != null) {
|
||||
const obj = Chart.convertFlattenColumnsToObject(
|
||||
latest as Record<string, any>);
|
||||
const obj = Chart.convertFlattenColumnsToObject(latest) as T;
|
||||
|
||||
// 空ログデータを作成
|
||||
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));
|
||||
|
||||
if (log) {
|
||||
const data = Chart.convertFlattenColumnsToObject(log as Record<string, any>);
|
||||
chart.unshift(Chart.countUniqueFields(data));
|
||||
const data = Chart.convertFlattenColumnsToObject(log);
|
||||
chart.unshift(Chart.countUniqueFields(data) as T);
|
||||
} else {
|
||||
// 隙間埋め
|
||||
const latest = logs.find(l => isTimeBefore(new Date(l.date * 1000), current));
|
||||
const data = latest ? Chart.convertFlattenColumnsToObject(latest as Record<string, any>) : null;
|
||||
chart.unshift(Chart.countUniqueFields(this.getNewLog(data)));
|
||||
const data = latest ? Chart.convertFlattenColumnsToObject(latest) as T : null;
|
||||
chart.unshift(Chart.countUniqueFields(this.getNewLog(data)) as T);
|
||||
}
|
||||
}
|
||||
} else if (span === 'day') {
|
||||
|
@ -497,14 +496,14 @@ export default abstract class Chart<T extends Record<string, any>> {
|
|||
|
||||
if (log) {
|
||||
if (logsForEachDays[currentDayIndex]) {
|
||||
logsForEachDays[currentDayIndex].unshift(Chart.convertFlattenColumnsToObject(log));
|
||||
logsForEachDays[currentDayIndex].unshift(Chart.convertFlattenColumnsToObject(log) as T);
|
||||
} else {
|
||||
logsForEachDays[currentDayIndex] = [Chart.convertFlattenColumnsToObject(log)];
|
||||
logsForEachDays[currentDayIndex] = [Chart.convertFlattenColumnsToObject(log) as T];
|
||||
}
|
||||
} else {
|
||||
// 隙間埋め
|
||||
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);
|
||||
if (logsForEachDays[currentDayIndex]) {
|
||||
logsForEachDays[currentDayIndex].unshift(newLog);
|
||||
|
@ -516,7 +515,7 @@ export default abstract class Chart<T extends Record<string, any>> {
|
|||
|
||||
for (const logs of logsForEachDays) {
|
||||
const log = this.aggregate(logs);
|
||||
chart.unshift(Chart.countUniqueFields(log));
|
||||
chart.unshift(Chart.countUniqueFields(log) as T);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue