From cbf3c63ef3bb85e235092eaf5aa6035dad04b185 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Wed, 26 Jul 2023 07:02:40 -0700 Subject: [PATCH] Revert overly permissive indexed access constraints (#54845) --- src/compiler/checker.ts | 11 ---- .../constraintWithIndexedAccess.errors.txt | 38 ++++++++++- .../indexedAccessConstraints.errors.txt | 31 +++++++++ .../indexedAccessConstraints.symbols | 63 +++++++++++++++++++ .../reference/indexedAccessConstraints.types | 59 +++++++++++++++++ .../compiler/indexedAccessConstraints.ts | 23 +++++++ 6 files changed, 213 insertions(+), 12 deletions(-) create mode 100644 tests/baselines/reference/indexedAccessConstraints.errors.txt create mode 100644 tests/baselines/reference/indexedAccessConstraints.symbols create mode 100644 tests/baselines/reference/indexedAccessConstraints.types create mode 100644 tests/cases/compiler/indexedAccessConstraints.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 8af1cc2e36d..a21ea466825 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -21714,17 +21714,6 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { else if (result = isRelatedTo(getTypeWithThisArgument(constraint, source), target, RecursionFlags.Source, reportErrors && constraint !== unknownType && !(targetFlags & sourceFlags & TypeFlags.TypeParameter), /*headMessage*/ undefined, intersectionState)) { return result; } - if (sourceFlags & TypeFlags.IndexedAccess) { - const indexType = (source as IndexedAccessType).indexType; - if (indexType.flags & TypeFlags.Index) { - const unresolvedIndexConstraint = getBaseConstraintOfType((indexType as IndexType).type); - const indexConstraint = unresolvedIndexConstraint && unresolvedIndexConstraint !== noConstraintType ? getIndexType(unresolvedIndexConstraint) : keyofConstraintType; - const constraint = getIndexedAccessType((source as IndexedAccessType).objectType, indexConstraint); - if (result = isRelatedTo(constraint, target, RecursionFlags.Source, /*reportErrors*/ false, /*headMessage*/ undefined, intersectionState)) { - return result; - } - } - } if (isMappedTypeGenericIndexedAccess(source)) { // For an indexed access type { [P in K]: E}[X], above we have already explored an instantiation of E with X // substituted for P. We also want to explore type { [P in K]: E }[C], where C is the constraint of X. diff --git a/tests/baselines/reference/constraintWithIndexedAccess.errors.txt b/tests/baselines/reference/constraintWithIndexedAccess.errors.txt index d2d495db176..11641a24b4f 100644 --- a/tests/baselines/reference/constraintWithIndexedAccess.errors.txt +++ b/tests/baselines/reference/constraintWithIndexedAccess.errors.txt @@ -1,3 +1,19 @@ +constraintWithIndexedAccess.ts(23,90): error TS2344: Type 'TypeHardcodedAsParameterWithoutReturnType' 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[F]' does not satisfy the constraint '(...args: any) => any'. + Type 'VehicleSelector[keyof DataFetchFns[T]]' is not assignable to type '(...args: any) => any'. + Type 'VehicleSelector[string] | VehicleSelector[number] | VehicleSelector[symbol]' is not assignable to type '(...args: any) => any'. + Type 'VehicleSelector[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'. @@ -11,7 +27,7 @@ constraintWithIndexedAccess.ts(29,102): error TS2344: Type 'DataFetchFns[F][F]' constraintWithIndexedAccess.ts(29,102): error TS2536: Type 'F' cannot be used to index type 'DataFetchFns[F]'. -==== constraintWithIndexedAccess.ts (5 errors) ==== +==== constraintWithIndexedAccess.ts (9 errors) ==== // #52399 type DataFetchFns = { Boat: { @@ -35,10 +51,30 @@ constraintWithIndexedAccess.ts(29,102): error TS2536: Type 'F' cannot be used to export type returnTypeOfFunctions = ReturnType; //string | number | boolean as expected export type SucceedingCombo = ReturnType>; export type FailingCombo = ReturnType>; + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2344: Type 'TypeHardcodedAsParameterWithoutReturnType' 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 = ReturnType; + ~~~~~~~~~~~~~~~~~~ +!!! 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 = DataFetchFns[T]; export type TypeHardcodedAsParameter2 = ReturnType[F]>; + ~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2344: Type 'VehicleSelector[F]' does not satisfy the constraint '(...args: any) => any'. +!!! error TS2344: Type 'VehicleSelector[keyof DataFetchFns[T]]' is not assignable to type '(...args: any) => any'. +!!! error TS2344: Type 'VehicleSelector[string] | VehicleSelector[number] | VehicleSelector[symbol]' is not assignable to type '(...args: any) => any'. +!!! error TS2344: Type 'VehicleSelector[string]' is not assignable to type '(...args: any) => any'. export type TypeGeneric1 = ReturnType; + ~~~~~~~~~~~~~~~~~~ +!!! 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 = ReturnType; // error ~~~~~~~~~~~~~~~~~~ !!! error TS2344: Type 'DataFetchFns[T][T]' does not satisfy the constraint '(...args: any) => any'. diff --git a/tests/baselines/reference/indexedAccessConstraints.errors.txt b/tests/baselines/reference/indexedAccessConstraints.errors.txt new file mode 100644 index 00000000000..02b87e2cad3 --- /dev/null +++ b/tests/baselines/reference/indexedAccessConstraints.errors.txt @@ -0,0 +1,31 @@ +indexedAccessConstraints.ts(2,9): error TS2322: Type 'T[keyof T]' is not assignable to type 'number'. + Type 'T[string] | T[number] | T[symbol]' is not assignable to type 'number'. + Type 'T[string]' is not assignable to type 'number'. + + +==== indexedAccessConstraints.ts (1 errors) ==== + function foo(a: T[keyof T]) { + let b: number = a; // Error + ~ +!!! error TS2322: Type 'T[keyof T]' is not assignable to type 'number'. +!!! error TS2322: Type 'T[string] | T[number] | T[symbol]' is not assignable to type 'number'. +!!! error TS2322: Type 'T[string]' is not assignable to type 'number'. + } + + // Repro from #54522 + + export function methodFnLength(obj: T, methodKey: K): number { + const fn = obj[methodKey]; + if (typeof fn !== 'function') { + return 0; + } + return fn.length; + } + + // Repro from #54837 + + function getField(x: T | null, k: keyof T) { + const result = x ? x[k] : null; + return result; // T[keyof T] | null + } + \ No newline at end of file diff --git a/tests/baselines/reference/indexedAccessConstraints.symbols b/tests/baselines/reference/indexedAccessConstraints.symbols new file mode 100644 index 00000000000..1f308726343 --- /dev/null +++ b/tests/baselines/reference/indexedAccessConstraints.symbols @@ -0,0 +1,63 @@ +//// [tests/cases/compiler/indexedAccessConstraints.ts] //// + +=== indexedAccessConstraints.ts === +function foo(a: T[keyof T]) { +>foo : Symbol(foo, Decl(indexedAccessConstraints.ts, 0, 0)) +>T : Symbol(T, Decl(indexedAccessConstraints.ts, 0, 13)) +>a : Symbol(a, Decl(indexedAccessConstraints.ts, 0, 31)) +>T : Symbol(T, Decl(indexedAccessConstraints.ts, 0, 13)) +>T : Symbol(T, Decl(indexedAccessConstraints.ts, 0, 13)) + + let b: number = a; // Error +>b : Symbol(b, Decl(indexedAccessConstraints.ts, 1, 7)) +>a : Symbol(a, Decl(indexedAccessConstraints.ts, 0, 31)) +} + +// Repro from #54522 + +export function methodFnLength(obj: T, methodKey: K): number { +>methodFnLength : Symbol(methodFnLength, Decl(indexedAccessConstraints.ts, 2, 1)) +>T : Symbol(T, Decl(indexedAccessConstraints.ts, 6, 31)) +>K : Symbol(K, Decl(indexedAccessConstraints.ts, 6, 44)) +>T : Symbol(T, Decl(indexedAccessConstraints.ts, 6, 31)) +>obj : Symbol(obj, Decl(indexedAccessConstraints.ts, 6, 64)) +>T : Symbol(T, Decl(indexedAccessConstraints.ts, 6, 31)) +>methodKey : Symbol(methodKey, Decl(indexedAccessConstraints.ts, 6, 71)) +>K : Symbol(K, Decl(indexedAccessConstraints.ts, 6, 44)) + + const fn = obj[methodKey]; +>fn : Symbol(fn, Decl(indexedAccessConstraints.ts, 7, 9)) +>obj : Symbol(obj, Decl(indexedAccessConstraints.ts, 6, 64)) +>methodKey : Symbol(methodKey, Decl(indexedAccessConstraints.ts, 6, 71)) + + if (typeof fn !== 'function') { +>fn : Symbol(fn, Decl(indexedAccessConstraints.ts, 7, 9)) + + return 0; + } + return fn.length; +>fn.length : Symbol(Function.length, Decl(lib.es5.d.ts, --, --)) +>fn : Symbol(fn, Decl(indexedAccessConstraints.ts, 7, 9)) +>length : Symbol(Function.length, Decl(lib.es5.d.ts, --, --)) +} + +// Repro from #54837 + +function getField(x: T | null, k: keyof T) { +>getField : Symbol(getField, Decl(indexedAccessConstraints.ts, 12, 1)) +>T : Symbol(T, Decl(indexedAccessConstraints.ts, 16, 18)) +>x : Symbol(x, Decl(indexedAccessConstraints.ts, 16, 36)) +>T : Symbol(T, Decl(indexedAccessConstraints.ts, 16, 18)) +>k : Symbol(k, Decl(indexedAccessConstraints.ts, 16, 48)) +>T : Symbol(T, Decl(indexedAccessConstraints.ts, 16, 18)) + + const result = x ? x[k] : null; +>result : Symbol(result, Decl(indexedAccessConstraints.ts, 17, 9)) +>x : Symbol(x, Decl(indexedAccessConstraints.ts, 16, 36)) +>x : Symbol(x, Decl(indexedAccessConstraints.ts, 16, 36)) +>k : Symbol(k, Decl(indexedAccessConstraints.ts, 16, 48)) + + return result; // T[keyof T] | null +>result : Symbol(result, Decl(indexedAccessConstraints.ts, 17, 9)) +} + diff --git a/tests/baselines/reference/indexedAccessConstraints.types b/tests/baselines/reference/indexedAccessConstraints.types new file mode 100644 index 00000000000..3fc2626344e --- /dev/null +++ b/tests/baselines/reference/indexedAccessConstraints.types @@ -0,0 +1,59 @@ +//// [tests/cases/compiler/indexedAccessConstraints.ts] //// + +=== indexedAccessConstraints.ts === +function foo(a: T[keyof T]) { +>foo : (a: T[keyof T]) => void +>a : T[keyof T] + + let b: number = a; // Error +>b : number +>a : T[keyof T] +} + +// Repro from #54522 + +export function methodFnLength(obj: T, methodKey: K): number { +>methodFnLength : (obj: T, methodKey: K) => number +>obj : T +>methodKey : K + + const fn = obj[methodKey]; +>fn : T[K] +>obj[methodKey] : T[K] +>obj : T +>methodKey : K + + if (typeof fn !== 'function') { +>typeof fn !== 'function' : boolean +>typeof fn : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function" +>fn : T[K] +>'function' : "function" + + return 0; +>0 : 0 + } + return fn.length; +>fn.length : number +>fn : T[K] & Function +>length : number +} + +// Repro from #54837 + +function getField(x: T | null, k: keyof T) { +>getField : (x: T | null, k: keyof T) => T[keyof T] | null +>x : T | null +>k : keyof T + + const result = x ? x[k] : null; +>result : T[keyof T] | null +>x ? x[k] : null : T[keyof T] | null +>x : T | null +>x[k] : T[keyof T] +>x : T +>k : keyof T + + return result; // T[keyof T] | null +>result : T[keyof T] | null +} + diff --git a/tests/cases/compiler/indexedAccessConstraints.ts b/tests/cases/compiler/indexedAccessConstraints.ts new file mode 100644 index 00000000000..5331b6b7126 --- /dev/null +++ b/tests/cases/compiler/indexedAccessConstraints.ts @@ -0,0 +1,23 @@ +// @strict: true +// @noEmit: true + +function foo(a: T[keyof T]) { + let b: number = a; // Error +} + +// Repro from #54522 + +export function methodFnLength(obj: T, methodKey: K): number { + const fn = obj[methodKey]; + if (typeof fn !== 'function') { + return 0; + } + return fn.length; +} + +// Repro from #54837 + +function getField(x: T | null, k: keyof T) { + const result = x ? x[k] : null; + return result; // T[keyof T] | null +}