Revert overly permissive indexed access constraints (#54845)

This commit is contained in:
Anders Hejlsberg 2023-07-26 07:02:40 -07:00 committed by GitHub
parent 04bfd23105
commit cbf3c63ef3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 213 additions and 12 deletions

View File

@ -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.

View File

@ -1,3 +1,19 @@
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'.
@ -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<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'.

View File

@ -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<T extends object>(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<T extends {}, K extends keyof T>(obj: T, methodKey: K): number {
const fn = obj[methodKey];
if (typeof fn !== 'function') {
return 0;
}
return fn.length;
}
// Repro from #54837
function getField<T extends object>(x: T | null, k: keyof T) {
const result = x ? x[k] : null;
return result; // T[keyof T] | null
}

View File

@ -0,0 +1,63 @@
//// [tests/cases/compiler/indexedAccessConstraints.ts] ////
=== indexedAccessConstraints.ts ===
function foo<T extends object>(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<T extends {}, K extends keyof T>(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<T extends object>(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))
}

View File

@ -0,0 +1,59 @@
//// [tests/cases/compiler/indexedAccessConstraints.ts] ////
=== indexedAccessConstraints.ts ===
function foo<T extends object>(a: T[keyof T]) {
>foo : <T extends object>(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<T extends {}, K extends keyof T>(obj: T, methodKey: K): number {
>methodFnLength : <T extends {}, K extends keyof T>(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<T extends object>(x: T | null, k: keyof T) {
>getField : <T extends object>(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
}

View File

@ -0,0 +1,23 @@
// @strict: true
// @noEmit: true
function foo<T extends object>(a: T[keyof T]) {
let b: number = a; // Error
}
// Repro from #54522
export function methodFnLength<T extends {}, K extends keyof T>(obj: T, methodKey: K): number {
const fn = obj[methodKey];
if (typeof fn !== 'function') {
return 0;
}
return fn.length;
}
// Repro from #54837
function getField<T extends object>(x: T | null, k: keyof T) {
const result = x ? x[k] : null;
return result; // T[keyof T] | null
}