Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 1c9ea42

Browse files
authored
Conform more code to strictNullChecks (#10374)
* Apply `strictNullChecks` to `src/components/views/room_settings/*` * Restore tsconfig.json * Conform more code to `strictNullChecks` * Iterate * Update matrix-widget-api * Conform more code to `strictNullChecks`
1 parent 9c816bb commit 1c9ea42

Some content is hidden

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

44 files changed

+223
-179
lines changed

src/KeyBindingsManager.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import { IS_MAC } from "./Keyboard";
2525
* The combo is evaluated strictly, i.e. the KeyboardEvent must match exactly what is specified in the KeyCombo.
2626
*/
2727
export type KeyCombo = {
28-
key?: string;
28+
key: string;
2929

3030
/** On PC: ctrl is pressed; on Mac: meta is pressed */
3131
ctrlOrCmdKey?: boolean;

src/MatrixClientPeg.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ class MatrixClientPegClass implements IMatrixClientPeg {
306306
homeserverUrl: this.matrixClient.baseUrl,
307307
identityServerUrl: this.matrixClient.idBaseUrl,
308308
userId: this.matrixClient.credentials.userId,
309-
deviceId: this.matrixClient.getDeviceId(),
309+
deviceId: this.matrixClient.getDeviceId() ?? undefined,
310310
accessToken: this.matrixClient.getAccessToken(),
311311
guest: this.matrixClient.isGuest(),
312312
};

src/Searching.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,7 @@ async function combinedPagination(searchResult: ISeshatSearchResults): Promise<I
576576
const newSlice = result.results.slice(Math.max(result.results.length - newResultCount, 0));
577577
restoreEncryptionInfo(newSlice);
578578

579-
searchResult.pendingRequest = null;
579+
searchResult.pendingRequest = undefined;
580580

581581
return result;
582582
}

src/components/views/dialogs/CreateRoomDialog.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ interface IProps {
4141
defaultName?: string;
4242
parentSpace?: Room;
4343
defaultEncrypted?: boolean;
44-
onFinished(proceed?: boolean, opts?: IOpts): void;
44+
onFinished(proceed?: false): void;
45+
onFinished(proceed: true, opts: IOpts): void;
4546
}
4647

4748
interface IState {

src/components/views/dialogs/ModalWidgetDialog.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ interface IProps {
4646
widgetDefinition: IModalWidgetOpenRequestData;
4747
widgetRoomId?: string;
4848
sourceWidgetId: string;
49-
onFinished(success?: boolean, data?: IModalWidgetReturnData): void;
49+
onFinished(success: true, data: IModalWidgetReturnData): void;
50+
onFinished(success?: false, data?: void): void;
5051
}
5152

5253
interface IState {

src/components/views/elements/ImageView.tsx

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -133,13 +133,13 @@ export default class ImageView extends React.Component<IProps, IState> {
133133
// We want to recalculate zoom whenever the window's size changes
134134
window.addEventListener("resize", this.recalculateZoom);
135135
// After the image loads for the first time we want to calculate the zoom
136-
this.image.current.addEventListener("load", this.imageLoaded);
136+
this.image.current?.addEventListener("load", this.imageLoaded);
137137
}
138138

139139
public componentWillUnmount(): void {
140140
this.focusLock.current.removeEventListener("wheel", this.onWheel);
141141
window.removeEventListener("resize", this.recalculateZoom);
142-
this.image.current.removeEventListener("load", this.imageLoaded);
142+
this.image.current?.removeEventListener("load", this.imageLoaded);
143143
}
144144

145145
private imageLoaded = (): void => {
@@ -171,6 +171,7 @@ export default class ImageView extends React.Component<IProps, IState> {
171171
private setZoomAndRotation = (inputRotation?: number): void => {
172172
const image = this.image.current;
173173
const imageWrapper = this.imageWrapper.current;
174+
if (!image || !imageWrapper) return;
174175

175176
const rotation = inputRotation ?? this.state.rotation;
176177

@@ -236,8 +237,8 @@ export default class ImageView extends React.Component<IProps, IState> {
236237
// Zoom relative to the given point on the image.
237238
// First we need to figure out the offset of the anchor point
238239
// relative to the center of the image, accounting for rotation.
239-
let offsetX;
240-
let offsetY;
240+
let offsetX: number | undefined;
241+
let offsetY: number | undefined;
241242
// The modulo operator can return negative values for some
242243
// rotations, so we have to do some extra work to normalize it
243244
switch (((this.state.rotation % 360) + 360) % 360) {
@@ -310,7 +311,7 @@ export default class ImageView extends React.Component<IProps, IState> {
310311
private onDownloadClick = (): void => {
311312
const a = document.createElement("a");
312313
a.href = this.props.src;
313-
a.download = this.props.name;
314+
if (this.props.name) a.download = this.props.name;
314315
a.target = "_blank";
315316
a.rel = "noreferrer noopener";
316317
a.click();
@@ -334,9 +335,9 @@ export default class ImageView extends React.Component<IProps, IState> {
334335
ev.preventDefault();
335336
dis.dispatch<ViewRoomPayload>({
336337
action: Action.ViewRoom,
337-
event_id: this.props.mxEvent.getId(),
338+
event_id: this.props.mxEvent?.getId(),
338339
highlighted: true,
339-
room_id: this.props.mxEvent.getRoomId(),
340+
room_id: this.props.mxEvent?.getRoomId(),
340341
metricsTrigger: undefined, // room doesn't change
341342
});
342343
this.props.onFinished();
@@ -440,11 +441,11 @@ export default class ImageView extends React.Component<IProps, IState> {
440441

441442
let info: JSX.Element | undefined;
442443
if (showEventMeta) {
443-
const mxEvent = this.props.mxEvent;
444+
const mxEvent = this.props.mxEvent!;
444445
const showTwelveHour = SettingsStore.getValue("showTwelveHourTimestamps");
445446
let permalink = "#";
446447
if (this.props.permalinkCreator) {
447-
permalink = this.props.permalinkCreator.forEvent(this.props.mxEvent.getId());
448+
permalink = this.props.permalinkCreator.forEvent(mxEvent.getId());
448449
}
449450

450451
const senderName = mxEvent.sender?.name ?? mxEvent.getSender();
@@ -453,7 +454,7 @@ export default class ImageView extends React.Component<IProps, IState> {
453454
<a
454455
href={permalink}
455456
onClick={this.onPermalinkClicked}
456-
aria-label={formatFullDate(new Date(this.props.mxEvent.getTs()), showTwelveHour, false)}
457+
aria-label={formatFullDate(new Date(mxEvent.getTs()), showTwelveHour, false)}
457458
>
458459
<MessageTimestamp
459460
showFullDate={true}

src/components/views/elements/LazyRenderList.tsx

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,17 +66,17 @@ interface IProps<T> {
6666
// should typically be less than `overflowItems` unless applying
6767
// margins in the parent component when using multiple LazyRenderList in one viewport.
6868
// use 0 to only rerender when items will come into view.
69-
overflowMargin?: number;
69+
overflowMargin: number;
7070
// the amount of items to add at the top and bottom to render,
7171
// so not every scroll of causes a rerender.
72-
overflowItems?: number;
72+
overflowItems: number;
7373

7474
element?: string;
7575
className?: string;
7676
}
7777

7878
interface IState {
79-
renderRange: ItemRange | null;
79+
renderRange: ItemRange;
8080
}
8181

8282
export default class LazyRenderList<T = any> extends React.Component<IProps<T>, IState> {
@@ -88,9 +88,7 @@ export default class LazyRenderList<T = any> extends React.Component<IProps<T>,
8888
public constructor(props: IProps<T>) {
8989
super(props);
9090

91-
this.state = {
92-
renderRange: null,
93-
};
91+
this.state = LazyRenderList.getDerivedStateFromProps(props, {} as IState) as IState;
9492
}
9593

9694
public static getDerivedStateFromProps(props: IProps<unknown>, state: IState): Partial<IState> | null {

src/components/views/messages/EncryptionEvent.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ const ALGORITHM = "m.megolm.v1.aes-sha2";
3535

3636
const EncryptionEvent = forwardRef<HTMLDivElement, IProps>(({ mxEvent, timestamp }, ref) => {
3737
const cli = useContext(MatrixClientContext);
38-
const roomId = mxEvent.getRoomId();
38+
const roomId = mxEvent.getRoomId()!;
3939
const isRoomEncrypted = MatrixClientPeg.get().isRoomEncrypted(roomId);
4040

4141
const prevContent = mxEvent.getPrevContent() as IRoomEncryption;
@@ -51,13 +51,13 @@ const EncryptionEvent = forwardRef<HTMLDivElement, IProps>(({ mxEvent, timestamp
5151
if (prevContent.algorithm === ALGORITHM) {
5252
subtitle = _t("Some encryption parameters have been changed.");
5353
} else if (dmPartner) {
54-
const displayName = room.getMember(dmPartner)?.rawDisplayName || dmPartner;
54+
const displayName = room?.getMember(dmPartner)?.rawDisplayName || dmPartner;
5555
subtitle = _t(
5656
"Messages here are end-to-end encrypted. " +
5757
"Verify %(displayName)s in their profile - tap on their avatar.",
5858
{ displayName },
5959
);
60-
} else if (isLocalRoom(room)) {
60+
} else if (room && isLocalRoom(room)) {
6161
subtitle = _t("Messages in this chat will be end-to-end encrypted.");
6262
} else {
6363
subtitle = _t(

src/components/views/messages/MBeaconBody.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ const MBeaconBody: React.FC<IBodyProps> = React.forwardRef(({ mxEvent, getRelati
183183
<SmartMarker
184184
map={map}
185185
id={`${mapId}-marker`}
186-
geoUri={latestLocationState.uri}
186+
geoUri={latestLocationState?.uri}
187187
roomMember={markerRoomMember ?? undefined}
188188
useMemberColor
189189
/>

src/components/views/right_panel/TimelineCard.tsx

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ interface IProps {
4747
room: Room;
4848
onClose: () => void;
4949
resizeNotifier: ResizeNotifier;
50-
permalinkCreator?: RoomPermalinkCreator;
50+
permalinkCreator: RoomPermalinkCreator;
5151
e2eStatus?: E2EStatus;
5252
classNames?: string;
53-
timelineSet?: EventTimelineSet;
53+
timelineSet: EventTimelineSet;
5454
timelineRenderingType?: TimelineRenderingType;
5555
showComposer?: boolean;
5656
composerRelation?: IEventRelation;
@@ -77,7 +77,7 @@ export default class TimelineCard extends React.Component<IProps, IState> {
7777
private layoutWatcherRef: string;
7878
private timelinePanel = React.createRef<TimelinePanel>();
7979
private card = React.createRef<HTMLDivElement>();
80-
private readReceiptsSettingWatcher: string;
80+
private readReceiptsSettingWatcher: string | undefined;
8181

8282
public constructor(props: IProps) {
8383
super(props);
@@ -87,7 +87,6 @@ export default class TimelineCard extends React.Component<IProps, IState> {
8787
atEndOfLiveTimeline: true,
8888
narrow: false,
8989
};
90-
this.readReceiptsSettingWatcher = null;
9190
}
9291

9392
public componentDidMount(): void {
@@ -129,7 +128,7 @@ export default class TimelineCard extends React.Component<IProps, IState> {
129128
case Action.EditEvent:
130129
this.setState(
131130
{
132-
editState: payload.event ? new EditorStateTransfer(payload.event) : null,
131+
editState: payload.event ? new EditorStateTransfer(payload.event) : undefined,
133132
},
134133
() => {
135134
if (payload.event) {
@@ -199,7 +198,7 @@ export default class TimelineCard extends React.Component<IProps, IState> {
199198
};
200199

201200
public render(): React.ReactNode {
202-
const highlightedEventId = this.state.isInitialEventHighlighted ? this.state.initialEventId : null;
201+
const highlightedEventId = this.state.isInitialEventHighlighted ? this.state.initialEventId : undefined;
203202

204203
let jumpToBottom;
205204
if (!this.state.atEndOfLiveTimeline) {
@@ -232,7 +231,7 @@ export default class TimelineCard extends React.Component<IProps, IState> {
232231
header={this.renderTimelineCardHeader()}
233232
ref={this.card}
234233
>
235-
<Measured sensor={this.card.current} onMeasurement={this.onMeasurement} />
234+
{this.card.current && <Measured sensor={this.card.current} onMeasurement={this.onMeasurement} />}
236235
<div className="mx_TimelineCard_timeline">
237236
{jumpToBottom}
238237
<TimelinePanel

0 commit comments

Comments
 (0)