-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Infer intersected reverse mapped types #52062
Closed
Closed
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ef2fba1
Infer intersected reverse mapped types
Andarist 16b6d35
Merge remote-tracking branch 'origin/main' into infer-intersected-map…
Andarist 25af013
Merge remote-tracking branch 'origin/main' into infer-intersected-map…
Andarist 1d1cf87
Merge remote-tracking branch 'origin/main' into infer-intersected-map…
Andarist 53acfd3
Merge remote-tracking branch 'origin/main' into infer-intersected-map…
Andarist 56f12bc
Avoid getting apparent type of intersected mapped types in contextual…
Andarist 294e907
Merge remote-tracking branch 'origin/main' into infer-intersected-map…
Andarist 27805ec
Merge remote-tracking branch 'origin/main' into infer-intersected-map…
Andarist File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
264 changes: 264 additions & 0 deletions
264
tests/baselines/reference/reverseMappedIntersectionInference.symbols
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,264 @@ | ||
=== tests/cases/compiler/reverseMappedIntersectionInference.ts === | ||
type Results<T> = { | ||
>Results : Symbol(Results, Decl(reverseMappedIntersectionInference.ts, 0, 0)) | ||
>T : Symbol(T, Decl(reverseMappedIntersectionInference.ts, 0, 13)) | ||
|
||
[K in keyof T]: { | ||
>K : Symbol(K, Decl(reverseMappedIntersectionInference.ts, 1, 3)) | ||
>T : Symbol(T, Decl(reverseMappedIntersectionInference.ts, 0, 13)) | ||
|
||
data: T[K]; | ||
>data : Symbol(data, Decl(reverseMappedIntersectionInference.ts, 1, 19)) | ||
>T : Symbol(T, Decl(reverseMappedIntersectionInference.ts, 0, 13)) | ||
>K : Symbol(K, Decl(reverseMappedIntersectionInference.ts, 1, 3)) | ||
|
||
onSuccess: (data: T[K]) => void; | ||
>onSuccess : Symbol(onSuccess, Decl(reverseMappedIntersectionInference.ts, 2, 15)) | ||
>data : Symbol(data, Decl(reverseMappedIntersectionInference.ts, 3, 16)) | ||
>T : Symbol(T, Decl(reverseMappedIntersectionInference.ts, 0, 13)) | ||
>K : Symbol(K, Decl(reverseMappedIntersectionInference.ts, 1, 3)) | ||
|
||
}; | ||
}; | ||
|
||
type Errors<E> = { | ||
>Errors : Symbol(Errors, Decl(reverseMappedIntersectionInference.ts, 5, 2)) | ||
>E : Symbol(E, Decl(reverseMappedIntersectionInference.ts, 7, 12)) | ||
|
||
[K in keyof E]: { | ||
>K : Symbol(K, Decl(reverseMappedIntersectionInference.ts, 8, 3)) | ||
>E : Symbol(E, Decl(reverseMappedIntersectionInference.ts, 7, 12)) | ||
|
||
error: E[K]; | ||
>error : Symbol(error, Decl(reverseMappedIntersectionInference.ts, 8, 19)) | ||
>E : Symbol(E, Decl(reverseMappedIntersectionInference.ts, 7, 12)) | ||
>K : Symbol(K, Decl(reverseMappedIntersectionInference.ts, 8, 3)) | ||
|
||
onError: (data: E[K]) => void; | ||
>onError : Symbol(onError, Decl(reverseMappedIntersectionInference.ts, 9, 16)) | ||
>data : Symbol(data, Decl(reverseMappedIntersectionInference.ts, 10, 14)) | ||
>E : Symbol(E, Decl(reverseMappedIntersectionInference.ts, 7, 12)) | ||
>K : Symbol(K, Decl(reverseMappedIntersectionInference.ts, 8, 3)) | ||
|
||
}; | ||
}; | ||
|
||
declare function withKeyedObj<T, E>( | ||
>withKeyedObj : Symbol(withKeyedObj, Decl(reverseMappedIntersectionInference.ts, 12, 2)) | ||
>T : Symbol(T, Decl(reverseMappedIntersectionInference.ts, 14, 30)) | ||
>E : Symbol(E, Decl(reverseMappedIntersectionInference.ts, 14, 32)) | ||
|
||
arg: Results<T> & Errors<E> | ||
>arg : Symbol(arg, Decl(reverseMappedIntersectionInference.ts, 14, 36)) | ||
>Results : Symbol(Results, Decl(reverseMappedIntersectionInference.ts, 0, 0)) | ||
>T : Symbol(T, Decl(reverseMappedIntersectionInference.ts, 14, 30)) | ||
>Errors : Symbol(Errors, Decl(reverseMappedIntersectionInference.ts, 5, 2)) | ||
>E : Symbol(E, Decl(reverseMappedIntersectionInference.ts, 14, 32)) | ||
|
||
): [T, E]; | ||
>T : Symbol(T, Decl(reverseMappedIntersectionInference.ts, 14, 30)) | ||
>E : Symbol(E, Decl(reverseMappedIntersectionInference.ts, 14, 32)) | ||
|
||
const res = withKeyedObj({ | ||
>res : Symbol(res, Decl(reverseMappedIntersectionInference.ts, 18, 5)) | ||
>withKeyedObj : Symbol(withKeyedObj, Decl(reverseMappedIntersectionInference.ts, 12, 2)) | ||
|
||
a: { | ||
>a : Symbol(a, Decl(reverseMappedIntersectionInference.ts, 18, 26)) | ||
|
||
data: "foo", | ||
>data : Symbol(data, Decl(reverseMappedIntersectionInference.ts, 19, 6)) | ||
|
||
onSuccess: (dataArg) => { | ||
>onSuccess : Symbol(onSuccess, Decl(reverseMappedIntersectionInference.ts, 20, 16)) | ||
>dataArg : Symbol(dataArg, Decl(reverseMappedIntersectionInference.ts, 21, 16)) | ||
|
||
dataArg; | ||
>dataArg : Symbol(dataArg, Decl(reverseMappedIntersectionInference.ts, 21, 16)) | ||
|
||
}, | ||
error: 404, | ||
>error : Symbol(error, Decl(reverseMappedIntersectionInference.ts, 23, 6)) | ||
|
||
onError: (errorArg) => { | ||
>onError : Symbol(onError, Decl(reverseMappedIntersectionInference.ts, 24, 15)) | ||
>errorArg : Symbol(errorArg, Decl(reverseMappedIntersectionInference.ts, 25, 14)) | ||
|
||
errorArg; | ||
>errorArg : Symbol(errorArg, Decl(reverseMappedIntersectionInference.ts, 25, 14)) | ||
|
||
}, | ||
}, | ||
b: { | ||
>b : Symbol(b, Decl(reverseMappedIntersectionInference.ts, 28, 4)) | ||
|
||
data: true, | ||
>data : Symbol(data, Decl(reverseMappedIntersectionInference.ts, 29, 6)) | ||
|
||
onSuccess: (dataArg) => { | ||
>onSuccess : Symbol(onSuccess, Decl(reverseMappedIntersectionInference.ts, 30, 15)) | ||
>dataArg : Symbol(dataArg, Decl(reverseMappedIntersectionInference.ts, 31, 16)) | ||
|
||
dataArg; | ||
>dataArg : Symbol(dataArg, Decl(reverseMappedIntersectionInference.ts, 31, 16)) | ||
|
||
}, | ||
error: 500, | ||
>error : Symbol(error, Decl(reverseMappedIntersectionInference.ts, 33, 6)) | ||
|
||
onError: (errorArg) => { | ||
>onError : Symbol(onError, Decl(reverseMappedIntersectionInference.ts, 34, 15)) | ||
>errorArg : Symbol(errorArg, Decl(reverseMappedIntersectionInference.ts, 35, 14)) | ||
|
||
errorArg; | ||
>errorArg : Symbol(errorArg, Decl(reverseMappedIntersectionInference.ts, 35, 14)) | ||
|
||
}, | ||
}, | ||
}); | ||
|
||
declare function withTuples<T extends any[], E extends any[]>( | ||
>withTuples : Symbol(withTuples, Decl(reverseMappedIntersectionInference.ts, 39, 3)) | ||
>T : Symbol(T, Decl(reverseMappedIntersectionInference.ts, 41, 28)) | ||
>E : Symbol(E, Decl(reverseMappedIntersectionInference.ts, 41, 44)) | ||
|
||
arg: [...(Results<T> & Errors<E>)] | ||
>arg : Symbol(arg, Decl(reverseMappedIntersectionInference.ts, 41, 62)) | ||
>Results : Symbol(Results, Decl(reverseMappedIntersectionInference.ts, 0, 0)) | ||
>T : Symbol(T, Decl(reverseMappedIntersectionInference.ts, 41, 28)) | ||
>Errors : Symbol(Errors, Decl(reverseMappedIntersectionInference.ts, 5, 2)) | ||
>E : Symbol(E, Decl(reverseMappedIntersectionInference.ts, 41, 44)) | ||
|
||
): [T, E]; | ||
>T : Symbol(T, Decl(reverseMappedIntersectionInference.ts, 41, 28)) | ||
>E : Symbol(E, Decl(reverseMappedIntersectionInference.ts, 41, 44)) | ||
|
||
const res2 = withTuples([ | ||
>res2 : Symbol(res2, Decl(reverseMappedIntersectionInference.ts, 45, 5)) | ||
>withTuples : Symbol(withTuples, Decl(reverseMappedIntersectionInference.ts, 39, 3)) | ||
{ | ||
data: "foo", | ||
>data : Symbol(data, Decl(reverseMappedIntersectionInference.ts, 46, 3)) | ||
|
||
onSuccess: (dataArg) => { | ||
>onSuccess : Symbol(onSuccess, Decl(reverseMappedIntersectionInference.ts, 47, 16)) | ||
>dataArg : Symbol(dataArg, Decl(reverseMappedIntersectionInference.ts, 48, 16)) | ||
|
||
dataArg; | ||
>dataArg : Symbol(dataArg, Decl(reverseMappedIntersectionInference.ts, 48, 16)) | ||
|
||
}, | ||
error: 404, | ||
>error : Symbol(error, Decl(reverseMappedIntersectionInference.ts, 50, 6)) | ||
|
||
onError: (errorArg) => { | ||
>onError : Symbol(onError, Decl(reverseMappedIntersectionInference.ts, 51, 15)) | ||
>errorArg : Symbol(errorArg, Decl(reverseMappedIntersectionInference.ts, 52, 14)) | ||
|
||
errorArg; | ||
>errorArg : Symbol(errorArg, Decl(reverseMappedIntersectionInference.ts, 52, 14)) | ||
|
||
}, | ||
}, | ||
{ | ||
data: true, | ||
>data : Symbol(data, Decl(reverseMappedIntersectionInference.ts, 56, 3)) | ||
|
||
onSuccess: (dataArg) => { | ||
>onSuccess : Symbol(onSuccess, Decl(reverseMappedIntersectionInference.ts, 57, 15)) | ||
>dataArg : Symbol(dataArg, Decl(reverseMappedIntersectionInference.ts, 58, 16)) | ||
|
||
dataArg; | ||
>dataArg : Symbol(dataArg, Decl(reverseMappedIntersectionInference.ts, 58, 16)) | ||
|
||
}, | ||
error: 500, | ||
>error : Symbol(error, Decl(reverseMappedIntersectionInference.ts, 60, 6)) | ||
|
||
onError: (errorArg) => { | ||
>onError : Symbol(onError, Decl(reverseMappedIntersectionInference.ts, 61, 15)) | ||
>errorArg : Symbol(errorArg, Decl(reverseMappedIntersectionInference.ts, 62, 14)) | ||
|
||
errorArg; | ||
>errorArg : Symbol(errorArg, Decl(reverseMappedIntersectionInference.ts, 62, 14)) | ||
|
||
}, | ||
}, | ||
]); | ||
|
||
type Tuple<T> = readonly [T, ...T[]]; | ||
>Tuple : Symbol(Tuple, Decl(reverseMappedIntersectionInference.ts, 66, 3)) | ||
>T : Symbol(T, Decl(reverseMappedIntersectionInference.ts, 68, 11)) | ||
>T : Symbol(T, Decl(reverseMappedIntersectionInference.ts, 68, 11)) | ||
>T : Symbol(T, Decl(reverseMappedIntersectionInference.ts, 68, 11)) | ||
|
||
declare function withTuplesConstraints<T extends Tuple<any>, E extends Tuple<any>>( | ||
>withTuplesConstraints : Symbol(withTuplesConstraints, Decl(reverseMappedIntersectionInference.ts, 68, 37)) | ||
>T : Symbol(T, Decl(reverseMappedIntersectionInference.ts, 70, 39)) | ||
>Tuple : Symbol(Tuple, Decl(reverseMappedIntersectionInference.ts, 66, 3)) | ||
>E : Symbol(E, Decl(reverseMappedIntersectionInference.ts, 70, 60)) | ||
>Tuple : Symbol(Tuple, Decl(reverseMappedIntersectionInference.ts, 66, 3)) | ||
|
||
arg: Results<T> & Errors<E> | ||
>arg : Symbol(arg, Decl(reverseMappedIntersectionInference.ts, 70, 83)) | ||
>Results : Symbol(Results, Decl(reverseMappedIntersectionInference.ts, 0, 0)) | ||
>T : Symbol(T, Decl(reverseMappedIntersectionInference.ts, 70, 39)) | ||
>Errors : Symbol(Errors, Decl(reverseMappedIntersectionInference.ts, 5, 2)) | ||
>E : Symbol(E, Decl(reverseMappedIntersectionInference.ts, 70, 60)) | ||
|
||
): [T, E]; | ||
>T : Symbol(T, Decl(reverseMappedIntersectionInference.ts, 70, 39)) | ||
>E : Symbol(E, Decl(reverseMappedIntersectionInference.ts, 70, 60)) | ||
|
||
const res3 = withTuplesConstraints([ | ||
>res3 : Symbol(res3, Decl(reverseMappedIntersectionInference.ts, 74, 5)) | ||
>withTuplesConstraints : Symbol(withTuplesConstraints, Decl(reverseMappedIntersectionInference.ts, 68, 37)) | ||
{ | ||
data: "foo", | ||
>data : Symbol(data, Decl(reverseMappedIntersectionInference.ts, 75, 3)) | ||
|
||
onSuccess: (dataArg) => { | ||
>onSuccess : Symbol(onSuccess, Decl(reverseMappedIntersectionInference.ts, 76, 16)) | ||
>dataArg : Symbol(dataArg, Decl(reverseMappedIntersectionInference.ts, 77, 16)) | ||
|
||
dataArg; | ||
>dataArg : Symbol(dataArg, Decl(reverseMappedIntersectionInference.ts, 77, 16)) | ||
|
||
}, | ||
error: 404, | ||
>error : Symbol(error, Decl(reverseMappedIntersectionInference.ts, 79, 6)) | ||
|
||
onError: (errorArg) => { | ||
>onError : Symbol(onError, Decl(reverseMappedIntersectionInference.ts, 80, 15)) | ||
>errorArg : Symbol(errorArg, Decl(reverseMappedIntersectionInference.ts, 81, 14)) | ||
|
||
errorArg; | ||
>errorArg : Symbol(errorArg, Decl(reverseMappedIntersectionInference.ts, 81, 14)) | ||
|
||
}, | ||
}, | ||
{ | ||
data: true, | ||
>data : Symbol(data, Decl(reverseMappedIntersectionInference.ts, 85, 3)) | ||
|
||
onSuccess: (dataArg) => { | ||
>onSuccess : Symbol(onSuccess, Decl(reverseMappedIntersectionInference.ts, 86, 15)) | ||
>dataArg : Symbol(dataArg, Decl(reverseMappedIntersectionInference.ts, 87, 16)) | ||
|
||
dataArg; | ||
>dataArg : Symbol(dataArg, Decl(reverseMappedIntersectionInference.ts, 87, 16)) | ||
|
||
}, | ||
error: 500, | ||
>error : Symbol(error, Decl(reverseMappedIntersectionInference.ts, 89, 6)) | ||
|
||
onError: (errorArg) => { | ||
>onError : Symbol(onError, Decl(reverseMappedIntersectionInference.ts, 90, 15)) | ||
>errorArg : Symbol(errorArg, Decl(reverseMappedIntersectionInference.ts, 91, 14)) | ||
|
||
errorArg; | ||
>errorArg : Symbol(errorArg, Decl(reverseMappedIntersectionInference.ts, 91, 14)) | ||
|
||
}, | ||
}, | ||
]); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, thinking about it, shouldn't we just do this for each
isGenericMappedType(t) && !t.declaration.nameType
within an intersection, and not only do this substitution if every member is such? (And still do the otherStructuredType
behaviors for other intersection members). This way aResults<T> & Errors<E> & {x?: string}
still works.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, so I actually... have another PR that does exactly that 🤣 This one here is intentionally conservative when it comes to the scope of the change, especially since some other changes (outside of
getTypeOfPropertyOfContextualType
) had to be made to support this use case. So it's not like that other PR completely replaces this one.The PR in question is this: #52095 . Actually, one version of it was already merged into 4.8-beta but it was reverted cause
@reduxjs/toolkit
regressed with it. However, it only regressed because of how I've handled intersections of types with concrete properties with types with index signatures.In the original version of this PR (#48668) I decided to prefer concrete properties over index signatures, so (IIRC) if one of the intersected members had a concrete property for the requested one then index signatures on other intersected members were skipped over. This wasn't strictly needed to fix the issue that I was fixing but it was something that made sense to me at the time.
Either way... if you could take a look at that other PR (#52095), I would highly appreciate it. There is some funky stuff going on there with index signatures and that part is not correct - the problem is that I'm not sure how to adjust it since I'm not sure what exact effect are we aiming for there. Mixing index signatures with concrete properties through intersections is weird (like
{ foo: number } & { [key: string]: boolean }
). This particular place in the code doesn't exactly deal with concrete types either... so perhaps a guiding principle should be that properties should be pulled from types that can still resolve to concrete properties? I'm not sure how checking that would look like though - so perhaps we should just ignore it and always pull from index signatures?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mmmmm, I'd rather not take this one with a known drawback like that - maybe it's best to just merge the two PRs, then?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To keep the spirit of minimal changes I would probably prefer to keep them separate (if you prefer to merge them, then I won't oppose it though, at the end of the day I'd like both improvements to find their way into the language :P). I don't mind this one staying on the sidelines and waiting for the other one to land though. I would love for the other one to get some traction in terms of being reviewed 😜
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that the common improvements need to be on both PRs (and the conflicts can be resolved later), or both PRs should be merged, yeah.