Answer the question
In order to leave comments, you need to log in
How to check the type of function parameters?
Type for object methods - AuthFormsType
type AuthFormsType = {
[s:string] : (props:FormPropsType | {path:string})=>React.ReactNode;
}
export type FormPropsType = {
formName:string;
onSubmit:ReturnType<typeof submitHandlersCreator>;
changeFormStateHandler:ReturnType<changeFormStateHandlerType>;
}
const authForms:AuthFormsType = {
login:(props) =>{
if(props === typeof FormPropsType ){
return <Login
formName={props.fromName}
onSubmit={props.onSubmit}
changeFormStateHandler={props.changeFormStateHandler}
/>
}
return <Login path={props.path} />
},
Answer the question
In order to leave comments, you need to log in
There are no types in runtime, so you need to check according to what is, for example, write something like this typeguard:
const isFormPropsType = (v: FormPropsType | {path: string}): v is FormPropsType => typeof (v as FormPropsType).formName === 'string';
const authForms:AuthFormsType = {
login:(props) =>{
if(isFormPropsType(props)){
return <Login
formName={props.fromName}
onSubmit={props.onSubmit}
changeFormStateHandler={props.changeFormStateHandler}
/>
}
return <Login path={props.path} />
},
const authForms:AuthFormsType = {
login:(props) => {
return <Login {...props} />
},
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question