Skip to content

Commit 1d30893

Browse files
emersionEthanPERRUZZA
authored andcommitted
front: replace some type assertions with non-null assertions
In general I prefer to avoid `as` type assertions as much as possible, because it makes the compiler ignore any type mismatch. The non-null assertion operator `!` is a bit better: if the types don't match, the compiler will error out. Use it in a bunch of places. See #8698 (comment)
1 parent 6e4015f commit 1d30893

File tree

43 files changed

+168
-201
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+168
-201
lines changed

front/src/applications/editor/Map.tsx

+15-18
Original file line numberDiff line numberDiff line change
@@ -174,13 +174,13 @@ const MapUnplugged = ({
174174
feature.properties.id !== toolState.hovered?.id
175175
) {
176176
partialToolState.hovered = {
177-
id: feature.properties?.id as string,
177+
id: feature.properties.id,
178178
type: LAYER_TO_EDITOAST_DICT[feature.sourceLayer as Layer],
179179
renderedEntity: feature,
180180
};
181181
} else if (feature.sourceLayer === 'errors') {
182182
partialToolState.hovered = {
183-
id: feature.properties?.obj_id as string,
183+
id: feature.properties.obj_id,
184184
type: feature.properties?.obj_type,
185185
renderedEntity: feature,
186186
error: feature.properties as InfraError,
@@ -255,23 +255,20 @@ const MapUnplugged = ({
255255
: e;
256256
if (toolState.hovered && activeTool.onClickEntity) {
257257
if (toolState.hovered.type) {
258-
getEntity(
259-
infraID as number,
260-
toolState.hovered.id,
261-
toolState.hovered.type,
262-
dispatch
263-
).then((entity) => {
264-
if (activeTool.onClickEntity) {
265-
// Those features lack a proper "geometry", and have a "_geometry"
266-
// instead. This fixes it:
267-
entity = {
268-
...entity,
269-
// eslint-disable-next-line no-underscore-dangle,@typescript-eslint/no-explicit-any
270-
geometry: entity.geometry || (entity as any)._geometry,
271-
};
272-
activeTool.onClickEntity(entity, eventWithFeature, extendedContext);
258+
getEntity(infraID!, toolState.hovered.id, toolState.hovered.type, dispatch).then(
259+
(entity) => {
260+
if (activeTool.onClickEntity) {
261+
// Those features lack a proper "geometry", and have a "_geometry"
262+
// instead. This fixes it:
263+
entity = {
264+
...entity,
265+
// eslint-disable-next-line no-underscore-dangle,@typescript-eslint/no-explicit-any
266+
geometry: entity.geometry || (entity as any)._geometry,
267+
};
268+
activeTool.onClickEntity(entity, eventWithFeature, extendedContext);
269+
}
273270
}
274-
});
271+
);
275272
}
276273
}
277274
if (activeTool.onClickMap) {

front/src/applications/editor/components/EntitySumUp.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -328,10 +328,10 @@ const EntitySumUp = ({ entity, id, objType, classes, status, error }: EntitySumU
328328
setState({ type: 'loading' });
329329

330330
if (!entity) {
331-
entity = await getEntity(infraID as number, id, objType as EditoastType, dispatch);
331+
entity = await getEntity(infraID!, id, objType as EditoastType, dispatch);
332332
}
333333

334-
const additionalEntities = await getAdditionalEntities(infraID as number, entity, dispatch);
334+
const additionalEntities = await getAdditionalEntities(infraID!, entity, dispatch);
335335
setState({
336336
type: 'ready',
337337
entity,

front/src/applications/editor/components/InfraErrors/InfraErrorCorrectorModal.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const InfraErrorCorrectorModal = () => {
1919

2020
const { data: infraAutoFixes, isLoading } =
2121
osrdEditoastApi.endpoints.getInfraByInfraIdAutoFixes.useQuery(
22-
{ infraId: infraId as number },
22+
{ infraId: infraId! },
2323
{ skip: !infraId }
2424
);
2525

front/src/applications/editor/components/LayersModal.tsx

+3-14
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,6 @@
11
import { useEffect, useMemo, useState } from 'react';
22

3-
import {
4-
groupBy,
5-
mapKeys,
6-
mapValues,
7-
sum,
8-
isString,
9-
isArray,
10-
uniq,
11-
isNil,
12-
concat,
13-
compact,
14-
} from 'lodash';
3+
import { groupBy, mapKeys, mapValues, sum, isString, isArray, uniq, concat, compact } from 'lodash';
154
import { useTranslation } from 'react-i18next';
165
import { GiElectric, GiUnplugged } from 'react-icons/gi';
176
import { MdSpeed } from 'react-icons/md';
@@ -80,8 +69,8 @@ const LayersModal = ({ initialLayers, selection, frozenLayers, onChange }: Layer
8069

8170
const { data: speedLimitTagsByInfraId } =
8271
osrdEditoastApi.endpoints.getInfraByInfraIdSpeedLimitTags.useQuery(
83-
{ infraId: infraID as number },
84-
{ skip: isNil(infraID) }
72+
{ infraId: infraID! },
73+
{ skip: !infraID }
8574
);
8675
const { data: speedLimitTags } = osrdEditoastApi.endpoints.getSpeedLimitTags.useQuery();
8776

front/src/applications/editor/components/LinearMetadata/FormComponent.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ export const FormComponent = (props: FieldProps) => {
421421
// Get the distance of the geometry
422422
const distance = useMemo(() => {
423423
if (!isNil(formContext.length)) {
424-
return formContext.length as number;
424+
return formContext.length!;
425425
}
426426
if (formContext.geometry?.type === 'LineString') {
427427
return getLineStringDistance(formContext.geometry);

front/src/applications/editor/tools/pointEdition/components/PointEditionLeftPanel.tsx

+10-12
Original file line numberDiff line numberDiff line change
@@ -72,24 +72,22 @@ const PointEditionLeftPanel = <Entity extends EditorEntity>({
7272

7373
useEffect(() => {
7474
const firstLoading = trackState.type === 'idle';
75-
const trackId = state.entity.properties.track as string | undefined;
75+
const trackId: string | undefined = state.entity.properties.track;
7676

7777
if (trackId && trackState.id !== trackId) {
7878
setTrackState({ type: 'isLoading', id: trackId });
79-
getEntity<TrackSectionEntity>(infraID as number, trackId, 'TrackSection', dispatch).then(
80-
(track) => {
81-
setTrackState({ type: 'ready', id: trackId, track });
79+
getEntity<TrackSectionEntity>(infraID!, trackId, 'TrackSection', dispatch).then((track) => {
80+
setTrackState({ type: 'ready', id: trackId, track });
8281

83-
if (!firstLoading) {
84-
const { position } = state.entity.properties;
85-
const turfPosition =
86-
(position * length(track, { units: 'meters' })) / track.properties.length;
87-
const point = along(track, turfPosition, { units: 'meters' });
82+
if (!firstLoading) {
83+
const { position } = state.entity.properties;
84+
const turfPosition =
85+
(position * length(track, { units: 'meters' })) / track.properties.length;
86+
const point = along(track, turfPosition, { units: 'meters' });
8887

89-
setState({ ...state, entity: { ...state.entity, geometry: point.geometry } });
90-
}
88+
setState({ ...state, entity: { ...state.entity, geometry: point.geometry } });
9189
}
92-
);
90+
});
9391
}
9492
}, [infraID, setState, state, state.entity.properties.track, trackState.id, trackState.type]);
9593

front/src/applications/editor/tools/pointEdition/tool-factory.tsx

+13-15
Original file line numberDiff line numberDiff line change
@@ -168,21 +168,19 @@ function getPointEditionTool<T extends EditorPoint>({
168168
newEntity.properties = newEntity.properties || {};
169169
newEntity.properties.track = nearestPoint.trackSectionID;
170170

171-
getEntity(infraID as number, newEntity.properties.track, 'TrackSection', dispatch).then(
172-
(track) => {
173-
const distanceAlongTrack = calculateDistanceAlongTrack(
174-
track as TrackSectionEntity,
175-
newEntity.geometry as Point
176-
);
177-
newEntity.properties.position = distanceAlongTrack;
171+
getEntity(infraID!, newEntity.properties.track, 'TrackSection', dispatch).then((track) => {
172+
const distanceAlongTrack = calculateDistanceAlongTrack(
173+
track as TrackSectionEntity,
174+
newEntity.geometry as Point
175+
);
176+
newEntity.properties.position = distanceAlongTrack;
178177

179-
setState({
180-
...state,
181-
entity: newEntity,
182-
nearestPoint: null,
183-
});
184-
}
185-
);
178+
setState({
179+
...state,
180+
entity: newEntity,
181+
nearestPoint: null,
182+
});
183+
});
186184
}
187185
},
188186
onMove(e, { setState, state }) {
@@ -247,7 +245,7 @@ function getPointEditionTool<T extends EditorPoint>({
247245

248246
if (typeof trackId !== 'string') return;
249247

250-
getEntity(infraID as number, trackId, 'TrackSection', dispatch).then((track) => {
248+
getEntity(infraID!, trackId, 'TrackSection', dispatch).then((track) => {
251249
const dbPosition = entity.properties.position;
252250
const computedPosition = nearestPointOnLine(
253251
(track as Feature<LineString>).geometry,

front/src/applications/editor/tools/rangeEdition/components/RangeEditionLeftPanel.tsx

+4-4
Original file line numberDiff line numberDiff line change
@@ -102,15 +102,15 @@ const RangeEditionLeftPanel = () => {
102102

103103
const { data: voltages } = osrdEditoastApi.endpoints.getInfraByInfraIdVoltages.useQuery(
104104
{
105-
infraId: infraID as number,
105+
infraId: infraID!,
106106
},
107107
{ skip: !infraID }
108108
);
109109

110110
const { data: speedLimitTagsByInfraId } =
111111
osrdEditoastApi.endpoints.getInfraByInfraIdSpeedLimitTags.useQuery(
112112
{
113-
infraId: infraID as number,
113+
infraId: infraID!,
114114
},
115115
{ skip: !infraID }
116116
);
@@ -190,7 +190,7 @@ const RangeEditionLeftPanel = () => {
190190
optionsState: { type: 'loading' },
191191
});
192192
const routesAndNodesPositions = await getRoutesFromSwitch({
193-
infraId: infraID as number,
193+
infraId: infraID!,
194194
body,
195195
}).unwrap();
196196
const { routes, available_node_positions } = routesAndNodesPositions;
@@ -206,7 +206,7 @@ const RangeEditionLeftPanel = () => {
206206
setSwitchesRouteCandidates(routes);
207207
setAvailableSwitchesPositions(available_node_positions);
208208
const trackRangesResults = await getTrackRangesByRoutes({
209-
infraId: infraID as number,
209+
infraId: infraID!,
210210
routes: routes.join(','),
211211
}).unwrap();
212212
const newRouteElements = makeRouteElements(trackRangesResults, routes);

front/src/applications/editor/tools/rangeEdition/electrification/ElectrificationEditionLayers.tsx

+22-28
Original file line numberDiff line numberDiff line change
@@ -93,20 +93,17 @@ export const ElectrificationEditionLayers = () => {
9393
),
9494
}));
9595

96-
getEntities<TrackSectionEntity>(
97-
infraID as number,
98-
missingTrackIDs,
99-
'TrackSection',
100-
dispatch
101-
).then((res) => {
102-
setState((s) => ({
103-
...s,
104-
trackSectionsCache: {
105-
...s.trackSectionsCache,
106-
...mapValues(res, (track) => ({ type: 'success', track }) as TrackState),
107-
},
108-
}));
109-
});
96+
getEntities<TrackSectionEntity>(infraID!, missingTrackIDs, 'TrackSection', dispatch).then(
97+
(res) => {
98+
setState((s) => ({
99+
...s,
100+
trackSectionsCache: {
101+
...s.trackSectionsCache,
102+
...mapValues(res, (track) => ({ type: 'success', track }) as TrackState),
103+
},
104+
}));
105+
}
106+
);
110107
}
111108
}, [entity.properties?.track_ranges]);
112109

@@ -121,20 +118,17 @@ export const ElectrificationEditionLayers = () => {
121118
},
122119
}));
123120

124-
getEntity<TrackSectionEntity>(
125-
infraID as number,
126-
hoveredItem.id,
127-
'TrackSection',
128-
dispatch
129-
).then((track) => {
130-
setState((s) => ({
131-
...s,
132-
trackSectionsCache: {
133-
...s.trackSectionsCache,
134-
[hoveredItem.id]: { type: 'success', track },
135-
},
136-
}));
137-
});
121+
getEntity<TrackSectionEntity>(infraID!, hoveredItem.id, 'TrackSection', dispatch).then(
122+
(track) => {
123+
setState((s) => ({
124+
...s,
125+
trackSectionsCache: {
126+
...s.trackSectionsCache,
127+
[hoveredItem.id]: { type: 'success', track },
128+
},
129+
}));
130+
}
131+
);
138132
}
139133
}, [hoveredItem]);
140134

front/src/applications/editor/tools/rangeEdition/speedSection/SpeedSectionEditionLayers.tsx

+22-28
Original file line numberDiff line numberDiff line change
@@ -163,20 +163,17 @@ export const SpeedSectionEditionLayers = () => {
163163
),
164164
}));
165165

166-
getEntities<TrackSectionEntity>(
167-
infraID as number,
168-
missingTrackIDs,
169-
'TrackSection',
170-
dispatch
171-
).then((res) => {
172-
setState((s) => ({
173-
...s,
174-
trackSectionsCache: {
175-
...s.trackSectionsCache,
176-
...mapValues(res, (track) => ({ type: 'success', track }) as TrackState),
177-
},
178-
}));
179-
});
166+
getEntities<TrackSectionEntity>(infraID!, missingTrackIDs, 'TrackSection', dispatch).then(
167+
(res) => {
168+
setState((s) => ({
169+
...s,
170+
trackSectionsCache: {
171+
...s.trackSectionsCache,
172+
...mapValues(res, (track) => ({ type: 'success', track }) as TrackState),
173+
},
174+
}));
175+
}
176+
);
180177
}
181178
}, [entity.properties?.track_ranges]);
182179

@@ -191,20 +188,17 @@ export const SpeedSectionEditionLayers = () => {
191188
},
192189
}));
193190

194-
getEntity<TrackSectionEntity>(
195-
infraID as number,
196-
hoveredItem.id,
197-
'TrackSection',
198-
dispatch
199-
).then((track) => {
200-
setState((s) => ({
201-
...s,
202-
trackSectionsCache: {
203-
...s.trackSectionsCache,
204-
[hoveredItem.id]: { type: 'success', track },
205-
},
206-
}));
207-
});
191+
getEntity<TrackSectionEntity>(infraID!, hoveredItem.id, 'TrackSection', dispatch).then(
192+
(track) => {
193+
setState((s) => ({
194+
...s,
195+
trackSectionsCache: {
196+
...s.trackSectionsCache,
197+
[hoveredItem.id]: { type: 'success', track },
198+
},
199+
}));
200+
}
201+
);
208202
}
209203
}, [hoveredItem]);
210204

front/src/applications/editor/tools/rangeEdition/tool-factory.tsx

+5-5
Original file line numberDiff line numberDiff line change
@@ -317,15 +317,15 @@ function getRangeEditionTool<T extends EditorRange>({
317317
if (interactionState.type === 'selectSwitch') {
318318
if (feature.sourceLayer && LAYERS_SET.has(feature.sourceLayer)) {
319319
const newHoveredItem = {
320-
id: feature.properties?.id as string,
320+
id: feature.properties.id,
321321
type: LAYER_TO_EDITOAST_DICT[feature.sourceLayer as Layer],
322322
renderedEntity: feature,
323323
};
324324
if (!isEqual(newHoveredItem, hoveredItem)) {
325325
if (feature.sourceLayer === 'switches') {
326326
setState({
327327
hovered: {
328-
id: feature.properties?.id as string,
328+
id: feature.properties.id,
329329
type: LAYER_TO_EDITOAST_DICT[feature.sourceLayer as Layer],
330330
renderedEntity: feature,
331331
},
@@ -376,8 +376,8 @@ function getRangeEditionTool<T extends EditorRange>({
376376
itemType: 'PSLSign',
377377
position: hoveredExtremity.geometry.coordinates,
378378
track: trackState.track,
379-
signIndex: feature.properties?.speedSectionSignIndex as number,
380-
signType: feature.properties?.speedSectionSignType as string,
379+
signIndex: feature.properties?.speedSectionSignIndex,
380+
signType: feature.properties?.speedSectionSignType,
381381
};
382382
if (!isEqual(newHoveredItem, hoveredItem))
383383
setState({
@@ -387,7 +387,7 @@ function getRangeEditionTool<T extends EditorRange>({
387387
// Handle hovering EditorEntity elements:
388388
else if (feature.sourceLayer && LAYERS_SET.has(feature.sourceLayer)) {
389389
const newHoveredItem = {
390-
id: feature.properties?.id as string,
390+
id: feature.properties.id,
391391
type: LAYER_TO_EDITOAST_DICT[feature.sourceLayer as Layer],
392392
renderedEntity: feature,
393393
};

0 commit comments

Comments
 (0)