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*asReactfrom"react";import*asReactDOMfrom"react-dom";typeTextProps={editable: false}|{editable: true,onEdit: (newText: string)=>void}classTextComponentextendsReact.Component<TextProps,{}>{render(){return<span>SomeText..</span>;}}ReactDOM.render(<TextComponenteditable={true}/>,// FAIL: should be an error, but it's not// `onEdit` is missingdocument.getElementById("example"));// OK: correctly issues an errorReact.createElement(TextComponent,{editable: trueastrue});
Unfortunately, not only is the parameter presence not enforced, but also it cannot be provided even if the programmer somehow notices:
ReactDOM.render(// Property 'onEdit' does not exist on type ...<TextComponenteditable={true}onEdit={()=>{}}/>,document.getElementById("example"));
Type-safe workaround:
consttextProps: TextProps={editable: trueastrue,// Whoops, why not literal type?onEdit: ()=>{}};ReactDOM.render(<TextComponent{...textProps}/>,document.getElementById("example"));
Expected behavior:
The compiler should check whether the provided property set in JSX syntax matches the expected one according to the normal typing rules.
PS: Is there a way to see the desugarred TS?
The text was updated successfully, but these errors were encountered:
ReactDOM.render(<TextComponenteditable={true}/>,// FAIL: should be an error, but it's not// `onEdit` is missingdocument.getElementById("example"));
is that editable has type boolean while below editable has literal type of true as you cast it so
// OK: correctly issues an errorReact.createElement(TextComponent,{editable: trueastrue});
Talk with @RyanCavanaugh offline, we should treat JSX attribute/property as literal type. However, this will be quite a big change so I will push it into the next release of 2.1.
TypeScript Version: nightly (2.1.0-dev.20160805)
Code
Unfortunately, not only is the parameter presence not enforced, but also it cannot be provided even if the programmer somehow notices:
Type-safe workaround:
Expected behavior:
The compiler should check whether the provided property set in JSX syntax matches the expected one according to the normal typing rules.
PS: Is there a way to see the desugarred TS?
The text was updated successfully, but these errors were encountered: