Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix(typescript): updated all array types to be readonly
  • Loading branch information
mlaursen committed Sep 7, 2021
1 parent 48d3d7f commit 8f71bcb
Show file tree
Hide file tree
Showing 33 changed files with 71 additions and 68 deletions.
8 changes: 4 additions & 4 deletions packages/alert/src/MessageQueueContext.tsx
Expand Up @@ -141,7 +141,7 @@ export type RestartVisibilityTimer = () => void;
* notifications. This will return the current queue at the time of reset if you
* would like to do some manual logic for adding items to the queue.
*/
export type ResetQueue<M extends Message> = () => M[];
export type ResetQueue<M extends Message> = () => readonly M[];

/**
* @internal
Expand Down Expand Up @@ -207,12 +207,12 @@ export function useMessageQueueActions<
/**
* @internal
*/
export const MessageQueueContext = createContext<Message[]>([]);
export const MessageQueueContext = createContext<readonly Message[]>([]);

/**
* This hook will allow you to get the current queue. This probably shouldn't be
* used that much.
*/
export function useQueue<M extends Message>(): M[] {
return useContext(MessageQueueContext) as M[];
export function useQueue<M extends Message>(): readonly M[] {
return useContext(MessageQueueContext) as readonly M[];
}
2 changes: 1 addition & 1 deletion packages/alert/src/SnackbarQueue.tsx
Expand Up @@ -22,7 +22,7 @@ export type ActionEventHandler<M extends ToastMessage> = (

export interface SnackbarQueueProps<M extends ToastMessage>
extends SnackbarProps {
queue: M[];
queue: readonly M[];
onActionClick?: ActionEventHandler<M>;
}

Expand Down
14 changes: 7 additions & 7 deletions packages/alert/src/useMessageQueue.ts
Expand Up @@ -75,10 +75,10 @@ export type MessageActions<M extends Message = ToastMessage> =
* @internal
*/
export function handleAddMessage<M extends Message = ToastMessage>(
state: M[],
state: readonly M[],
message: M,
duplicates: DuplicateBehavior
): M[] {
): readonly M[] {
if (state.length === 0) {
return [message];
}
Expand Down Expand Up @@ -132,17 +132,17 @@ export function handleAddMessage<M extends Message = ToastMessage>(
}

type MessageQueueReducer<M extends Message = ToastMessage> = Reducer<
M[],
readonly M[],
MessageActions<M>
>;

/**
* @internal
*/
export function reducer<M extends Message = ToastMessage>(
state: M[],
state: readonly M[],
action: MessageActions<M>
): M[] {
): readonly M[] {
switch (action.type) {
case ADD_MESSAGE:
return handleAddMessage(state, action.message, action.duplicates);
Expand All @@ -158,12 +158,12 @@ export function reducer<M extends Message = ToastMessage>(
export interface MessageQueueOptions<M extends Message = ToastMessage> {
timeout?: number;
duplicates?: DuplicateBehavior;
defaultQueue?: M[];
defaultQueue?: readonly M[];
}

export interface MessageQueueResult<M extends Message = ToastMessage>
extends MessageQueueActions<M> {
queue: M[];
queue: readonly M[];
visible: boolean;
addMessage: AddMessage<M>;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/autocomplete/src/AutoComplete.tsx
Expand Up @@ -27,7 +27,7 @@ const DEFAULT_FILTER_OPTIONS = {
ignoreWhitespace: true,
};

const EMPTY_LIST: string[] = [];
const EMPTY_LIST: readonly string[] = [];

/**
* An AutoComplete is an accessible combobox widget that allows for real-time
Expand Down
2 changes: 1 addition & 1 deletion packages/dev-utils/src/indexer/types.ts
Expand Up @@ -92,7 +92,7 @@ export interface RouteNavItem {
href: string;
children: string;
leftAddon?: ReactNode;
routes?: NavItem[];
routes?: readonly NavItem[];
}

export interface DividerNavItem {
Expand Down
4 changes: 2 additions & 2 deletions packages/dialog/src/NestedDialogContext.tsx
Expand Up @@ -10,7 +10,7 @@ import React, {
} from "react";

interface NestedDialogContext {
stack: string[];
stack: readonly string[];
add: (dialogId: string) => void;
remove: (dialogId: string) => void;
}
Expand Down Expand Up @@ -47,7 +47,7 @@ export interface NestedDialogContextProviderProps {
export function NestedDialogContextProvider({
children,
}: NestedDialogContextProviderProps): ReactElement {
const [stack, setStack] = useState<string[]>([]);
const [stack, setStack] = useState<readonly string[]>([]);
const add = useCallback((dialogId: string) => {
setStack((prevStack) => {
/* istanbul ignore next */
Expand Down
4 changes: 2 additions & 2 deletions packages/documentation/src/constants/navItems.tsx
Expand Up @@ -27,7 +27,7 @@ import { snakeCase } from "lodash";
const uuid = createIdGenerator("nav");
const TSDOCS_PREFIX = "/tsdocs/modules/_react_md_";

const getPackageRoutes = (name: string): RouteNavItem[] => {
const getPackageRoutes = (name: string): readonly RouteNavItem[] => {
const routes: RouteNavItem[] = [];

if (name === "form") {
Expand Down Expand Up @@ -76,7 +76,7 @@ const getPackageRoutes = (name: string): RouteNavItem[] => {
return routes;
};

const routes: NavItem[] = [
const routes: readonly NavItem[] = [
{
href: "/",
children: "Home",
Expand Down
10 changes: 5 additions & 5 deletions packages/expansion-panel/src/usePanels.ts
Expand Up @@ -46,7 +46,7 @@ export interface UsePanelsOptions {
* When this is omitted and `undefined`, no panels will be expanded by
* default.
*/
defaultExpandedIndex?: number | number[];
defaultExpandedIndex?: number | readonly number[];
}

/**
Expand Down Expand Up @@ -91,13 +91,13 @@ export interface ProvidedPanelProps {
onExpandClick(): void;
}

type ExpandedIds = string[];
type ExpandedIds = readonly string[];
type CreateExpandById = (panelId: string) => () => void;
type ExpansionDispatcher = Dispatch<SetStateAction<ExpandedIds>>;
type ExpansionPanelKeyDownHandler = KeyboardEventHandler<HTMLDivElement>;

type ReturnValue = [
ProvidedPanelProps[],
readonly ProvidedPanelProps[],
ExpansionPanelKeyDownHandler,
ExpandedIds,
ExpansionDispatcher,
Expand All @@ -109,7 +109,7 @@ type PanelMemo = Pick<ProvidedPanelProps, "id" | "headerRef">;
/**
* @internal
*/
const attemptFocus = (index: number, panels: PanelMemo[]): void => {
const attemptFocus = (index: number, panels: readonly PanelMemo[]): void => {
const panel = panels[index]?.headerRef.current;
if (panel) {
panel.focus();
Expand Down Expand Up @@ -217,7 +217,7 @@ export function usePanels({
}
}

const panels = useMemo<PanelMemo[]>(
const panels = useMemo<readonly PanelMemo[]>(
() =>
Array.from({ length: count }, (_, i) => ({
id: `${idPrefix}-${i + 1}`,
Expand Down
2 changes: 1 addition & 1 deletion packages/form/README.md
Expand Up @@ -104,7 +104,7 @@ const App = () => {
const [email, setEmail] = useState("");
const [password, setPassword] = useState("");
const [rememberMe, setRememberMe] = useChecked(false);
const [errors, setErrors] = useState<string[]>([]);
const [errors, setErrors] = useState<readonly string[]>([]);

const handleSubmit = async () => {
const response = await fetch("/login", {
Expand Down
11 changes: 5 additions & 6 deletions packages/form/src/file-input/useFileUpload.ts
Expand Up @@ -39,7 +39,7 @@ export interface FileUploadState<CustomError = never> {
* Each key in this object is the {@link BaseFileUploadStats.key} generated
* once the upload starts pending.
*/
stats: Record<string, FileUploadStats>;
stats: Readonly<Record<string, Readonly<FileUploadStats>>>;

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

/**
Expand Down Expand Up @@ -155,7 +155,7 @@ export interface FileUploadHookReturnValue<
*
* @see {@link getSplitFileUploads} for separating by status
*/
stats: readonly FileUploadStats[];
stats: readonly Readonly<FileUploadStats>[];

/**
* The total number of bytes for all the files that exist in the
Expand Down Expand Up @@ -218,9 +218,8 @@ export function useFileUpload<E extends HTMLElement, CustomError = never>({
onChange: propOnChange,
validateFiles = defaultValidateFiles,
getFileParser = defaultGetFileParser,
}: FileUploadOptions<E, CustomError> = {}): FileUploadHookReturnValue<
E,
CustomError
}: FileUploadOptions<E, CustomError> = {}): Readonly<
FileUploadHookReturnValue<E, CustomError>
> {
const [state, dispatch] = useReducer(
function reducer(
Expand Down
2 changes: 1 addition & 1 deletion packages/form/src/select/Listbox.tsx
Expand Up @@ -62,7 +62,7 @@ export interface ListboxOptions extends RenderConditionalPortalProps {
/**
* The list of options to display within the listbox.
*/
options: ListboxOption[];
options: readonly ListboxOption[];

/**
* A key to use that extracts the display label for an option from the options
Expand Down
4 changes: 2 additions & 2 deletions packages/form/src/select/NativeSelect.tsx
Expand Up @@ -79,7 +79,7 @@ export interface NativeSelectProps
*
* If the `multiple` prop is enabled, this **must** be a list of strings.
*/
value?: string | string[];
value?: string | readonly string[];

/**
* The default value for the text field which will make it uncontrolled. If
Expand All @@ -89,7 +89,7 @@ export interface NativeSelectProps
*
* If the `multiple` prop is enabled, this **must** be a list of strings.
*/
defaultValue?: string | string[];
defaultValue?: string | readonly string[];
}

const block = bem("rmd-native-select");
Expand Down
2 changes: 1 addition & 1 deletion packages/form/src/text-field/TextField.tsx
Expand Up @@ -117,7 +117,7 @@ export interface TextFieldProps

const block = bem("rmd-text-field");

const SPECIAL_TYPES: SupportedInputTypes[] = [
const SPECIAL_TYPES: readonly SupportedInputTypes[] = [
"date",
"time",
"datetime-local",
Expand Down
2 changes: 1 addition & 1 deletion packages/form/src/toggle/Radio.tsx
Expand Up @@ -7,7 +7,7 @@ export interface RadioProps extends InputToggleProps {
/**
* A value is required for a radio button unlike a checkbox.
*/
value: string[] | string | number;
value: readonly string[] | string | number;
}

/**
Expand Down
6 changes: 3 additions & 3 deletions packages/form/src/useChoice.ts
Expand Up @@ -5,8 +5,8 @@ type ChangeEventHandler<E extends InputElement> = React.ChangeEventHandler<E>;
type DefaultValue =
| string
| number
| string[]
| (() => string | number | string[]);
| readonly string[]
| (() => string | number | readonly string[]);
type SetValue<T extends DefaultValue> = Dispatch<SetStateAction<T>>;

/**
Expand All @@ -26,7 +26,7 @@ export function useChoice<
>(
defaultValue: T,
onChange?: ChangeEventHandler<E>
): [T, ChangeEventHandler<E>, SetValue<T>] {
): readonly [T, ChangeEventHandler<E>, SetValue<T>] {
const [value, setValue] = useState<T>(defaultValue);
const handleChange = useCallback<ChangeEventHandler<E>>(
(event) => {
Expand Down
4 changes: 2 additions & 2 deletions packages/form/src/useFieldStates.ts
Expand Up @@ -29,8 +29,8 @@ interface EventHandlers<E extends FormElement> {
* @remarks \@since 2.5.2
*/
interface FieldStatesOptions<E extends FormElement> extends EventHandlers<E> {
value?: string | string[];
defaultValue?: string | string[];
value?: string | readonly string[];
defaultValue?: string | readonly string[];
}

/**
Expand Down
4 changes: 2 additions & 2 deletions packages/form/src/useIndeterminateChecked.ts
Expand Up @@ -240,7 +240,7 @@ export function useIndeterminateChecked<V extends string>(
* }
*
*
* const [data, setData] = useState<ServerFetchedData[]>([]);
* const [data, setData] = useState<readonly ServerFetchedData[]>([]);
* const { getProps, rootProps, setCheckedValues } = useIndeterminateChecked(
* data.map(({ id }) => id),
* );
Expand All @@ -252,7 +252,7 @@ export function useIndeterminateChecked<V extends string>(
* const json = await response.json();
* if (!cancelled) {
* // pretend validation and sanity checks
* setData(json as ServerFetchedData[]);
* setData(json);
* setCheckedValues(json[0].id);
* }
* })();
Expand Down
3 changes: 2 additions & 1 deletion packages/layout/src/useLayoutNavigation.ts
Expand Up @@ -36,7 +36,8 @@ export interface LayoutNavigationState<
const getParentIds = (
itemId: TreeItemId,
navItems: TreeData<BaseTreeItem>
): TreeItemId[] => getItemsFrom(navItems, itemId).map(({ itemId }) => itemId);
): readonly TreeItemId[] =>
getItemsFrom(navItems, itemId).map(({ itemId }) => itemId);

/**
* This is used to disable the item select and multi item select functionality
Expand Down
2 changes: 1 addition & 1 deletion packages/material-icons/scripts/create.ts
Expand Up @@ -69,7 +69,7 @@ async function parseSVGFileAndCreateComponents(
]);
}

async function createIndexFile(components: string[]): Promise<void> {
async function createIndexFile(components: readonly string[]): Promise<void> {
const contents = `// This is a generated file from running the "createIcons" script. This file should not be updated manually.
${components.reduce(
(s, c) => `${s ? `${s}\n` : ""}export * from "./${c}";`,
Expand Down
2 changes: 1 addition & 1 deletion packages/menu/src/DropdownMenu.tsx
Expand Up @@ -59,7 +59,7 @@ export interface BaseDropdownMenuProps
* A list of menu items to render. Each item will be passed to the
* `menuItemRenderer` function.
*/
items: ValidMenuItem[];
items: readonly ValidMenuItem[];

/**
* A function to call for each `item` in the `items` list to render a
Expand Down
2 changes: 1 addition & 1 deletion packages/menu/src/defaultMenuRenderer.tsx
Expand Up @@ -54,7 +54,7 @@ export type InjectedMenuProps = LabelRequiredForA11y<AllInjectedMenuProps>;
*/
export type MenuRenderer = (
props: InjectedMenuProps,
items: ValidMenuItem[]
items: readonly ValidMenuItem[]
) => ReactNode;

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/states/src/ripples/types.ts
Expand Up @@ -24,7 +24,7 @@ export interface RippleState {
exiting: boolean;
entered: boolean;
}
export type RipplesState = RippleState[];
export type RipplesState = readonly RippleState[];

export type MergableRippleHandlerNames =
| "onKeyDown"
Expand Down
2 changes: 1 addition & 1 deletion packages/tabs/src/TabsManager.tsx
Expand Up @@ -45,7 +45,7 @@ export interface TabsManagerContext {
/**
* The list of tabs that should be controlled by the tabs manager.
*/
tabs: InitializedTabConfig[];
tabs: readonly InitializedTabConfig[];
}

export type InitializedTabsManagerContext = Required<TabsManagerContext>;
Expand Down
2 changes: 1 addition & 1 deletion packages/tabs/src/useTabsMovement.ts
Expand Up @@ -26,7 +26,7 @@ interface Options {
}

interface ReturnValue {
tabs: ReactNode[];
tabs: readonly ReactNode[];
itemRefs: ItemRefList;
handleClick: MouseEventHandler<HTMLDivElement>;
handleKeyDown: KeyboardEventHandler<HTMLDivElement>;
Expand Down
4 changes: 3 additions & 1 deletion packages/tree/src/types.ts
Expand Up @@ -86,7 +86,9 @@ export type TreeData<T extends BaseTreeItem> = Record<TreeItemId, T>;
* - `[c1, c2, c3]`
* - `[a, b, c]`
*/
export type TreeItemSorter<T extends BaseTreeItem> = (items: T[]) => T[];
export type TreeItemSorter<T extends BaseTreeItem> = (
items: readonly T[]
) => readonly T[];

/**
* A render function that allows you to add additional functionality to or
Expand Down

0 comments on commit 8f71bcb

Please sign in to comment.