Skip to content

Fix inconsistent generic indexed access #59415

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
26 changes: 9 additions & 17 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29665,15 +29665,15 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {

function isConstraintPosition(type: Type, node: Node) {
const parent = node.parent;
// In an element access obj[x], we consider obj to be in a constraint position, except when obj is of
// a generic type without a nullable constraint and x is a generic type. This is because when both obj
// and x are of generic types T and K, we want the resulting type to be T[K].
// In an element access obj[key], we consider obj to be in a constraint position, except when
// obj and key both have generic types. When obj and key are of generic types T and K, we want
// the resulting type to be T[K].
return parent.kind === SyntaxKind.PropertyAccessExpression ||
parent.kind === SyntaxKind.QualifiedName ||
parent.kind === SyntaxKind.CallExpression && (parent as CallExpression).expression === node ||
parent.kind === SyntaxKind.NewExpression && (parent as NewExpression).expression === node ||
parent.kind === SyntaxKind.ElementAccessExpression && (parent as ElementAccessExpression).expression === node &&
!(someType(type, isGenericTypeWithoutNullableConstraint) && isGenericIndexType(getTypeOfExpression((parent as ElementAccessExpression).argumentExpression)));
!(isGenericObjectType(type) && isGenericIndexType(getTypeOfExpression((parent as ElementAccessExpression).argumentExpression)));
}

function isGenericTypeWithUnionConstraint(type: Type): boolean {
Expand All @@ -29682,12 +29682,6 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
!!(type.flags & TypeFlags.Instantiable && getBaseConstraintOrType(type).flags & (TypeFlags.Nullable | TypeFlags.Union));
}

function isGenericTypeWithoutNullableConstraint(type: Type): boolean {
return type.flags & TypeFlags.Intersection ?
some((type as IntersectionType).types, isGenericTypeWithoutNullableConstraint) :
!!(type.flags & TypeFlags.Instantiable && !maybeTypeOfKind(getBaseConstraintOrType(type), TypeFlags.Nullable));
}

function hasContextualTypeWithNoGenericTypes(node: Node, checkMode: CheckMode | undefined) {
// Computing the contextual type for a child of a JSX element involves resolving the type of the
// element's tag name, so we exclude that here to avoid circularities.
Expand Down Expand Up @@ -34589,14 +34583,12 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}

function checkIndexedAccess(node: ElementAccessExpression, checkMode: CheckMode | undefined): Type {
return node.flags & NodeFlags.OptionalChain ? checkElementAccessChain(node as ElementAccessChain, checkMode) :
checkElementAccessExpression(node, checkNonNullExpression(node.expression), checkMode);
}

function checkElementAccessChain(node: ElementAccessChain, checkMode: CheckMode | undefined) {
const isOptional = node.flags & NodeFlags.OptionalChain;
const exprType = checkExpression(node.expression);
const nonOptionalType = getOptionalExpressionType(exprType, node.expression);
return propagateOptionalTypeMarker(checkElementAccessExpression(node, checkNonNullType(nonOptionalType, node.expression), checkMode), node, nonOptionalType !== exprType);
const nonOptionalType = isOptional ? getOptionalExpressionType(exprType, node.expression) : exprType;
const nonNullType = maybeTypeOfKind(nonOptionalType, TypeFlags.Unknown | TypeFlags.Nullable) ? checkNonNullType(nonOptionalType, node.expression) : nonOptionalType;
const elementType = checkElementAccessExpression(node, nonNullType, checkMode);
return isOptional ? propagateOptionalTypeMarker(elementType, node as ElementAccessChain, nonOptionalType !== exprType) : elementType;
}

function checkElementAccessExpression(node: ElementAccessExpression, exprType: Type, checkMode: CheckMode | undefined): Type {
Expand Down
53 changes: 40 additions & 13 deletions tests/baselines/reference/controlFlowGenericTypes.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ controlFlowGenericTypes.ts(81,11): error TS2339: Property 'foo' does not exist o
controlFlowGenericTypes.ts(90,44): error TS2355: A function whose declared type is neither 'undefined', 'void', nor 'any' must return a value.
controlFlowGenericTypes.ts(91,11): error TS2339: Property 'foo' does not exist on type 'MyUnion'.
Property 'foo' does not exist on type 'AA'.
controlFlowGenericTypes.ts(156,16): error TS18048: 'obj' is possibly 'undefined'.
controlFlowGenericTypes.ts(167,9): error TS18048: 'iSpec' is possibly 'undefined'.
controlFlowGenericTypes.ts(168,9): error TS18048: 'iSpec' is possibly 'undefined'.
controlFlowGenericTypes.ts(176,16): error TS18049: 'obj' is possibly 'null' or 'undefined'.
controlFlowGenericTypes.ts(182,16): error TS18049: 'obj' is possibly 'null' or 'undefined'.
controlFlowGenericTypes.ts(195,9): error TS2536: Type 'keyof PublicSpec' cannot be used to index type 'InternalSpec'.


==== controlFlowGenericTypes.ts (8 errors) ====
Expand Down Expand Up @@ -163,25 +163,54 @@ controlFlowGenericTypes.ts(168,9): error TS18048: 'iSpec' is possibly 'undefined
emittingObject.off(eventName as typeof eventName, 0);
}

// In an element access obj[x], we consider obj to be in a constraint position, except when obj is of
// a generic type without a nullable constraint and x is a generic type. This is because when both obj
// and x are of generic types T and K, we want the resulting type to be T[K].
// In an element access obj[key], we consider obj to be in a constraint position, except when
// obj and key both have generic types. When obj and key are of generic types T and K, we want
// the resulting type to be T[K].

function fx1<T, K extends keyof T>(obj: T, key: K) {
const x1 = obj[key];
const x2 = obj && obj[key];
const x3 = obj?.[key];
}

function fx2<T extends Record<keyof T, string>, K extends keyof T>(obj: T, key: K) {
const x1 = obj[key];
const x2 = obj && obj[key];
const x3 = obj?.[key];
}

function fx3<T extends Record<keyof T, string> | undefined, K extends keyof T>(obj: T, key: K) {
const x1 = obj[key];
const x2 = obj && obj[key];
const x3 = obj?.[key];
}

function fx4<T extends unknown, K extends keyof T>(obj: T, key: K) {
const x1 = obj[key];
const x2 = obj && obj[key];
const x3 = obj?.[key];
}

function fx5<T extends {} | null | undefined, K extends keyof T>(obj: T, key: K) {
const x1 = obj[key];
const x2 = obj && obj[key];
const x3 = obj?.[key];
}

function fx6<T, K extends keyof T>(obj: T | null | undefined, key: K) {
const x1 = obj[key]; // Error
~~~
!!! error TS18048: 'obj' is possibly 'undefined'.
!!! error TS18049: 'obj' is possibly 'null' or 'undefined'.
const x2 = obj && obj[key];
const x3 = obj?.[key];
}

function fx7<T, K extends keyof T>(obj: { x: T } | null | undefined, key: K) {
const x1 = obj.x[key]; // Error
~~~
!!! error TS18049: 'obj' is possibly 'null' or 'undefined'.
const x2 = obj && obj.x[key];
const x3 = obj?.x[key];
}

// Repro from #44166
Expand All @@ -191,12 +220,10 @@ controlFlowGenericTypes.ts(168,9): error TS18048: 'iSpec' is possibly 'undefined
InternalSpec extends Record<keyof PublicSpec, any> | undefined = undefined> {
m() {
let iSpec = null! as InternalSpec;
iSpec[null! as keyof InternalSpec]; // Error, object possibly undefined
~~~~~
!!! error TS18048: 'iSpec' is possibly 'undefined'.
iSpec[null! as keyof PublicSpec]; // Error, object possibly undefined
~~~~~
!!! error TS18048: 'iSpec' is possibly 'undefined'.
iSpec[null! as keyof InternalSpec];
iSpec[null! as keyof PublicSpec]; // Error
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2536: Type 'keyof PublicSpec' cannot be used to index type 'InternalSpec'.
if (iSpec === undefined) {
return;
}
Expand Down
70 changes: 60 additions & 10 deletions tests/baselines/reference/controlFlowGenericTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,23 +141,50 @@ function once<ET, T extends EventEmitter<ET>>(emittingObject: T, eventName: keyo
emittingObject.off(eventName as typeof eventName, 0);
}

// In an element access obj[x], we consider obj to be in a constraint position, except when obj is of
// a generic type without a nullable constraint and x is a generic type. This is because when both obj
// and x are of generic types T and K, we want the resulting type to be T[K].
// In an element access obj[key], we consider obj to be in a constraint position, except when
// obj and key both have generic types. When obj and key are of generic types T and K, we want
// the resulting type to be T[K].

function fx1<T, K extends keyof T>(obj: T, key: K) {
const x1 = obj[key];
const x2 = obj && obj[key];
const x3 = obj?.[key];
}

function fx2<T extends Record<keyof T, string>, K extends keyof T>(obj: T, key: K) {
const x1 = obj[key];
const x2 = obj && obj[key];
const x3 = obj?.[key];
}

function fx3<T extends Record<keyof T, string> | undefined, K extends keyof T>(obj: T, key: K) {
const x1 = obj[key];
const x2 = obj && obj[key];
const x3 = obj?.[key];
}

function fx4<T extends unknown, K extends keyof T>(obj: T, key: K) {
const x1 = obj[key];
const x2 = obj && obj[key];
const x3 = obj?.[key];
}

function fx5<T extends {} | null | undefined, K extends keyof T>(obj: T, key: K) {
const x1 = obj[key];
const x2 = obj && obj[key];
const x3 = obj?.[key];
}

function fx6<T, K extends keyof T>(obj: T | null | undefined, key: K) {
const x1 = obj[key]; // Error
const x2 = obj && obj[key];
const x3 = obj?.[key];
}

function fx7<T, K extends keyof T>(obj: { x: T } | null | undefined, key: K) {
const x1 = obj.x[key]; // Error
const x2 = obj && obj.x[key];
const x3 = obj?.x[key];
}

// Repro from #44166
Expand All @@ -167,8 +194,8 @@ class TableBaseEnum<
InternalSpec extends Record<keyof PublicSpec, any> | undefined = undefined> {
m() {
let iSpec = null! as InternalSpec;
iSpec[null! as keyof InternalSpec]; // Error, object possibly undefined
iSpec[null! as keyof PublicSpec]; // Error, object possibly undefined
iSpec[null! as keyof InternalSpec];
iSpec[null! as keyof PublicSpec]; // Error
if (iSpec === undefined) {
return;
}
Expand Down Expand Up @@ -327,29 +354,52 @@ function once(emittingObject, eventName) {
emittingObject.off(eventName, 0);
emittingObject.off(eventName, 0);
}
// In an element access obj[x], we consider obj to be in a constraint position, except when obj is of
// a generic type without a nullable constraint and x is a generic type. This is because when both obj
// and x are of generic types T and K, we want the resulting type to be T[K].
// In an element access obj[key], we consider obj to be in a constraint position, except when
// obj and key both have generic types. When obj and key are of generic types T and K, we want
// the resulting type to be T[K].
function fx1(obj, key) {
var x1 = obj[key];
var x2 = obj && obj[key];
var x3 = obj === null || obj === void 0 ? void 0 : obj[key];
}
function fx2(obj, key) {
var x1 = obj[key];
var x2 = obj && obj[key];
var x3 = obj === null || obj === void 0 ? void 0 : obj[key];
}
function fx3(obj, key) {
var x1 = obj[key];
var x2 = obj && obj[key];
var x3 = obj === null || obj === void 0 ? void 0 : obj[key];
}
function fx4(obj, key) {
var x1 = obj[key];
var x2 = obj && obj[key];
var x3 = obj === null || obj === void 0 ? void 0 : obj[key];
}
function fx5(obj, key) {
var x1 = obj[key];
var x2 = obj && obj[key];
var x3 = obj === null || obj === void 0 ? void 0 : obj[key];
}
function fx6(obj, key) {
var x1 = obj[key]; // Error
var x2 = obj && obj[key];
var x3 = obj === null || obj === void 0 ? void 0 : obj[key];
}
function fx7(obj, key) {
var x1 = obj.x[key]; // Error
var x2 = obj && obj.x[key];
var x3 = obj === null || obj === void 0 ? void 0 : obj.x[key];
}
// Repro from #44166
var TableBaseEnum = /** @class */ (function () {
function TableBaseEnum() {
}
TableBaseEnum.prototype.m = function () {
var iSpec = null;
iSpec[null]; // Error, object possibly undefined
iSpec[null]; // Error, object possibly undefined
iSpec[null];
iSpec[null]; // Error
if (iSpec === undefined) {
return;
}
Expand Down
Loading