mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-09 02:30:15 -06:00
Optimize T[K] where T has string index signature and no other members
This commit is contained in:
parent
71f3e1fdde
commit
eb1e7ec6db
@ -10145,6 +10145,11 @@ namespace ts {
|
||||
if (objectType === wildcardType || indexType === wildcardType) {
|
||||
return wildcardType;
|
||||
}
|
||||
// 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)) {
|
||||
indexType = stringType;
|
||||
}
|
||||
// If the index type is generic, or if the object type is generic and doesn't originate in an expression,
|
||||
// we are performing a higher-order index access where we cannot meaningfully access the properties of the
|
||||
// object type. Note that for a generic T and a non-generic K, we eagerly resolve T[K] if it originates in
|
||||
@ -12022,6 +12027,12 @@ namespace ts {
|
||||
return !!(getObjectFlags(type) & ObjectFlags.Anonymous) && isEmptyObjectType(type);
|
||||
}
|
||||
|
||||
function isStringIndexSignatureOnlyType(type: Type): boolean {
|
||||
return type.flags & TypeFlags.Object && getPropertiesOfType(type).length === 0 && getIndexInfoOfType(type, IndexKind.String) && !getIndexInfoOfType(type, IndexKind.Number) ||
|
||||
type.flags & TypeFlags.UnionOrIntersection && every((<UnionOrIntersectionType>type).types, isStringIndexSignatureOnlyType) ||
|
||||
false;
|
||||
}
|
||||
|
||||
function isEnumTypeRelatedTo(sourceSymbol: Symbol, targetSymbol: Symbol, errorReporter?: ErrorReporter) {
|
||||
if (sourceSymbol === targetSymbol) {
|
||||
return true;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user