Skip to content

Commit 8f71bcb

Browse files
committed
fix(typescript): updated all array types to be readonly
1 parent 48d3d7f commit 8f71bcb

File tree

33 files changed

+71
-68
lines changed

33 files changed

+71
-68
lines changed

packages/alert/src/MessageQueueContext.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ export type RestartVisibilityTimer = () => void;
141141
* notifications. This will return the current queue at the time of reset if you
142142
* would like to do some manual logic for adding items to the queue.
143143
*/
144-
export type ResetQueue<M extends Message> = () => M[];
144+
export type ResetQueue<M extends Message> = () => readonly M[];
145145

146146
/**
147147
* @internal
@@ -207,12 +207,12 @@ export function useMessageQueueActions<
207207
/**
208208
* @internal
209209
*/
210-
export const MessageQueueContext = createContext<Message[]>([]);
210+
export const MessageQueueContext = createContext<readonly Message[]>([]);
211211

212212
/**
213213
* This hook will allow you to get the current queue. This probably shouldn't be
214214
* used that much.
215215
*/
216-
export function useQueue<M extends Message>(): M[] {
217-
return useContext(MessageQueueContext) as M[];
216+
export function useQueue<M extends Message>(): readonly M[] {
217+
return useContext(MessageQueueContext) as readonly M[];
218218
}

packages/alert/src/SnackbarQueue.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export type ActionEventHandler<M extends ToastMessage> = (
2222

2323
export interface SnackbarQueueProps<M extends ToastMessage>
2424
extends SnackbarProps {
25-
queue: M[];
25+
queue: readonly M[];
2626
onActionClick?: ActionEventHandler<M>;
2727
}
2828

packages/alert/src/useMessageQueue.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,10 @@ export type MessageActions<M extends Message = ToastMessage> =
7575
* @internal
7676
*/
7777
export function handleAddMessage<M extends Message = ToastMessage>(
78-
state: M[],
78+
state: readonly M[],
7979
message: M,
8080
duplicates: DuplicateBehavior
81-
): M[] {
81+
): readonly M[] {
8282
if (state.length === 0) {
8383
return [message];
8484
}
@@ -132,17 +132,17 @@ export function handleAddMessage<M extends Message = ToastMessage>(
132132
}
133133

134134
type MessageQueueReducer<M extends Message = ToastMessage> = Reducer<
135-
M[],
135+
readonly M[],
136136
MessageActions<M>
137137
>;
138138

139139
/**
140140
* @internal
141141
*/
142142
export function reducer<M extends Message = ToastMessage>(
143-
state: M[],
143+
state: readonly M[],
144144
action: MessageActions<M>
145-
): M[] {
145+
): readonly M[] {
146146
switch (action.type) {
147147
case ADD_MESSAGE:
148148
return handleAddMessage(state, action.message, action.duplicates);
@@ -158,12 +158,12 @@ export function reducer<M extends Message = ToastMessage>(
158158
export interface MessageQueueOptions<M extends Message = ToastMessage> {
159159
timeout?: number;
160160
duplicates?: DuplicateBehavior;
161-
defaultQueue?: M[];
161+
defaultQueue?: readonly M[];
162162
}
163163

164164
export interface MessageQueueResult<M extends Message = ToastMessage>
165165
extends MessageQueueActions<M> {
166-
queue: M[];
166+
queue: readonly M[];
167167
visible: boolean;
168168
addMessage: AddMessage<M>;
169169
}

packages/autocomplete/src/AutoComplete.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const DEFAULT_FILTER_OPTIONS = {
2727
ignoreWhitespace: true,
2828
};
2929

30-
const EMPTY_LIST: string[] = [];
30+
const EMPTY_LIST: readonly string[] = [];
3131

3232
/**
3333
* An AutoComplete is an accessible combobox widget that allows for real-time

packages/dev-utils/src/indexer/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ export interface RouteNavItem {
9292
href: string;
9393
children: string;
9494
leftAddon?: ReactNode;
95-
routes?: NavItem[];
95+
routes?: readonly NavItem[];
9696
}
9797

9898
export interface DividerNavItem {

packages/dialog/src/NestedDialogContext.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import React, {
1010
} from "react";
1111

1212
interface NestedDialogContext {
13-
stack: string[];
13+
stack: readonly string[];
1414
add: (dialogId: string) => void;
1515
remove: (dialogId: string) => void;
1616
}
@@ -47,7 +47,7 @@ export interface NestedDialogContextProviderProps {
4747
export function NestedDialogContextProvider({
4848
children,
4949
}: NestedDialogContextProviderProps): ReactElement {
50-
const [stack, setStack] = useState<string[]>([]);
50+
const [stack, setStack] = useState<readonly string[]>([]);
5151
const add = useCallback((dialogId: string) => {
5252
setStack((prevStack) => {
5353
/* istanbul ignore next */

packages/documentation/src/constants/navItems.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import { snakeCase } from "lodash";
2727
const uuid = createIdGenerator("nav");
2828
const TSDOCS_PREFIX = "/tsdocs/modules/_react_md_";
2929

30-
const getPackageRoutes = (name: string): RouteNavItem[] => {
30+
const getPackageRoutes = (name: string): readonly RouteNavItem[] => {
3131
const routes: RouteNavItem[] = [];
3232

3333
if (name === "form") {
@@ -76,7 +76,7 @@ const getPackageRoutes = (name: string): RouteNavItem[] => {
7676
return routes;
7777
};
7878

79-
const routes: NavItem[] = [
79+
const routes: readonly NavItem[] = [
8080
{
8181
href: "/",
8282
children: "Home",

packages/expansion-panel/src/usePanels.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export interface UsePanelsOptions {
4646
* When this is omitted and `undefined`, no panels will be expanded by
4747
* default.
4848
*/
49-
defaultExpandedIndex?: number | number[];
49+
defaultExpandedIndex?: number | readonly number[];
5050
}
5151

5252
/**
@@ -91,13 +91,13 @@ export interface ProvidedPanelProps {
9191
onExpandClick(): void;
9292
}
9393

94-
type ExpandedIds = string[];
94+
type ExpandedIds = readonly string[];
9595
type CreateExpandById = (panelId: string) => () => void;
9696
type ExpansionDispatcher = Dispatch<SetStateAction<ExpandedIds>>;
9797
type ExpansionPanelKeyDownHandler = KeyboardEventHandler<HTMLDivElement>;
9898

9999
type ReturnValue = [
100-
ProvidedPanelProps[],
100+
readonly ProvidedPanelProps[],
101101
ExpansionPanelKeyDownHandler,
102102
ExpandedIds,
103103
ExpansionDispatcher,
@@ -109,7 +109,7 @@ type PanelMemo = Pick<ProvidedPanelProps, "id" | "headerRef">;
109109
/**
110110
* @internal
111111
*/
112-
const attemptFocus = (index: number, panels: PanelMemo[]): void => {
112+
const attemptFocus = (index: number, panels: readonly PanelMemo[]): void => {
113113
const panel = panels[index]?.headerRef.current;
114114
if (panel) {
115115
panel.focus();
@@ -217,7 +217,7 @@ export function usePanels({
217217
}
218218
}
219219

220-
const panels = useMemo<PanelMemo[]>(
220+
const panels = useMemo<readonly PanelMemo[]>(
221221
() =>
222222
Array.from({ length: count }, (_, i) => ({
223223
id: `${idPrefix}-${i + 1}`,

packages/form/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ const App = () => {
104104
const [email, setEmail] = useState("");
105105
const [password, setPassword] = useState("");
106106
const [rememberMe, setRememberMe] = useChecked(false);
107-
const [errors, setErrors] = useState<string[]>([]);
107+
const [errors, setErrors] = useState<readonly string[]>([]);
108108

109109
const handleSubmit = async () => {
110110
const response = await fetch("/login", {

packages/form/src/file-input/useFileUpload.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export interface FileUploadState<CustomError = never> {
3939
* Each key in this object is the {@link BaseFileUploadStats.key} generated
4040
* once the upload starts pending.
4141
*/
42-
stats: Record<string, FileUploadStats>;
42+
stats: Readonly<Record<string, Readonly<FileUploadStats>>>;
4343

4444
/**
4545
* A list of validation errors that have occurred before starting the upload
@@ -66,7 +66,7 @@ export interface FileUploadHookState<CustomError = never>
6666
*
6767
* Note: Once an upload has completed, the reader will be removed.
6868
*/
69-
readers: Record<string, FileReader>;
69+
readers: Readonly<Record<string, FileReader>>;
7070
}
7171

7272
/**
@@ -155,7 +155,7 @@ export interface FileUploadHookReturnValue<
155155
*
156156
* @see {@link getSplitFileUploads} for separating by status
157157
*/
158-
stats: readonly FileUploadStats[];
158+
stats: readonly Readonly<FileUploadStats>[];
159159

160160
/**
161161
* The total number of bytes for all the files that exist in the
@@ -218,9 +218,8 @@ export function useFileUpload<E extends HTMLElement, CustomError = never>({
218218
onChange: propOnChange,
219219
validateFiles = defaultValidateFiles,
220220
getFileParser = defaultGetFileParser,
221-
}: FileUploadOptions<E, CustomError> = {}): FileUploadHookReturnValue<
222-
E,
223-
CustomError
221+
}: FileUploadOptions<E, CustomError> = {}): Readonly<
222+
FileUploadHookReturnValue<E, CustomError>
224223
> {
225224
const [state, dispatch] = useReducer(
226225
function reducer(

0 commit comments

Comments
 (0)