-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Mapped Tuple Type, removing optional modifier always removes undefined from property type #31810
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
Comments
This workaround works, //strictNullChecks
function foo (_0 : number, _opt? : string) {
}
type AddUndefined<T, OriginalT> = (
{
[index in keyof T] : (
undefined extends OriginalT[Extract<index, keyof OriginalT>] ?
undefined|T[index] :
T[index]
)
}
);
type RemoveOpt<T> = (
AddUndefined<
{
[index in keyof T]-? : T[index]
},
T
>
)
//This is now [number, string|undefined]
type p = RemoveOpt<Parameters<typeof foo>>; But I shouldn't have to use such a hack =/ I should call it And I should call it |
I might be wrong about this but I think that in general, TS doesn't make a distinction between "optional thing" and "thing that might be undefined" other than for the purpose of checking for existence in object literals. Other than that they treated as equivalent. |
Except, in the very first example, I would expect the Related to that somewhat is that I expect If I really wanted to remove It's not just object literals. Basically just any object type, if it is required but may be undefined, the property must exist. But I guess one can't just break existing functionality... |
This is a duplicate of #31025 unless the code that excludes |
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
Uh oh!
There was an error while loading. Please reload this page.
TypeScript Version: 3.4.1, 3.5.1
Search Terms: mapped tuple type, remove optional, removes undefined
Code
Expected behavior:
Actual behavior:
Playground Link: Playground
Related Issues:
#31025 Related, but somewhat different. This is for mapped tuple types.
The workaround by @jcalz will not work for mapped tuple types
My original intent was to have
[number, string|undefined]
instead of[number, (string|undefined)?]
So, I tried to remove the optional modifier.
But it removed
undefined
as well.So, I tried to add it back. But it refused to take it.
The text was updated successfully, but these errors were encountered: