remove most uses of as any in typescript

- replaced many uses of `as any` with another more specific cast `as T`
- filled in missing typed for items that needed to be typed
  - new runtime code was added where necessary to satisfy the new types with comments
- added missing theme variable "sidebar-active" to the Theme variables
- forms using `react-hook-form` are now typechecked
- changed some instances of `target` into `currentTarget` while removing `as any` assertions
This commit is contained in:
bree
2021-07-04 07:09:39 -04:00
parent 841320aab7
commit a4051330a3
31 changed files with 161 additions and 117 deletions

View File

@@ -119,8 +119,9 @@ export function SpecialInputModal(props: SpecialProps) {
question={<Text id="app.settings.permissions.create_role" />}
field={<Text id="app.settings.permissions.role_name" />}
callback={async name => {
// bree: this returns void, dunno why props.callback was being called
const role = await client.servers.createRole(props.server, name);
props.callback(role.id);
// props.callback(role.id);
}}
/>;
}

View File

@@ -1,6 +1,6 @@
import { Text } from "preact-i18n";
import { useState } from "preact/hooks";
import { useForm } from "react-hook-form";
import { SubmitHandler, useForm } from "react-hook-form";
import styles from "./Onboarding.module.scss";
import { takeError } from "../../revoltjs/util";
import Button from "../../../components/ui/Button";
@@ -14,12 +14,16 @@ interface Props {
callback: (username: string, loginAfterSuccess?: true) => Promise<void>;
}
interface FormInputs {
username: string
}
export function OnboardingModal({ onClose, callback }: Props) {
const { handleSubmit, register } = useForm();
const { handleSubmit, register } = useForm<FormInputs>();
const [loading, setLoading] = useState(false);
const [error, setError] = useState<string | undefined>(undefined);
async function onSubmit({ username }: { username: string }) {
const onSubmit: SubmitHandler<FormInputs> = ({ username }) => {
setLoading(true);
callback(username, true)
.then(onClose)
@@ -45,7 +49,7 @@ export function OnboardingModal({ onClose, callback }: Props) {
<p>
<Text id="app.special.modals.onboarding.pick" />
</p>
<form onSubmit={handleSubmit(onSubmit) as any}>
<form onSubmit={handleSubmit(onSubmit) as JSX.GenericEventHandler<HTMLFormElement>}>
<div>
<FormField
type="username"

View File

@@ -1,5 +1,5 @@
import { Text } from "preact-i18n";
import { useForm } from "react-hook-form";
import { SubmitHandler, useForm } from "react-hook-form";
import Modal from "../../../components/ui/Modal";
import { takeError } from "../../revoltjs/util";
import { useContext, useState } from "preact/hooks";
@@ -12,22 +12,28 @@ interface Props {
field: "username" | "email" | "password";
}
interface FormInputs {
password: string,
new_email: string,
new_username: string,
new_password: string,
// TODO: figure out if this is correct or not
// it wasn't in the types before this was typed but the element itself was there
current_password?: string
}
export function ModifyAccountModal({ onClose, field }: Props) {
const client = useContext(AppContext);
const { handleSubmit, register, errors } = useForm();
const { handleSubmit, register, errors } = useForm<FormInputs>();
const [error, setError] = useState<string | undefined>(undefined);
async function onSubmit({
const onSubmit: SubmitHandler<FormInputs> = async ({
password,
new_username,
new_email,
new_password
}: {
password: string;
new_username: string;
new_email: string;
new_password: string;
}) {
}) => {
try {
if (field === "email") {
await client.req("POST", "/auth/change/email", {
@@ -75,7 +81,8 @@ export function ModifyAccountModal({ onClose, field }: Props) {
}
]}
>
<form onSubmit={handleSubmit(onSubmit) as any}>
{/* Preact / React typing incompatabilities */}
<form onSubmit={handleSubmit(onSubmit) as JSX.GenericEventHandler<HTMLFormElement>}>
{field === "email" && (
<FormField
type="email"