AxaFrance / oidc-client

Light, Secure, Pure Javascript OIDC (Open ID Connect) Client. We provide also a REACT wrapper (compatible NextJS, etc.).
MIT License
582 stars 160 forks source link

TS2322 error while building #1294

Closed mteterel closed 7 months ago

mteterel commented 7 months ago

Issue and Steps to Reproduce

I am trying to implement this library into my front-end application using @axa-fr/react-oidc, I followed the README and everything works smoothly in development. However building my app results in type errors within the library.

node_modules/.pnpm/@axa-fr+react-oidc@7.17.0_react@18.2.0/node_modules/@axa-fr/react-oidc/src/User.ts:17:61 - error TS2322: Type 'null' is not assignable to type 'T'.
--
16:00:15.790 | 'T' could be instantiated with an arbitrary type which could be unrelated to 'null'.
16:00:15.790 |  
16:00:15.790 | 15 \|
16:00:15.790 | 16 \| export const useOidcUser = <T extends OidcUserInfo = OidcUserInfo>(configurationName = 'default') => {
16:00:15.790 | > 17 \|     const [oidcUser, setOidcUser] = useState<OidcUser<T>>({ user: null, status: OidcUserStatus.Unauthenticated });
16:00:15.790 | \|                                                             ^
16:00:15.790 | 18 \|     const [oidcUserId, setOidcUserId] = useState<string>('');
16:00:15.790 | 19 \|
16:00:15.791 | 20 \|     const oidc = OidcClient.get(configurationName);
node_modules/.pnpm/@axa-fr+react-oidc@7.17.0_react@18.2.0/node_modules/@axa-fr/react-oidc/src/User.ts:35:27 - error TS2322: Type 'null' is not assignable to type 'T'.
--
16:00:15.791 | 'T' could be instantiated with an arbitrary type which could be unrelated to 'null'.
16:00:15.791 |  
16:00:15.791 | 33 \|                 .catch(() => setOidcUser({ ...oidcUser, status: OidcUserStatus.LoadingError }));
16:00:15.791 | 34 \|         } else {
16:00:15.791 | > 35 \|             setOidcUser({ user: null, status: OidcUserStatus.Unauthenticated });
16:00:15.791 | \|                           ^
16:00:15.791 | 36 \|         }
16:00:15.791 | 37 \|         return () => { isMounted = false; };
16:00:15.791 | 38 \|     }, [oidcUserId]);

Versions

Currently using 7.17.0

Screenshots

image

guillaume-chervet commented 7 months ago

Hi @mteterel thank you for your issue. I have update build librairies few Day ago. It may have break something. Version 7.15 or 7.14 should work properly for you to continue.

Are you using vite.js with typescript ? Which stack are your using? To reproduce it.

guillaume-chervet commented 7 months ago

Version 7.15.4 @mteterel should work

mteterel commented 7 months ago

Negative, none of the versions after 7.0 seem to be working, I always get the same error message. I am indeed building with Vite.js+Typescript in strict mode. I'll try to reproduce it on a side project as well

guillaume-chervet commented 7 months ago

I work with some vite.js with js and typescript (default vite config) I did not have some trouble. If you can build a small sample. I coulb be able to fix typing errors @mteterel . It would be awesome.

mteterel commented 7 months ago

I solved it on my end, I was importing OidcUserStatus from "@axa-fr/react-oidc/src/User" instead of the top-level export.

However, the error that was logged is indeed a real one, OidcUser expects a T but does not specify it can be null, which it can when the userinfo has not been retrieved, however the useOidcUser hook initialize its value to null, causing TS2322. The lib has all strict mode build checks disabled, causing the error to go unreported. I tried re-enabling it to issue a PR to fix it but there are errors in most of the source code, so I'll leave you to that in the eventuality it was a voluntary choice.

guillaume-chervet commented 7 months ago

hi @mteterel thank you for all your information and feedbacks :) Version 7.17.2 should fix the typing error.

The library was historicaly in javascript. Typing are fixed completed by the time.