Skip to content

Commit

Permalink
front: add PickAndNonNullableFields type
Browse files Browse the repository at this point in the history
Signed-off-by: Clara Ni <clara.ni@outlook.fr>
  • Loading branch information
clarani committed Jan 9, 2025
1 parent 8acc6c3 commit 2e83f60
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 12 deletions.
25 changes: 13 additions & 12 deletions front/src/reducers/osrdconf/stdcmConf/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { defaultCommonConf, buildCommonConfReducers } from 'reducers/osrdconf/os
import type { OsrdStdcmConfState, StdcmPathStep } from 'reducers/osrdconf/types';
import { addElementAtIndex } from 'utils/array';
import { isArrivalDateInSearchTimeWindow } from 'utils/date';
import type { ArrayElement } from 'utils/types';
import type { ArrayElement, PickAndNonNullableFields } from 'utils/types';

const DEFAULT_TOLERANCE = 1800; // 30min

Expand Down Expand Up @@ -136,17 +136,18 @@ export const stdcmConfSlice = createSlice({
},
updateStdcmEnvironment(
state: Draft<OsrdStdcmConfState>,
action: PayloadAction<{
infraID: number;
timetableID: number;
searchDatetimeWindow?: {
begin: Date;
end: Date;
};
electricalProfileSetId?: number;
temporarySpeedLimitGroupId?: number;
workScheduleGroupId?: number;
}>
action: PayloadAction<
PickAndNonNullableFields<
OsrdStdcmConfState,
| 'infraID'
| 'timetableID'
| 'electricalProfileSetId'
| 'workScheduleGroupId'
| 'temporarySpeedLimitGroupId'
| 'searchDatetimeWindow',
'infraID' | 'timetableID'
>
>
) {
const { searchDatetimeWindow } = action.payload;
state.infraID = action.payload.infraID;
Expand Down
26 changes: 26 additions & 0 deletions front/src/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,29 @@ export const mapBy = <K extends keyof T, T>(items: T[] | undefined, key: K): Map

export const concatMap = <K, V>(map1: Map<K, V>, map2: Map<K, V>) =>
new Map([...map1.entries(), ...map2.entries()]);

/**
* Transform the provided keys of an object to be non-nullable.
*
* -? removes the optional modifier
*
* NonNullable removes the nullable modifier
* @param Type The object type we want to transform
* @param K The keys we want to make non-nullable
* @example NonNullableObject<{ a?: string | null; b?: string; c?: string }, 'a' | 'c'> = { a: string; b?: string; c: string }
*/
type NonNullableObject<Type, K extends keyof Type> = {
[Property in keyof Type as Property extends K ? Property : never]-?: NonNullable<Type[Property]>;
} & {
[Property in keyof Type as Property extends K ? never : Property]: Type[Property];
};

/**
* Shortcut type to pick some properties of an object and make some of them non-nullable.
* @example PickAndNonNullableFields<{ a?: string | null; b?: string; c?: string }, 'a' | 'c', 'a'> = { a: string; c?: string }
*/
export type PickAndNonNullableFields<
Type,
PickKeys extends keyof Type,
TransformKeys extends keyof Type,
> = NonNullableObject<Pick<Type, PickKeys>, Extract<TransformKeys, PickKeys>>;

0 comments on commit 2e83f60

Please sign in to comment.