Skip to content

Revert "Fix getConstraintOfIndexedAccess" #17903

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

Merged
merged 1 commit into from
Aug 18, 2017
Merged
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
18 changes: 7 additions & 11 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5911,13 +5911,7 @@ namespace ts {
return transformed;
}
const baseObjectType = getBaseConstraintOfType(type.objectType);
const keepTypeParameterForMappedType = baseObjectType && getObjectFlags(baseObjectType) & ObjectFlags.Mapped && type.indexType.flags & TypeFlags.TypeParameter;
const baseIndexType = !keepTypeParameterForMappedType && getBaseConstraintOfType(type.indexType);
if (baseObjectType && baseIndexType === stringType && !getIndexInfoOfType(baseObjectType, IndexKind.String)) {
// getIndexedAccessType returns `any` for X[string] where X doesn't have an index signature.
// instead, return undefined so that the indexed access check fails
return undefined;
}
const baseIndexType = getBaseConstraintOfType(type.indexType);
return baseObjectType || baseIndexType ? getIndexedAccessType(baseObjectType || type.objectType, baseIndexType || type.indexType) : undefined;
}

Expand Down Expand Up @@ -5967,9 +5961,8 @@ namespace ts {
function computeBaseConstraint(t: Type): Type {
if (t.flags & TypeFlags.TypeParameter) {
const constraint = getConstraintFromTypeParameter(<TypeParameter>t);
return (t as TypeParameter).isThisType || !constraint ?
constraint :
getBaseConstraint(constraint);
return (<TypeParameter>t).isThisType ? constraint :
constraint ? getBaseConstraint(constraint) : undefined;
}
if (t.flags & TypeFlags.UnionOrIntersection) {
const types = (<UnionOrIntersectionType>t).types;
Expand Down Expand Up @@ -5997,6 +5990,9 @@ namespace ts {
const baseIndexedAccess = baseObjectType && baseIndexType ? getIndexedAccessType(baseObjectType, baseIndexType) : undefined;
return baseIndexedAccess && baseIndexedAccess !== unknownType ? getBaseConstraint(baseIndexedAccess) : undefined;
}
if (isGenericMappedType(t)) {
return emptyObjectType;
}
return t;
}
}
Expand Down Expand Up @@ -9293,7 +9289,7 @@ namespace ts {
}
else if (target.flags & TypeFlags.IndexedAccess) {
// A type S is related to a type T[K] if S is related to A[K], where K is string-like and
// A is the apparent type of T.
// A is the apparent type of S.
const constraint = getConstraintOfType(<IndexedAccessType>target);
if (constraint) {
if (result = isRelatedTo(source, constraint, reportErrors)) {
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,7 @@ namespace ts {
export function sum<T extends Record<K, number>, K extends string>(array: T[], prop: K): number {
let result = 0;
for (const v of array) {
// TODO: Remove the following type assertion once the fix for #17069 is merged
// Note: we need the following type assertion because of GH #17069
result += v[prop] as number;
}
return result;
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.