Optional chaining operator on props produces wrong output in angular
See original GitHub issueI am interested in helping provide a fix!
Yes
Which generators are impacted?
- All
- Angular
- HTML
- Qwik
- React
- React-Native
- Solid
- Stencil
- Svelte
- Vue
- Web components
Reproduction case
Expected Behaviour
export default function MyComponent(props) {
const state = useStore({
value: true,
visibile: false,
});
const someRef = useRef(null);
return (
<div
ref={someRef}
className={classNames({ 'some-class': state.value })}
>
testing
</div>
);
}
should produce
@Component({
selector: "my-component, MyComponent",
template: `
<div>
<div [attr.id]="id" [class]="someClass">does not work</div>
</div>
`,
})
export class MyComponent {
@Input() id: any;
@Input() someClass: any;
}
Actual Behaviour
This
export default function MyComponent(props) {
return (
<div>
<div
id={props?.id}
className={props?.someClass}
>
does not work
</div>
</div>
);
}
produces
@Component({
selector: "my-component, MyComponent",
template: `
<div>
<div [attr.id]="props?.id" [class]="props?.someClass">does not work</div>
</div>
`,
})
export class MyComponent {}
but props
is not defined anywhere.
Additional Information
If you don’t use optional chaining operator ?.
works as expected.
Issue Analytics
- State:
- Created 10 months ago
- Comments:5 (5 by maintainers)
Top Results From Across the Web
What is wrong with optional chaining and how to fix it - DEV ...
Optional chaining simplifies above and removes a lot of errors by saying that values considered as no value are only two - null...
Read more >Optional Chaining in JavaScript returns undefined instead of ...
An optional chain does not evaluate to the nullish value on which the property was accessed, but to undefined - just like you...
Read more >Align with the optional chaining spec · Issue #34385 - GitHub
TL;DR: Angular Templates resolves to null in safe navigation and TypeScript resolves to undefined in optional chaining. Consider the following:.
Read more >The Beauty of the Optional Chaining Operator (?.) in TypeScript
The OR value in a simple optional chaining expression, triggered when the nullish check fails, is always undefined .
Read more >Optional Chaining: The ?. Operator in TypeScript
Optional chaining [is] a property access and function invocation operator that short-circuits if the value to access/invoke is nullish. The ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I am in the process of making the code that strips/replaces
props.
more intelligent, using babel ASTs. So we’ll wait for that to happen before digging into this any further, as it’s a lot priority issue.That’s fair. Was mostly pointing out that it’s never technically needed in the Mitosis code.
Fixing this should be as simple as updating this regex check:
https://github.com/BuilderIO/mitosis/blob/a762948e6014450d5b8b80c961b771df4568f847/packages/core/src/helpers/strip-state-and-props-refs.ts#L67-L71
to include an optional
?
, e.g./props(\??)\./