Skip to content

Commit bec74cc

Browse files
committed
front: add PickAndNonNullableFields type
Signed-off-by: Clara Ni <[email protected]>
1 parent 4cbdce7 commit bec74cc

File tree

2 files changed

+39
-12
lines changed

2 files changed

+39
-12
lines changed

front/src/reducers/osrdconf/stdcmConf/index.ts

+13-12
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { defaultCommonConf, buildCommonConfReducers } from 'reducers/osrdconf/os
1212
import type { OsrdStdcmConfState, StdcmPathStep } from 'reducers/osrdconf/types';
1313
import { addElementAtIndex } from 'utils/array';
1414
import { isArrivalDateInSearchTimeWindow } from 'utils/date';
15-
import type { ArrayElement } from 'utils/types';
15+
import type { ArrayElement, PickAndNonNullableFields } from 'utils/types';
1616

1717
const DEFAULT_TOLERANCE = 1800; // 30min
1818

@@ -135,17 +135,18 @@ export const stdcmConfSlice = createSlice({
135135
},
136136
updateStdcmEnvironment(
137137
state: Draft<OsrdStdcmConfState>,
138-
action: PayloadAction<{
139-
infraID: number;
140-
timetableID: number;
141-
searchDatetimeWindow?: {
142-
begin: Date;
143-
end: Date;
144-
};
145-
electricalProfileSetId?: number;
146-
temporarySpeedLimitGroupId?: number;
147-
workScheduleGroupId?: number;
148-
}>
138+
action: PayloadAction<
139+
PickAndNonNullableFields<
140+
OsrdStdcmConfState,
141+
| 'infraID'
142+
| 'timetableID'
143+
| 'electricalProfileSetId'
144+
| 'workScheduleGroupId'
145+
| 'temporarySpeedLimitGroupId'
146+
| 'searchDatetimeWindow',
147+
'infraID' | 'timetableID'
148+
>
149+
>
149150
) {
150151
const { searchDatetimeWindow } = action.payload;
151152
state.infraID = action.payload.infraID;

front/src/utils/types.ts

+26
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,29 @@ export const mapBy = <K extends keyof T, T>(items: T[] | undefined, key: K): Map
1919

2020
export const concatMap = <K, V>(map1: Map<K, V>, map2: Map<K, V>) =>
2121
new Map([...map1.entries(), ...map2.entries()]);
22+
23+
/**
24+
* Transform the provided keys of an object to be non-nullable.
25+
*
26+
* -? removes the optional modifier
27+
*
28+
* NonNullable removes the nullable modifier
29+
* @param Type The object type we want to transform
30+
* @param K The keys we want to make non-nullable
31+
* @example NonNullableObject<{ a?: string | null; b?: string; c?: string }, 'a' | 'c'> = { a: string; b?: string; c: string }
32+
*/
33+
type NonNullableObject<Type, K extends keyof Type> = {
34+
[Property in keyof Type as Property extends K ? Property : never]-?: NonNullable<Type[Property]>;
35+
} & {
36+
[Property in keyof Type as Property extends K ? never : Property]: Type[Property];
37+
};
38+
39+
/**
40+
* Shortcut type to pick some properties of an object and make some of them non-nullable.
41+
* @example PickAndNonNullableFields<{ a?: string | null; b?: string; c?: string }, 'a' | 'c', 'a'> = { a: string; c?: string }
42+
*/
43+
export type PickAndNonNullableFields<
44+
Type,
45+
PickKeys extends keyof Type,
46+
TransformKeys extends keyof Type,
47+
> = NonNullableObject<Pick<Type, PickKeys>, Extract<TransformKeys, PickKeys>>;

0 commit comments

Comments
 (0)