Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

typescript configuration should be more strict #1890

Open
alirezamirian opened this issue May 8, 2021 · 13 comments
Open

typescript configuration should be more strict #1890

alirezamirian opened this issue May 8, 2021 · 13 comments

Comments

@alirezamirian
Copy link
Contributor

alirezamirian commented May 8, 2021

馃摑 Feedback

Current typescript configuration doesn't have any of the strict flags. While it might not be the biggest problem for users of @react-spectrum packages, for users of @react-aria and @react-stately, it can cause problems which are mostly related to strictNullCheck flag.
A couple of examples of these problems:

馃敠 Context

We are using @react-aria and @react-stately to implement our own design system, and we have strict flag turned on in our tsconfig.

Suggestion

I suggest turning strict flag on. Or at least strictNullCheck. I would be happy to contribute by fixing current errors that would appear by strict flag.
It might be hard to fix all errors at once, because of conflict, PR size, etc. It can be split into a few PRs where the change to strict flag is committed in the last one.

@LFDanLu
Copy link
Member

LFDanLu commented May 13, 2021

This sounds reasonable to me, I'd be interested to see how much would need to be fixed.

@alirezamirian
Copy link
Contributor Author

@LFDanLu
1673 errors with strict set to true.
1593 errors with only strictNullCheck set to true.

@dannify
Copy link
Member

dannify commented May 14, 2021

Having to add | undefined (and even null?) to pretty much every type (based on the number of errors reported for strictNullCheck) can't be the only way to solve this issue right? This seems a little over the top. Are there no other options for this in typescript?

@ChocolateLoverRaj
Copy link

@dannify One possible method could be to bulk modify every function and add | undefined to all the parameters. This wouldn't change any internal behavior, but would decrease errors in packages that use the packages.

@dannify
Copy link
Member

dannify commented Jun 17, 2021

This is something I would be interested in doing. We would need it on all props as well. An option I'd like to explore is to handle this at build time instead of in the source code.

@ChocolateLoverRaj
Copy link

@dannify I made a babel plugin that adds | undefined to all parameters of exported functions. Can you try that and see it it can be used during build time?

@dannify
Copy link
Member

dannify commented Jun 23, 2021

I think this is something we would be able to build on and use. It would also need to include all types, including Object properties, within the component packages and the shared types as well (not just function parameters).

@ChocolateLoverRaj
Copy link

K I will add object values of functions parameters too.

@dannify What do you mean by

shared types

@dannify
Copy link
Member

dannify commented Jun 23, 2021

A lot of our packages use the @react-types package for their types (mostly in React Aria and React Spectrum)
https://github.com/adobe/react-spectrum/blob/main/packages/%40react-aria/textfield/src/useTextField.ts#L13 so we would want to include those as well.

@ChocolateLoverRaj
Copy link

@dannify So I looked at https://github.com/adobe/react-spectrum/blob/main/packages/%40react-types/textfield/src/index.d.ts and it exports interfaces. So I will make the babel plugin make every property in the interface optional.

@ChocolateLoverRaj
Copy link

@dannify I released v1.2.0 of the babel plugin. Can you try it out? Let me know what other modifications the plugin needs to do.

@dannify
Copy link
Member

dannify commented Jul 2, 2021

Ok great! Next week we have a break but I can take a look when we are back. Thanks for the update

@unional
Copy link
Contributor

unional commented May 17, 2024

Another thing to consider is enabling exactOptionalPropertyTypes.

i.e. all optional types should have | undefined.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants