I have a react component I made in the Visual Studio React + Redux template project with the following "render()" method:
public render() {
return (
<React.Fragment>
<h1>Welcome to the Wayfaring Company {this.props.user?.activeCharacter}!</h1>
</React.Fragment>
);
}
The idea is, display one message if the user property (etc.) exists, and another if it does not. When the page loads, it displays: "Welcome to the Wayfaring Company !", then changes to a typescript error screen with:
Failed to compile
./src/components/Splashpage.tsx
Syntax error: Expression expected (34:77)
(Which corresponds to the line with the .? in the render function above)
If I remove the optional chaining operation (the .?) I get a compilation error because user could be undefined. Great. Typescript's doing exactly what we want!
So why does the optional chain cause the code to compile in Visual Studio and fail in the browser? Am I somehow compiling the code to the wrong version of typescript?
Edit: updating to the pre-3.7 style Elvis operator doesn't cause the runtime error, so this does appear to be related to a Typescript versioning problem somehow.
<h1>Welcome to the Wayfaring Company, {this.props.user!== null && this.props.user!== undefined ? this.props.user.activeCharacter : "Scout"}!</h1>
Still trying to get to the bottom of this -- is IIS somehow running the wrong version of Typescript? I have a very good understanding of the what, but the why (and the how to fix) still elude me.
Is it possible that running this template through visual studio uses a version of Node.js that doesn't support the ?? or .? operators? (I think I'm using Node 12.X)