Skip to content
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

Improve relationship checking with keyof GenericMappedType as the source type #56246

Conversation

Andarist
Copy link
Contributor

fixes #56239

@typescript-bot typescript-bot added the For Uncommitted Bug PR for untriaged, rejected, closed or missing bug label Oct 28, 2023
@@ -17468,6 +17468,12 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
const modifiersType = getApparentType(getModifiersTypeFromMappedType(type)); // The 'T' in 'keyof T'
forEachMappedTypePropertyKeyTypeAndIndexSignatureKeyType(modifiersType, TypeFlags.StringOrNumberLiteralOrUnique, !!(indexFlags & IndexFlags.StringsOnly), addMemberForKeyType);
}
else if (isFilteringMappedType(type)) {
const modifiersIndex = getIndexType(getModifiersTypeFromMappedType(type), indexFlags, UnionReduction.None);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

UnionReduction.None was added here to keep this assignment allowed.

I think that without it the union reduction was forgetting about 'str' since it's a subtype of string. string is a valid input K but it is meant to be filtered away by DistributiveNonIndex. So with the union reduction on, I have ended up removing 'str' entirely - breaking the test case.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm somewhat unsure - but maybe the union reduction could still mess this up when another union gets created manually with that as one of its members? Maybe I should explore a different fix that would do the same~ instantiation but later on, when comparing some constraints?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

UnionReduction.None is, in some ways, a hack to make contextual typing work a little bit better (and avoid the perf overhead of subtype reduction we don't need to bother doing). The union members could, very easily, evaporate later on down the line.

IMO, it'd be better to leave this case as producing a generic keyof type (especially since this nameType could still just be filtering a homomorphic mapped type), and improve constraint-following logic inside the generic-keyof-over-mapped-type case in structuredTypeRelatedToWorker - specifically, we already have a generic-index-type-target case that does the right thing (tm) - that probably just needs to be brought up and situationally applied when the source is a generic index, too. Since our only special case for keyof on the source side right now is

            else if (sourceFlags & TypeFlags.Index) {
                if (result = isRelatedTo(keyofConstraintType, target, RecursionFlags.Source, reportErrors)) {
                    return result;
                }
            }

it could probably use some more specifics, since it's definitely true that generic keyofs (over mapped types) can have constraints more specific than string | number | symbol.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the advice! I now reused the mentioned logic for keyof GenericMappedType as the target type so now the whole fix is within structuredTypeRelatedToWorker

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hah, I never linked it here, but after I looked over this, I was assigned another issue that tracked back to the same thing, so I ended up writing #56742, which basically scooped this change. Do you want to sync this and at least add the new test cases? In #56742 I used much more specific carve-outs of which keyof types this applies to than the very general check here (and actually use a slightly different source key type).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#56742 didn't fix #56239 , see the nightly playground here. I'll investigate what's the difference between our approaches in the coming days and report back.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason this wasn't fixed is that keyof { [K in keyof TActors as K & string]: { src: K; logic: TActors[K]; }; } wasn't treated as a deferred index type (although I'd argue that it clearly is deferred). So the logic added by #56742 couldn't kick in.

I recently changed that in #60528 and I think that essentially supersedes this PR so I just merged it in there now to keep the tests.

@Andarist Andarist changed the title Instantiate nameType of filtering mapped types when creating index for their mapped types Instantiate nameType of mapped types when creating their index types Oct 28, 2023
@typescript-bot typescript-bot added For Backlog Bug PRs that fix a backlog bug and removed For Uncommitted Bug PR for untriaged, rejected, closed or missing bug labels Nov 17, 2023
@@ -17468,6 +17468,12 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
const modifiersType = getApparentType(getModifiersTypeFromMappedType(type)); // The 'T' in 'keyof T'
forEachMappedTypePropertyKeyTypeAndIndexSignatureKeyType(modifiersType, TypeFlags.StringOrNumberLiteralOrUnique, !!(indexFlags & IndexFlags.StringsOnly), addMemberForKeyType);
}
else if (isFilteringMappedType(type)) {
const modifiersIndex = getIndexType(getModifiersTypeFromMappedType(type), indexFlags, UnionReduction.None);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

UnionReduction.None is, in some ways, a hack to make contextual typing work a little bit better (and avoid the perf overhead of subtype reduction we don't need to bother doing). The union members could, very easily, evaporate later on down the line.

IMO, it'd be better to leave this case as producing a generic keyof type (especially since this nameType could still just be filtering a homomorphic mapped type), and improve constraint-following logic inside the generic-keyof-over-mapped-type case in structuredTypeRelatedToWorker - specifically, we already have a generic-index-type-target case that does the right thing (tm) - that probably just needs to be brought up and situationally applied when the source is a generic index, too. Since our only special case for keyof on the source side right now is

            else if (sourceFlags & TypeFlags.Index) {
                if (result = isRelatedTo(keyofConstraintType, target, RecursionFlags.Source, reportErrors)) {
                    return result;
                }
            }

it could probably use some more specifics, since it's definitely true that generic keyofs (over mapped types) can have constraints more specific than string | number | symbol.

@Andarist Andarist changed the title Instantiate nameType of mapped types when creating their index types Improve relationship checking with keyof GenericMappedType as the source type Dec 28, 2023
Copy link
Member

@weswigham weswigham left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitely conflicts with #56742 😄

@Andarist Andarist force-pushed the instantiate-name-type-of-filtering-mapped-types-for-keyof branch from c618aff to 3d1fe73 Compare November 25, 2024 08:37
@Andarist
Copy link
Contributor Author

Included in #60528

@Andarist Andarist closed this Nov 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
For Backlog Bug PRs that fix a backlog bug
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

keyof FilteringMappedType can't compute its constraint
3 participants