-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Fixed types of properties of contextual filtering mapped types #56201
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
Merged
andrewbranch
merged 2 commits into
microsoft:main
from
Andarist:fix/type-of-prop-of-contextual-filtering-mapped-type
Aug 8, 2024
Merged
Changes from all commits
Commits
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 hidden or 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
90 changes: 90 additions & 0 deletions
90
tests/baselines/reference/contextualPropertyOfGenericFilteringMappedType.symbols
This file contains hidden or 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,90 @@ | ||
//// [tests/cases/compiler/contextualPropertyOfGenericFilteringMappedType.ts] //// | ||
|
||
=== contextualPropertyOfGenericFilteringMappedType.ts === | ||
declare function f1<T extends object>( | ||
>f1 : Symbol(f1, Decl(contextualPropertyOfGenericFilteringMappedType.ts, 0, 0)) | ||
>T : Symbol(T, Decl(contextualPropertyOfGenericFilteringMappedType.ts, 0, 20)) | ||
|
||
data: T, | ||
>data : Symbol(data, Decl(contextualPropertyOfGenericFilteringMappedType.ts, 0, 38)) | ||
>T : Symbol(T, Decl(contextualPropertyOfGenericFilteringMappedType.ts, 0, 20)) | ||
|
||
handlers: { [P in keyof T as P]: (value: T[P], prop: P) => void }, | ||
>handlers : Symbol(handlers, Decl(contextualPropertyOfGenericFilteringMappedType.ts, 1, 10)) | ||
>P : Symbol(P, Decl(contextualPropertyOfGenericFilteringMappedType.ts, 2, 15)) | ||
>T : Symbol(T, Decl(contextualPropertyOfGenericFilteringMappedType.ts, 0, 20)) | ||
>P : Symbol(P, Decl(contextualPropertyOfGenericFilteringMappedType.ts, 2, 15)) | ||
>value : Symbol(value, Decl(contextualPropertyOfGenericFilteringMappedType.ts, 2, 36)) | ||
>T : Symbol(T, Decl(contextualPropertyOfGenericFilteringMappedType.ts, 0, 20)) | ||
>P : Symbol(P, Decl(contextualPropertyOfGenericFilteringMappedType.ts, 2, 15)) | ||
>prop : Symbol(prop, Decl(contextualPropertyOfGenericFilteringMappedType.ts, 2, 48)) | ||
>P : Symbol(P, Decl(contextualPropertyOfGenericFilteringMappedType.ts, 2, 15)) | ||
|
||
): void; | ||
|
||
f1( | ||
>f1 : Symbol(f1, Decl(contextualPropertyOfGenericFilteringMappedType.ts, 0, 0)) | ||
{ | ||
foo: 0, | ||
>foo : Symbol(foo, Decl(contextualPropertyOfGenericFilteringMappedType.ts, 6, 3)) | ||
|
||
bar: "", | ||
>bar : Symbol(bar, Decl(contextualPropertyOfGenericFilteringMappedType.ts, 7, 11)) | ||
|
||
}, | ||
{ | ||
foo: (value, key) => {}, | ||
>foo : Symbol(foo, Decl(contextualPropertyOfGenericFilteringMappedType.ts, 10, 3)) | ||
>value : Symbol(value, Decl(contextualPropertyOfGenericFilteringMappedType.ts, 11, 10)) | ||
>key : Symbol(key, Decl(contextualPropertyOfGenericFilteringMappedType.ts, 11, 16)) | ||
|
||
bar: (value, key) => {}, | ||
>bar : Symbol(bar, Decl(contextualPropertyOfGenericFilteringMappedType.ts, 11, 28)) | ||
>value : Symbol(value, Decl(contextualPropertyOfGenericFilteringMappedType.ts, 12, 10)) | ||
>key : Symbol(key, Decl(contextualPropertyOfGenericFilteringMappedType.ts, 12, 16)) | ||
|
||
}, | ||
); | ||
|
||
declare function f2<T extends object>( | ||
>f2 : Symbol(f2, Decl(contextualPropertyOfGenericFilteringMappedType.ts, 14, 2)) | ||
>T : Symbol(T, Decl(contextualPropertyOfGenericFilteringMappedType.ts, 16, 20)) | ||
|
||
data: T, | ||
>data : Symbol(data, Decl(contextualPropertyOfGenericFilteringMappedType.ts, 16, 38)) | ||
>T : Symbol(T, Decl(contextualPropertyOfGenericFilteringMappedType.ts, 16, 20)) | ||
|
||
handlers: { [P in keyof T as T[P] extends string ? P : never]: (value: T[P], prop: P) => void }, | ||
>handlers : Symbol(handlers, Decl(contextualPropertyOfGenericFilteringMappedType.ts, 17, 10)) | ||
>P : Symbol(P, Decl(contextualPropertyOfGenericFilteringMappedType.ts, 18, 15)) | ||
>T : Symbol(T, Decl(contextualPropertyOfGenericFilteringMappedType.ts, 16, 20)) | ||
>T : Symbol(T, Decl(contextualPropertyOfGenericFilteringMappedType.ts, 16, 20)) | ||
>P : Symbol(P, Decl(contextualPropertyOfGenericFilteringMappedType.ts, 18, 15)) | ||
>P : Symbol(P, Decl(contextualPropertyOfGenericFilteringMappedType.ts, 18, 15)) | ||
>value : Symbol(value, Decl(contextualPropertyOfGenericFilteringMappedType.ts, 18, 66)) | ||
>T : Symbol(T, Decl(contextualPropertyOfGenericFilteringMappedType.ts, 16, 20)) | ||
>P : Symbol(P, Decl(contextualPropertyOfGenericFilteringMappedType.ts, 18, 15)) | ||
>prop : Symbol(prop, Decl(contextualPropertyOfGenericFilteringMappedType.ts, 18, 78)) | ||
>P : Symbol(P, Decl(contextualPropertyOfGenericFilteringMappedType.ts, 18, 15)) | ||
|
||
): void; | ||
|
||
f2( | ||
>f2 : Symbol(f2, Decl(contextualPropertyOfGenericFilteringMappedType.ts, 14, 2)) | ||
{ | ||
foo: 0, | ||
>foo : Symbol(foo, Decl(contextualPropertyOfGenericFilteringMappedType.ts, 22, 3)) | ||
|
||
bar: "", | ||
>bar : Symbol(bar, Decl(contextualPropertyOfGenericFilteringMappedType.ts, 23, 11)) | ||
|
||
}, | ||
{ | ||
bar: (value, key) => {}, | ||
>bar : Symbol(bar, Decl(contextualPropertyOfGenericFilteringMappedType.ts, 26, 3)) | ||
>value : Symbol(value, Decl(contextualPropertyOfGenericFilteringMappedType.ts, 27, 10)) | ||
>key : Symbol(key, Decl(contextualPropertyOfGenericFilteringMappedType.ts, 27, 16)) | ||
|
||
}, | ||
); | ||
|
127 changes: 127 additions & 0 deletions
127
tests/baselines/reference/contextualPropertyOfGenericFilteringMappedType.types
This file contains hidden or 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,127 @@ | ||
//// [tests/cases/compiler/contextualPropertyOfGenericFilteringMappedType.ts] //// | ||
|
||
=== contextualPropertyOfGenericFilteringMappedType.ts === | ||
declare function f1<T extends object>( | ||
>f1 : <T extends object>(data: T, handlers: { [P in keyof T as P]: (value: T[P], prop: P) => void; }) => void | ||
> : ^ ^^^^^^^^^ ^^ ^^ ^^ ^^ ^^^^^ | ||
|
||
data: T, | ||
>data : T | ||
> : ^ | ||
|
||
handlers: { [P in keyof T as P]: (value: T[P], prop: P) => void }, | ||
>handlers : { [P in keyof T as P]: (value: T[P], prop: P) => void; } | ||
> : ^^^ ^^^^^^^^^^^^^^^^^^^^ ^^ ^^ ^^ ^^^^^ ^^^ | ||
>value : T[P] | ||
> : ^^^^ | ||
>prop : P | ||
> : ^ | ||
|
||
): void; | ||
|
||
f1( | ||
>f1( { foo: 0, bar: "", }, { foo: (value, key) => {}, bar: (value, key) => {}, },) : void | ||
> : ^^^^ | ||
>f1 : <T extends object>(data: T, handlers: { [P in keyof T as P]: (value: T[P], prop: P) => void; }) => void | ||
> : ^ ^^^^^^^^^ ^^ ^^ ^^ ^^ ^^^^^ | ||
{ | ||
>{ foo: 0, bar: "", } : { foo: number; bar: string; } | ||
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
foo: 0, | ||
>foo : number | ||
> : ^^^^^^ | ||
>0 : 0 | ||
> : ^ | ||
|
||
bar: "", | ||
>bar : string | ||
> : ^^^^^^ | ||
>"" : "" | ||
> : ^^ | ||
|
||
}, | ||
{ | ||
>{ foo: (value, key) => {}, bar: (value, key) => {}, } : { foo: (value: number, key: "foo") => void; bar: (value: string, key: "bar") => void; } | ||
> : ^^^^^^^^ ^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^ | ||
|
||
foo: (value, key) => {}, | ||
>foo : (value: number, key: "foo") => void | ||
> : ^ ^^^^^^^^^^ ^^^^^^^^^^^^^^^^ | ||
>(value, key) => {} : (value: number, key: "foo") => void | ||
> : ^ ^^^^^^^^^^ ^^^^^^^^^^^^^^^^ | ||
>value : number | ||
> : ^^^^^^ | ||
>key : "foo" | ||
> : ^^^^^ | ||
|
||
bar: (value, key) => {}, | ||
>bar : (value: string, key: "bar") => void | ||
> : ^ ^^^^^^^^^^ ^^^^^^^^^^^^^^^^ | ||
>(value, key) => {} : (value: string, key: "bar") => void | ||
> : ^ ^^^^^^^^^^ ^^^^^^^^^^^^^^^^ | ||
>value : string | ||
> : ^^^^^^ | ||
>key : "bar" | ||
> : ^^^^^ | ||
|
||
}, | ||
); | ||
|
||
declare function f2<T extends object>( | ||
>f2 : <T extends object>(data: T, handlers: { [P in keyof T as T[P] extends string ? P : never]: (value: T[P], prop: P) => void; }) => void | ||
> : ^ ^^^^^^^^^ ^^ ^^ ^^ ^^ ^^^^^ | ||
|
||
data: T, | ||
>data : T | ||
> : ^ | ||
|
||
handlers: { [P in keyof T as T[P] extends string ? P : never]: (value: T[P], prop: P) => void }, | ||
>handlers : { [P in keyof T as T[P] extends string ? P : never]: (value: T[P], prop: P) => void; } | ||
> : ^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^ ^^ ^^ ^^^^^ ^^^ | ||
>value : T[P] | ||
> : ^^^^ | ||
>prop : P | ||
> : ^ | ||
|
||
): void; | ||
|
||
f2( | ||
>f2( { foo: 0, bar: "", }, { bar: (value, key) => {}, },) : void | ||
> : ^^^^ | ||
>f2 : <T extends object>(data: T, handlers: { [P in keyof T as T[P] extends string ? P : never]: (value: T[P], prop: P) => void; }) => void | ||
> : ^ ^^^^^^^^^ ^^ ^^ ^^ ^^ ^^^^^ | ||
{ | ||
>{ foo: 0, bar: "", } : { foo: number; bar: string; } | ||
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
foo: 0, | ||
>foo : number | ||
> : ^^^^^^ | ||
>0 : 0 | ||
> : ^ | ||
|
||
bar: "", | ||
>bar : string | ||
> : ^^^^^^ | ||
>"" : "" | ||
> : ^^ | ||
|
||
}, | ||
{ | ||
>{ bar: (value, key) => {}, } : { bar: (value: string, key: "bar") => void; } | ||
> : ^^^^^^^^ ^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^ | ||
|
||
bar: (value, key) => {}, | ||
>bar : (value: string, key: "bar") => void | ||
> : ^ ^^^^^^^^^^ ^^^^^^^^^^^^^^^^ | ||
>(value, key) => {} : (value: string, key: "bar") => void | ||
> : ^ ^^^^^^^^^^ ^^^^^^^^^^^^^^^^ | ||
>value : string | ||
> : ^^^^^^ | ||
>key : "bar" | ||
> : ^^^^^ | ||
|
||
}, | ||
); | ||
|
33 changes: 33 additions & 0 deletions
33
tests/cases/compiler/contextualPropertyOfGenericFilteringMappedType.ts
This file contains hidden or 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,33 @@ | ||
// @strict: true | ||
// @noEmit: true | ||
|
||
declare function f1<T extends object>( | ||
data: T, | ||
handlers: { [P in keyof T as P]: (value: T[P], prop: P) => void }, | ||
): void; | ||
|
||
f1( | ||
{ | ||
foo: 0, | ||
bar: "", | ||
}, | ||
{ | ||
foo: (value, key) => {}, | ||
bar: (value, key) => {}, | ||
}, | ||
); | ||
|
||
declare function f2<T extends object>( | ||
data: T, | ||
handlers: { [P in keyof T as T[P] extends string ? P : never]: (value: T[P], prop: P) => void }, | ||
): void; | ||
|
||
f2( | ||
{ | ||
foo: 0, | ||
bar: "", | ||
}, | ||
{ | ||
bar: (value, key) => {}, | ||
}, | ||
); |
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.
Question: Is there any reason you're using the
extends string
here instead ofextends PropertyKey
?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.
it's meant to actually filter something, so I intentionally filter
T[P]
that match some subset of potential types. Note that I'm checking againstT[P]
and not justP
- but even if the check would be againstP
, I could still want to filter the keys soPropertyKey
isn't what I'd use here