diff --git a/front/src/reducers/osrdconf/stdcmConf/index.ts b/front/src/reducers/osrdconf/stdcmConf/index.ts index 12186a2ac29..6bf713d8fee 100644 --- a/front/src/reducers/osrdconf/stdcmConf/index.ts +++ b/front/src/reducers/osrdconf/stdcmConf/index.ts @@ -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 @@ -135,17 +135,18 @@ export const stdcmConfSlice = createSlice({ }, updateStdcmEnvironment( state: Draft, - 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; diff --git a/front/src/utils/types.ts b/front/src/utils/types.ts index 4d3d8598743..442110020ef 100644 --- a/front/src/utils/types.ts +++ b/front/src/utils/types.ts @@ -19,3 +19,29 @@ export const mapBy = (items: T[] | undefined, key: K): Map export const concatMap = (map1: Map, map2: Map) => 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 = { + [Property in keyof Type as Property extends K ? Property : never]-?: NonNullable; +} & { + [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, Extract>;