Skip to content

Commit 2e0dc46

Browse files
committed
bug #548 [React] Fix window types (AlexandreGerault)
This PR was squashed before being merged into the 2.x branch. Discussion ---------- [React] Fix window types 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: ```tsx // `@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'; ``` Commits ------- 9b43394 [React] Fix window types
2 parents 843f1dd + 9b43394 commit 2e0dc46

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

src/React/Resources/assets/src/register_controller.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,20 @@
99

1010
'use strict';
1111

12+
import { ComponentClass, FunctionComponent } from 'react';
13+
14+
type Component = string | FunctionComponent<object> | ComponentClass<object, any>;
15+
16+
declare global {
17+
function resolveReactComponent(name: string): Component;
18+
19+
interface Window {
20+
resolveReactComponent(name: string): Component;
21+
}
22+
}
23+
1224
export function registerReactControllerComponents(context: __WebpackModuleApi.RequireContext) {
13-
const reactControllers: { [key: string]: object } = {};
25+
const reactControllers: { [key: string]: Component } = {};
1426

1527
const importAllReactComponents = (r: __WebpackModuleApi.RequireContext) => {
1628
r.keys().forEach((key) => (reactControllers[key] = r(key).default));
@@ -19,7 +31,7 @@ export function registerReactControllerComponents(context: __WebpackModuleApi.Re
1931
importAllReactComponents(context);
2032

2133
// Expose a global React loader to allow rendering from the Stimulus controller
22-
(window as any).resolveReactComponent = (name: string): object => {
34+
window.resolveReactComponent = (name: string): Component => {
2335
const component = reactControllers[`./${name}.jsx`] || reactControllers[`./${name}.tsx`];
2436
if (typeof component === 'undefined') {
2537
throw new Error('React controller "' + name + '" does not exist');

src/React/Resources/assets/src/render_controller.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { createRoot } from 'react-dom/client';
1414
import { Controller } from '@hotwired/stimulus';
1515

1616
export default class extends Controller {
17-
readonly componentValue: string;
17+
readonly componentValue?: string;
1818
readonly propsValue?: object;
1919

2020
static values = {
@@ -27,6 +27,10 @@ export default class extends Controller {
2727

2828
this._dispatchEvent('react:connect', { component: this.componentValue, props: props });
2929

30+
if (!this.componentValue) {
31+
throw new Error('No component specified.');
32+
}
33+
3034
const component = window.resolveReactComponent(this.componentValue);
3135
this._renderReactElement(React.createElement(component, props, null));
3236

0 commit comments

Comments
 (0)