-
Notifications
You must be signed in to change notification settings - Fork 12.3k
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
Unable to specify 'if T is optional' in a conditional Type #23126
Comments
I guess you want you can also remove the optionality later on using type MappedOptions<T> = {
readonly [P in keyof T]-?: undefined extends T[P] ? NumberOption : StringOption
} |
I was just about to reply and say that still doesn't work but string null checks wasn't turned on. Now does exactly what I want. Thanks |
I arrived looking for these, and was able to piece it together based on the great information above. |
Should edit: Nevermind, looks like this was fixed in 3.1.0 |
I'm looking for a way to create optional props conditionally, let's say, if prop |
Thank you @aleclarson, testing whether a parameter is optional works well using |
Hi, I'm trying to find any docs on ##Edit Found this PR #21919 |
TypeScript Version: 2.8.1
Search Terms:
condition types, unions, optional, undefined
Code
Expected behavior:
as
optionalParam
is optional you should be able to switch on that in conditional type so that when mappedoptionalParam
is of typeStringOption
andrequiredParam
is of typeNumberOption
.Actual behavior:
both types are of type
NumberOption
as neither extend undefined. A syntax is needed to say that a type includes a type in a union I suppose and as far as I know this is not yet available.Playground Link:
Playground link
The text was updated successfully, but these errors were encountered: