-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
RequiredProps<T>: Unexpected result when T = {} #21988
Comments
This means |
@falsandtru I agree. Actually I think Index access notation |
In #11929 (the PR introducing indexed access types) it states:
...and in #8652 (the 'never' PR) it states that "
...and if n=0, then we effectively have an empty union / union of zero types, which is the type having zero values, which is Well that's my reasoning anyway. #11929 doesn't mention |
cc @sandersn didn't we recently change how |
Closing in favour of #22042, which more simply addresses just the underlying issue. |
@weswigham I removed the constraint of T[string], which used to be any for unconstrained T. Maybe we have some more recent changes than that though? |
TypeScript Version: 2.8.0-dev.20180216
Code
Expected behavior:
P0Names = never
andP0Props = {}
Actual behavior:
P0Names = any
andP0Props = {[x: string]: any}
Notes:
@ahejlsberg helpfully came up with the
RequiredProps
andOptionalProps
definitions above from a question I asked in #21919. They work great except whenT = {}
Not 100% sure if this is a bug or not, but it's definitely counterintuitive.
RequiredProps
works for any object type except the empty one, when a string indexer suddenly appears. It certainly breaks the semantic expectations ofRequiredProps
.Workaround:
The following version of
RequiredPropNames
achieves the expected behaviour by singling out the{}
case:The text was updated successfully, but these errors were encountered: