// props is still an interface but it has behaviors as a classclassPropsextendsPropsWithChildren<any>{constructor(props)requiredString:string=props.optionalString!==undefined?props.optionalString:'yo';requiredNumber:number=props.optionalNumber!==undefined?props.optionalNumber:42;optionalBoolean?:boolean=props.optionalBoolean!==undefined?props.optionalBoolean:true;}exportdefaultfunctionMyTSComponent(props:Props){console.log(props);return(<>HereisMyComponent:<br/>{props.children}</>
);}
Here's two possible examples:
// I think this may work.exportdefaultfunctionMyTSComponent(props:Props){// But, this always workexportdefaultfunctionMyTSComponent(props){letproperties:props=newProps(props);
There is another way to cast Javascript into TS types. Like this:
//but this bypasses the ctor logicletnewtype:Props=props;// so we redesign the Props class.// meaning always initilize with default values, except for optional props.classPropsextendsPropsWithChildren<any>{requiredString:string='yo';requiredNumber:number42;optionalBoolean?:boolean;}
A number of javascript folks don't like the new keyword, no problem create a factory...
Adam,
From this.
To this?
Here's two possible examples:
There is another way to cast Javascript into TS types. Like this:
A number of javascript folks don't like the new keyword, no problem create a factory...
Ok Adam, updated my response.
When I type:
The TS linter tells me that:
Ok make that one a class too
Or change extends to implements.