You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
import{forwardRef}from"react";exportinterfacePropsType{
name: string,}constRefTest=forwardRef<HTMLInputElement,PropsType>((props,ref)=>{return(<inputtype="text"placeholder="test"ref={ref}/>);});RefTest.defaultProps={name: "test"};export{RefTest};<RefTest/>// Property 'name' is missing in type '{}' but required in type 'PropsType'.
The current behavior
Property 'name' is missing in type '{}' but required in type 'PropsType'.
The expected behavior
It should compile fine. This code is similar to the one suggested in #16653 (link to comment)
The text was updated successfully, but these errors were encountered:
I insist that there IS a problem. Using the answer in the link above raises another issue, props (from the component's perspective) are now nullable, meaning that you need to null check them. defaultProps are supposed to provide the component with defaults so that it doesn't need to null check. For example:
importReact,{useState}from"react";constdefaultProps={validate: (value: string)=>(value==="hello")};exporttypeTestProps={firstName: string,lastName: string}&Partial<typeofdefaultProps>;constTestRef=React.forwardRef<HTMLInputElement,TestProps>((props,ref)=>{const[valid,setValid]=useState(false);return(<><div>{props.firstName}{props.lastName}</div><inputref={ref}onChange={(e)=>{setValid(props.validate(e.target.value))// ■ Cannot invoke an object which is possibly 'undefined'.}}/><span>{valid ? "valid" : "invalid"}</span></>);});TestRef.defaultProps=defaultProps;export{TestRef};
The error which says Cannot invoke an object which is possibly 'undefined' is not supposed to happen as the value will always be defined (thanks to defaultProps). If I put a @ts-ignore in front of it it goes and defaultProps function without a problem. This suggests that there IS a problem with the type definitions.
React version: 18.2.0
Steps To Reproduce
The current behavior
The expected behavior
It should compile fine. This code is similar to the one suggested in #16653 (link to comment)
The text was updated successfully, but these errors were encountered: