-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Fix noUncheckedIndexedAccess with tuple rest types and generic index types #40681
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
Changes from 2 commits
d5204a7
4b21647
54705e5
c91bc93
0b95cc6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -180,15 +180,6 @@ namespace ts { | |
IsForSignatureHelp = 1 << 4, // Call resolution for purposes of signature help | ||
} | ||
|
||
const enum AccessFlags { | ||
None = 0, | ||
NoIndexSignatures = 1 << 0, | ||
Writing = 1 << 1, | ||
CacheSymbol = 1 << 2, | ||
NoTupleBoundsCheck = 1 << 3, | ||
ExpressionPosition = 1 << 4, | ||
} | ||
|
||
const enum SignatureCheckMode { | ||
BivariantCallback = 1 << 0, | ||
StrictCallback = 1 << 1, | ||
|
@@ -10744,14 +10735,14 @@ namespace ts { | |
function getConstraintFromIndexedAccess(type: IndexedAccessType) { | ||
const indexConstraint = getSimplifiedTypeOrConstraint(type.indexType); | ||
if (indexConstraint && indexConstraint !== type.indexType) { | ||
const indexedAccess = getIndexedAccessTypeOrUndefined(type.objectType, indexConstraint); | ||
const indexedAccess = getIndexedAccessTypeOrUndefined(type.objectType, indexConstraint, /*accessNode*/ undefined, type.accessFlags); | ||
if (indexedAccess) { | ||
return indexedAccess; | ||
} | ||
} | ||
const objectConstraint = getSimplifiedTypeOrConstraint(type.objectType); | ||
if (objectConstraint && objectConstraint !== type.objectType) { | ||
return getIndexedAccessTypeOrUndefined(objectConstraint, type.indexType); | ||
return getIndexedAccessTypeOrUndefined(objectConstraint, type.indexType, /*accessNode*/ undefined, type.accessFlags); | ||
} | ||
return undefined; | ||
} | ||
|
@@ -10949,7 +10940,7 @@ namespace ts { | |
if (t.flags & TypeFlags.IndexedAccess) { | ||
const baseObjectType = getBaseConstraint((<IndexedAccessType>t).objectType); | ||
const baseIndexType = getBaseConstraint((<IndexedAccessType>t).indexType); | ||
const baseIndexedAccess = baseObjectType && baseIndexType && getIndexedAccessTypeOrUndefined(baseObjectType, baseIndexType); | ||
const baseIndexedAccess = baseObjectType && baseIndexType && getIndexedAccessTypeOrUndefined(baseObjectType, baseIndexType, /*accessNode*/ undefined, (t as IndexedAccessType).accessFlags); | ||
return baseIndexedAccess && getBaseConstraint(baseIndexedAccess); | ||
} | ||
if (t.flags & TypeFlags.Conditional) { | ||
|
@@ -13592,12 +13583,13 @@ namespace ts { | |
return result; | ||
} | ||
|
||
function createIndexedAccessType(objectType: Type, indexType: Type, aliasSymbol: Symbol | undefined, aliasTypeArguments: readonly Type[] | undefined) { | ||
function createIndexedAccessType(objectType: Type, indexType: Type, aliasSymbol: Symbol | undefined, aliasTypeArguments: readonly Type[] | undefined, accessFlags: AccessFlags) { | ||
const type = <IndexedAccessType>createType(TypeFlags.IndexedAccess); | ||
type.objectType = objectType; | ||
type.indexType = indexType; | ||
type.aliasSymbol = aliasSymbol; | ||
type.aliasTypeArguments = aliasTypeArguments; | ||
type.accessFlags = accessFlags; | ||
return type; | ||
} | ||
|
||
|
@@ -13650,6 +13642,10 @@ namespace ts { | |
function getPropertyTypeForIndexType(originalObjectType: Type, objectType: Type, indexType: Type, fullIndexType: Type, suppressNoImplicitAnyError: boolean, accessNode: ElementAccessExpression | IndexedAccessTypeNode | PropertyName | BindingName | SyntheticExpression | undefined, accessFlags: AccessFlags, reportDeprecated?: boolean) { | ||
const accessExpression = accessNode && accessNode.kind === SyntaxKind.ElementAccessExpression ? accessNode : undefined; | ||
const propName = accessNode && isPrivateIdentifier(accessNode) ? undefined : getPropertyNameFromIndex(indexType, accessNode); | ||
const shouldIncludeUndefined = | ||
compilerOptions.noUncheckedIndexedAccess && | ||
(accessFlags & (AccessFlags.Writing | AccessFlags.ExpressionPosition)) === AccessFlags.ExpressionPosition; | ||
|
||
if (propName !== undefined) { | ||
const prop = getPropertyOfType(objectType, propName); | ||
if (prop) { | ||
|
@@ -13687,7 +13683,10 @@ namespace ts { | |
} | ||
} | ||
errorIfWritingToReadonlyIndex(getIndexInfoOfType(objectType, IndexKind.Number)); | ||
return mapType(objectType, t => getRestTypeOfTupleType(<TupleTypeReference>t) || undefinedType); | ||
return mapType(objectType, t => { | ||
const restType = getRestTypeOfTupleType(<TupleTypeReference>t) || undefinedType; | ||
return shouldIncludeUndefined ? getUnionType([restType, undefinedType]) : restType; | ||
}); | ||
} | ||
} | ||
if (!(indexType.flags & TypeFlags.Nullable) && isTypeAssignableToKind(indexType, TypeFlags.StringLike | TypeFlags.NumberLike | TypeFlags.ESSymbolLike)) { | ||
|
@@ -13703,9 +13702,6 @@ namespace ts { | |
} | ||
return undefined; | ||
} | ||
const shouldIncludeUndefined = | ||
compilerOptions.noUncheckedIndexedAccess && | ||
(accessFlags & (AccessFlags.Writing | AccessFlags.ExpressionPosition)) === AccessFlags.ExpressionPosition; | ||
if (accessNode && !isTypeAssignableToKind(indexType, TypeFlags.String | TypeFlags.Number)) { | ||
const indexNode = getIndexNodeForAccessExpression(accessNode); | ||
error(indexNode, Diagnostics.Type_0_cannot_be_used_as_an_index_type, typeToString(indexType)); | ||
|
@@ -13976,10 +13972,6 @@ namespace ts { | |
return wildcardType; | ||
} | ||
|
||
const shouldIncludeUndefined = | ||
compilerOptions.noUncheckedIndexedAccess && | ||
(accessFlags & (AccessFlags.Writing | AccessFlags.ExpressionPosition)) === AccessFlags.ExpressionPosition; | ||
|
||
// If the object type has a string index signature and no other members we know that the result will | ||
// always be the type of that index signature and we can simplify accordingly. | ||
if (isStringIndexSignatureOnlyType(objectType) && !(indexType.flags & TypeFlags.Nullable) && isTypeAssignableToKind(indexType, TypeFlags.String | TypeFlags.Number)) { | ||
|
@@ -14001,10 +13993,10 @@ namespace ts { | |
const id = objectType.id + "," + indexType.id; | ||
let type = indexedAccessTypes.get(id); | ||
if (!type) { | ||
indexedAccessTypes.set(id, type = createIndexedAccessType(objectType, indexType, aliasSymbol, aliasTypeArguments)); | ||
indexedAccessTypes.set(id, type = createIndexedAccessType(objectType, indexType, aliasSymbol, aliasTypeArguments, accessFlags)); | ||
} | ||
|
||
return shouldIncludeUndefined ? getUnionType([type, undefinedType]) : type; | ||
return type; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. At this point, we don’t yet know whether the index type will be constrained to something that resolves to property signatures or index signatures, so we can’t return a union with undefined. But when we come back to do the deferred checking of the indexed access type we saved, we’ve lost the As I’m writing this, I’m realizing that this is probably broken for generic indexed access types that get used both for reading and for writing, so converting back to draft. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The only easy way I could find to fix this was creating separate IndexedAccessTypes (and keying the cache) depending on whether it was an expression where function<T extends string>(key: T) {
obj[key] = undefined;
return obj[key];
} the two usages of |
||
} | ||
// In the following we resolve T[K] to the type of the property in T selected by K. | ||
// We treat boolean as different from other unions to improve errors; | ||
|
Uh oh!
There was an error while loading. Please reload this page.