Skip to content

Use lower bound of index types as their constraints when they are applied to indexed access types with non-generic root object type #60091

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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14393,7 +14393,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
setStructuredTypeMembers(type, members, emptyArray, emptyArray, indexInfos);
}

// Return the lower bound of the key type in a mapped type. Intuitively, the lower
// Return the lower bound of the key or index type. Intuitively, the lower
// bound includes those keys that are known to always be present, for example because
// because of constraints on type parameters (e.g. 'keyof T' for a constrained T).
function getLowerBoundOfKeyType(type: Type): Type {
Expand Down Expand Up @@ -14934,11 +14934,11 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}

function getBaseConstraintOfType(type: Type): Type | undefined {
if (type.flags & (TypeFlags.InstantiableNonPrimitive | TypeFlags.UnionOrIntersection | TypeFlags.TemplateLiteral | TypeFlags.StringMapping) || isGenericTupleType(type)) {
const constraint = getResolvedBaseConstraint(type as InstantiableType | UnionOrIntersectionType);
if (type.flags & (TypeFlags.InstantiableNonPrimitive | TypeFlags.UnionOrIntersection | TypeFlags.TemplateLiteral | TypeFlags.StringMapping | TypeFlags.Index) || isGenericTupleType(type)) {
const constraint = getResolvedBaseConstraint(type as InstantiableType | UnionOrIntersectionType | IndexType);
return constraint !== noConstraintType && constraint !== circularConstraintType ? constraint : undefined;
}
return type.flags & TypeFlags.Index ? stringNumberSymbolType : undefined;
return undefined;
}

/**
Expand Down Expand Up @@ -15036,6 +15036,19 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
undefined;
}
if (t.flags & TypeFlags.Index) {
const type = (t as IndexType).type;
if (type.flags & TypeFlags.IndexedAccess) {
let objectType = (type as IndexedAccessType).objectType;
while (objectType.flags & TypeFlags.IndexedAccess) {
objectType = (objectType as IndexedAccessType).objectType;
}
if (!isGenericObjectType(objectType)) {
const consraint = getLowerBoundOfKeyType(t);
if (!(consraint.flags & TypeFlags.Never)) {
return consraint;
}
}
}
return stringNumberSymbolType;
}
if (t.flags & TypeFlags.TemplateLiteral) {
Expand Down Expand Up @@ -23503,7 +23516,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}
else if (sourceFlags & TypeFlags.Index) {
const isDeferredMappedIndex = shouldDeferIndexType((source as IndexType).type, (source as IndexType).indexFlags) && getObjectFlags((source as IndexType).type) & ObjectFlags.Mapped;
if (result = isRelatedTo(stringNumberSymbolType, target, RecursionFlags.Source, reportErrors && !isDeferredMappedIndex)) {
if (result = isRelatedTo(getBaseConstraintOfType(source)!, target, RecursionFlags.Source, reportErrors && !isDeferredMappedIndex)) {
return result;
}
if (isDeferredMappedIndex) {
Expand Down
50 changes: 7 additions & 43 deletions tests/baselines/reference/constraintWithIndexedAccess.errors.txt
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Those date to #52399 and #53059 (that was later reverted by #54845 ). The original intention was for those not to be errors. This is issue was essentially a duplicate of the one I'm fixing here ( #21760 ). So it makes sense that those errors are gone - as long as my proposed fix is correct.

Original file line number Diff line number Diff line change
@@ -1,33 +1,17 @@
constraintWithIndexedAccess.ts(23,90): error TS2344: Type 'TypeHardcodedAsParameterWithoutReturnType<T, F>' does not satisfy the constraint '(...args: any) => any'.
Type 'DataFetchFns[T][keyof DataFetchFns[T]]' is not assignable to type '(...args: any) => any'.
Type 'DataFetchFns[T][string] | DataFetchFns[T][number] | DataFetchFns[T][symbol]' is not assignable to type '(...args: any) => any'.
Type 'DataFetchFns[T][string]' is not assignable to type '(...args: any) => any'.
constraintWithIndexedAccess.ts(24,102): error TS2344: Type 'DataFetchFns[T][F]' does not satisfy the constraint '(...args: any) => any'.
Type 'DataFetchFns[T][keyof DataFetchFns[T]]' is not assignable to type '(...args: any) => any'.
Type 'DataFetchFns[T][string] | DataFetchFns[T][number] | DataFetchFns[T][symbol]' is not assignable to type '(...args: any) => any'.
Type 'DataFetchFns[T][string]' is not assignable to type '(...args: any) => any'.
constraintWithIndexedAccess.ts(26,103): error TS2344: Type 'VehicleSelector<T>[F]' does not satisfy the constraint '(...args: any) => any'.
Type 'VehicleSelector<T>[keyof DataFetchFns[T]]' is not assignable to type '(...args: any) => any'.
Type 'VehicleSelector<T>[string] | VehicleSelector<T>[number] | VehicleSelector<T>[symbol]' is not assignable to type '(...args: any) => any'.
Type 'VehicleSelector<T>[string]' is not assignable to type '(...args: any) => any'.
constraintWithIndexedAccess.ts(27,102): error TS2344: Type 'DataFetchFns[T][F]' does not satisfy the constraint '(...args: any) => any'.
Type 'DataFetchFns[T][keyof DataFetchFns[T]]' is not assignable to type '(...args: any) => any'.
Type 'DataFetchFns[T][string] | DataFetchFns[T][number] | DataFetchFns[T][symbol]' is not assignable to type '(...args: any) => any'.
Type 'DataFetchFns[T][string]' is not assignable to type '(...args: any) => any'.
constraintWithIndexedAccess.ts(28,102): error TS2344: Type 'DataFetchFns[T][T]' does not satisfy the constraint '(...args: any) => any'.
Type 'DataFetchFns[T]["Boat"] | DataFetchFns[T]["Plane"]' is not assignable to type '(...args: any) => any'.
Type 'DataFetchFns[T]["Boat"]' is not assignable to type '(...args: any) => any'.
constraintWithIndexedAccess.ts(28,102): error TS2536: Type 'T' cannot be used to index type 'DataFetchFns[T]'.
constraintWithIndexedAccess.ts(29,102): error TS2536: Type 'F' cannot be used to index type 'DataFetchFns'.
constraintWithIndexedAccess.ts(29,102): error TS2344: Type 'DataFetchFns[F][F]' does not satisfy the constraint '(...args: any) => any'.
Type 'DataFetchFns[F][keyof DataFetchFns[T]]' is not assignable to type '(...args: any) => any'.
Type 'DataFetchFns[F][string] | DataFetchFns[F][number] | DataFetchFns[F][symbol]' is not assignable to type '(...args: any) => any'.
Type 'DataFetchFns[F][string]' is not assignable to type '(...args: any) => any'.
Type 'DataFetchFns[keyof DataFetchFns[T]][string]' is not assignable to type '(...args: any) => any'.
Type 'DataFetchFns[F]["requiresLicense"] | DataFetchFns[F]["maxGroundSpeed"] | DataFetchFns[F]["name"]' is not assignable to type '(...args: any) => any'.
Type 'DataFetchFns[F]["requiresLicense"]' is not assignable to type '(...args: any) => any'.
Type 'DataFetchFns[keyof DataFetchFns[T]]["requiresLicense"]' is not assignable to type '(...args: any) => any'.
constraintWithIndexedAccess.ts(29,102): error TS2536: Type 'F' cannot be used to index type 'DataFetchFns[F]'.


==== constraintWithIndexedAccess.ts (9 errors) ====
==== constraintWithIndexedAccess.ts (5 errors) ====
// #52399
type DataFetchFns = {
Boat: {
Expand All @@ -51,30 +35,10 @@ constraintWithIndexedAccess.ts(29,102): error TS2536: Type 'F' cannot be used to
export type returnTypeOfFunctions = ReturnType<allAreFunctionsAsExpected>; //string | number | boolean as expected
export type SucceedingCombo = ReturnType<TypeHardcodedAsParameterWithoutReturnType<'Boat', keyof DataFetchFns['Boat']>>;
export type FailingCombo<T extends 'Boat', F extends keyof DataFetchFns[T]> = ReturnType<TypeHardcodedAsParameterWithoutReturnType<T,F>>;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2344: Type 'TypeHardcodedAsParameterWithoutReturnType<T, F>' does not satisfy the constraint '(...args: any) => any'.
!!! error TS2344: Type 'DataFetchFns[T][keyof DataFetchFns[T]]' is not assignable to type '(...args: any) => any'.
!!! error TS2344: Type 'DataFetchFns[T][string] | DataFetchFns[T][number] | DataFetchFns[T][symbol]' is not assignable to type '(...args: any) => any'.
!!! error TS2344: Type 'DataFetchFns[T][string]' is not assignable to type '(...args: any) => any'.
export type TypeHardcodedAsParameter<T extends 'Boat', F extends keyof DataFetchFns[T]> = ReturnType<DataFetchFns[T][F]>;
~~~~~~~~~~~~~~~~~~
!!! error TS2344: Type 'DataFetchFns[T][F]' does not satisfy the constraint '(...args: any) => any'.
!!! error TS2344: Type 'DataFetchFns[T][keyof DataFetchFns[T]]' is not assignable to type '(...args: any) => any'.
!!! error TS2344: Type 'DataFetchFns[T][string] | DataFetchFns[T][number] | DataFetchFns[T][symbol]' is not assignable to type '(...args: any) => any'.
!!! error TS2344: Type 'DataFetchFns[T][string]' is not assignable to type '(...args: any) => any'.
type VehicleSelector<T extends keyof DataFetchFns> = DataFetchFns[T];
export type TypeHardcodedAsParameter2<T extends 'Boat', F extends keyof DataFetchFns[T]> = ReturnType<VehicleSelector<T>[F]>;
~~~~~~~~~~~~~~~~~~~~~
!!! error TS2344: Type 'VehicleSelector<T>[F]' does not satisfy the constraint '(...args: any) => any'.
!!! error TS2344: Type 'VehicleSelector<T>[keyof DataFetchFns[T]]' is not assignable to type '(...args: any) => any'.
!!! error TS2344: Type 'VehicleSelector<T>[string] | VehicleSelector<T>[number] | VehicleSelector<T>[symbol]' is not assignable to type '(...args: any) => any'.
!!! error TS2344: Type 'VehicleSelector<T>[string]' is not assignable to type '(...args: any) => any'.
export type TypeGeneric1<T extends keyof DataFetchFns, F extends keyof DataFetchFns[T]> = ReturnType<DataFetchFns[T][F]>;
~~~~~~~~~~~~~~~~~~
!!! error TS2344: Type 'DataFetchFns[T][F]' does not satisfy the constraint '(...args: any) => any'.
!!! error TS2344: Type 'DataFetchFns[T][keyof DataFetchFns[T]]' is not assignable to type '(...args: any) => any'.
!!! error TS2344: Type 'DataFetchFns[T][string] | DataFetchFns[T][number] | DataFetchFns[T][symbol]' is not assignable to type '(...args: any) => any'.
!!! error TS2344: Type 'DataFetchFns[T][string]' is not assignable to type '(...args: any) => any'.
export type TypeGeneric2<T extends keyof DataFetchFns, F extends keyof DataFetchFns[T]> = ReturnType<DataFetchFns[T][T]>; // error
~~~~~~~~~~~~~~~~~~
!!! error TS2344: Type 'DataFetchFns[T][T]' does not satisfy the constraint '(...args: any) => any'.
Expand All @@ -88,9 +52,9 @@ constraintWithIndexedAccess.ts(29,102): error TS2536: Type 'F' cannot be used to
~~~~~~~~~~~~~~~~~~
!!! error TS2344: Type 'DataFetchFns[F][F]' does not satisfy the constraint '(...args: any) => any'.
!!! error TS2344: Type 'DataFetchFns[F][keyof DataFetchFns[T]]' is not assignable to type '(...args: any) => any'.
!!! error TS2344: Type 'DataFetchFns[F][string] | DataFetchFns[F][number] | DataFetchFns[F][symbol]' is not assignable to type '(...args: any) => any'.
!!! error TS2344: Type 'DataFetchFns[F][string]' is not assignable to type '(...args: any) => any'.
!!! error TS2344: Type 'DataFetchFns[keyof DataFetchFns[T]][string]' is not assignable to type '(...args: any) => any'.
!!! error TS2344: Type 'DataFetchFns[F]["requiresLicense"] | DataFetchFns[F]["maxGroundSpeed"] | DataFetchFns[F]["name"]' is not assignable to type '(...args: any) => any'.
!!! error TS2344: Type 'DataFetchFns[F]["requiresLicense"]' is not assignable to type '(...args: any) => any'.
!!! error TS2344: Type 'DataFetchFns[keyof DataFetchFns[T]]["requiresLicense"]' is not assignable to type '(...args: any) => any'.
~~~~~~~~~~~~~~~~~~
!!! error TS2536: Type 'F' cannot be used to index type 'DataFetchFns[F]'.

Loading