-
Notifications
You must be signed in to change notification settings - Fork 266
Allow passing undefined to optional properties with exactOptionalPropertyTypes #574
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
base: 0.2.x
Are you sure you want to change the base?
Conversation
It seems like overkill to me to update the library to use If your project has E.g. let className = '';
if (addClass) {
className = "mx-2";
} |
The configuration option
We're talking about one line to allow the property to be However, if you're aware of other properties that require the same change, I'm happy to include them here within the same change. So far, I'm not aware of any. |
I do understand what you’re saying. However, the property is already allowed to be undefined, by being marked as optional. The type evaluates as “string | undefined” already. The typescript setting you’re using is specifically designed to override that behaviour. By enabling it, you’re saying you want this alternate behaviour. Libraries that you consume should not need to be changed to avoid having to follow the rules of your project configuration. You could resolve this issue for yourself instantly by replacing the default undefined with an empty string, as per the rules of the exactOptionalPropertyTypes setting. If you don’t want to have to do that, then I have to wonder why you have the TS configuration enabled at all? (Not trying to be snarky, just genuinely curious) |
The property is defined as optional, yes. This means it can be omitted. However, it is not defined as allowing
FWIW, nearly all the libraries I have come in contact with adjust for this TypeScript configuration option already. Right now, react-fontawesome is the only library that still does not for me and my project. I think if you were to survey other big libraries, you'll find most define their types for compatibility with
I have already worked around the issue within my own codebase. You provided an option, but there are others as well. I'm here to try to improve react-fontawesome so other library consumers don't need to apply the same workarounds to bypass this false-error.
I have this option enabled because it strengthens the TypeScript type system in way that is similar to Thanks for the responses and for considering. |
@jdufresne very fair call mate, thanks for walking through it in detail! Given our discussion, I'm keen for this to go ahead, but would you be able to expand upon the PR so all optional props on |
00a8f35
to
f05ca92
Compare
…Types The TypeScript configuration option exactOptionalPropertyTypes is documented at: https://www.typescriptlang.org/tsconfig/#exactOptionalPropertyTypes > With exactOptionalPropertyTypes enabled, TypeScript applies stricter > rules around how it handles properties on type or interfaces which > have a ? prefix. This means TypeScript will handle "undefined" differently than "not provided". Consider the following construct: ```jsx function MyIcon({ addCLass: false }) { let className: string | undefined; if (addClass) { className = "mx-2"; } return <FontAwesomeIcon icon={["fal", "coffee"]} className={className}/> } ``` In this scenario, classname will sometimes be passed as "undefined". This is allowed by the implementation, so allow it through the type interface as well.
f05ca92
to
e24242a
Compare
That makes sense to me. I updated the PR to to apply the same change to all optional properties. |
The TypeScript configuration option exactOptionalPropertyTypes is documented at:
https://www.typescriptlang.org/tsconfig/#exactOptionalPropertyTypes
This means TypeScript will handle "undefined" differently than "not provided". Consider the following construct:
In this scenario, classname will sometimes be passed as "undefined". This is allowed by the implementation, so allow it through the type interface as well.