Skip to content

[React] Fix window types #548

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 21, 2022

Conversation

AlexandreGerault
Copy link
Contributor

@AlexandreGerault AlexandreGerault commented Nov 12, 2022

Hi!

As I discussed with @weaverryan on the Slack, I got an error with the symfony/ux-react while using it at work in a Sylius project.

Here was the log I had:

30 files written to public/build/shop
2022-11-12T14:17:23.295499920Z  ERROR  Failed to compile with 5 errors2:17:23 PM
2022-11-12T14:17:23.295526133Z 
2022-11-12T14:17:23.296104408Z  error  in /srv/sylius/vendor/symfony/ux-react/Resources/assets/src/register_controller.ts2:17:23 PM
2022-11-12T14:17:23.296117145Z 
2022-11-12T14:17:23.296138524Z [tsl] ERROR in /srv/sylius/vendor/symfony/ux-react/Resources/assets/src/register_controller.ts(12,60)
2022-11-12T14:17:23.296153391Z       TS2503: Cannot find namespace '__WebpackModuleApi'.
2022-11-12T14:17:23.296154753Z 
2022-11-12T14:17:23.296250335Z  error  in /srv/sylius/vendor/symfony/ux-react/Resources/assets/src/register_controller.ts2:17:23 PM
2022-11-12T14:17:23.296259533Z 
2022-11-12T14:17:23.296270017Z [tsl] ERROR in /srv/sylius/vendor/symfony/ux-react/Resources/assets/src/register_controller.ts(15,42)
2022-11-12T14:17:23.296271595Z       TS2503: Cannot find namespace '__WebpackModuleApi'.
2022-11-12T14:17:23.296320585Z 
2022-11-12T14:17:23.296401049Z  error  in /srv/sylius/vendor/symfony/ux-react/Resources/assets/src/register_controller.ts2:17:23 PM
2022-11-12T14:17:23.296408589Z 
2022-11-12T14:17:23.296453666Z [tsl] ERROR in /srv/sylius/vendor/symfony/ux-react/Resources/assets/src/register_controller.ts(16,27)
2022-11-12T14:17:23.296455663Z       TS7006: Parameter 'key' implicitly has an 'any' type.
2022-11-12T14:17:23.296462276Z 
2022-11-12T14:17:23.296522288Z  error  in /srv/sylius/vendor/symfony/ux-react/Resources/assets/src/render_controller.ts2:17:23 PM
2022-11-12T14:17:23.296537227Z 
2022-11-12T14:17:23.296544783Z [tsl] ERROR in /srv/sylius/vendor/symfony/ux-react/Resources/assets/src/render_controller.ts(17,14)
2022-11-12T14:17:23.296546386Z       TS2564: Property 'componentValue' has no initializer and is not definitely assigned in the constructor.
2022-11-12T14:17:23.296547827Z 
2022-11-12T14:17:23.296563284Z  error  in /srv/sylius/vendor/symfony/ux-react/Resources/assets/src/render_controller.ts2:17:23 PM
2022-11-12T14:17:23.296569130Z 
2022-11-12T14:17:23.296578146Z [tsl] ERROR in /srv/sylius/vendor/symfony/ux-react/Resources/assets/src/render_controller.ts(30,34)
2022-11-12T14:17:23.296580556Z       TS2339: Property 'resolveReactComponent' does not exist on type 'Window & typeof globalThis'.

To handle errors about __WebpackModuleApi I just installed the @types/webpack-env package.

But I still had errors for the window.resolveReactComponent function and for the componentValue field.

My PR goal is to fix the two type errors. Also I wonder if it is possible to generate (maybe in other PR) the declaration files so that it is compatible with Typescript files without having to use // @ts-expect-error (or // @ts-ignore) when importing the register react controllers function like so:

// @ts-expect-error No types exists
// eslint-disable-next-line import/no-extraneous-dependencies
import { registerReactControllerComponents } from '@symfony/ux-react';
import { FunctionComponent, ComponentClass } from 'react';

tsconfig.json Outdated
@@ -6,7 +6,7 @@
"noUnusedLocals": true,
"rootDir": "src",
"strict": true,
"strictPropertyInitialization": false,
"strictPropertyInitialization": true,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just realised I changed this to see if it was raising errors when this is set to true. It might break other packages...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better to turn it back off for now, I believe. But yes, I'd love to turn this back on later - @tgalopin turned it off originally in sha: b3b836b - but probably we will need to fix some other things when we do (any TypeScript fixing for any packages would be WARMLY welcomed, like this one) :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I gonna turn this off later today

if (!this.componentValue) {
throw new Error('No component specified for react controller');
}

Copy link
Contributor Author

@AlexandreGerault AlexandreGerault Nov 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just to solve the unitialized error that Typescript raise when compiling assets, as it seems to be related to stimulus code -- we don't initialise it here but I guess it would in the runtime


type Component = string | FunctionComponent<object> | ComponentClass<object, any>;

declare global {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This piece of code is inspired from a StackOverflow example and the Vue example

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only part I'm not sure about is how you define the type Component. But, I think it's pretty safe to use this and if there is a better way, someone will tell us :). This is just to make TypeScript happier. So, I think the worst-case scenario is that someone tells us a nicer way to make TypeScript happier.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with you. I just looked at how Typescript inferred the type where it is used.

If someone knows a better way, let's use it

Copy link
Member

@weaverryan weaverryan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just some minor tweaks - thanks so much for this!


type Component = string | FunctionComponent<object> | ComponentClass<object, any>;

declare global {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only part I'm not sure about is how you define the type Component. But, I think it's pretty safe to use this and if there is a better way, someone will tell us :). This is just to make TypeScript happier. So, I think the worst-case scenario is that someone tells us a nicer way to make TypeScript happier.

tsconfig.json Outdated
@@ -6,7 +6,7 @@
"noUnusedLocals": true,
"rootDir": "src",
"strict": true,
"strictPropertyInitialization": false,
"strictPropertyInitialization": true,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better to turn it back off for now, I believe. But yes, I'd love to turn this back on later - @tgalopin turned it off originally in sha: b3b836b - but probably we will need to fix some other things when we do (any TypeScript fixing for any packages would be WARMLY welcomed, like this one) :)

@AlexandreGerault
Copy link
Contributor Author

Just commited your suggestions. 😉

Hope that is enough to make it work! I wonder if it would be possible to generate declaration file too as I mentionned in OP

@weaverryan weaverryan force-pushed the fix/react_typescript_error branch from 53db2bc to 9b43394 Compare November 21, 2022 14:17
@weaverryan
Copy link
Member

Thanks @AlexandreGerault for the PR and your patience with the merge! About the declaration files, indeed, that would be great to have if I'm understanding the situation correctly. Cheers!

@weaverryan weaverryan merged commit 2e0dc46 into symfony:2.x Nov 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants