* This roughly mirrors `resolveMappedTypeMembers` in the nongeneric case, except only reports a union of the keys calculated, * rather than manufacturing the properties. We can't just fetch the `constraintType` since that would ignore mappings * and mapping the `constraintType` dir
(type, stringsOnly, noIndexSignatures)
| 61754 | * @param noIndexSignatures Indicates if _string_ index signatures should be elided. (other index signatures are always reported) |
| 61755 | */ |
| 61756 | function getIndexTypeForMappedType(type, stringsOnly, noIndexSignatures) { |
| 61757 | var typeParameter = getTypeParameterFromMappedType(type); |
| 61758 | var constraintType = getConstraintTypeFromMappedType(type); |
| 61759 | var nameType = getNameTypeFromMappedType(type.target || type); |
| 61760 | if (!nameType && !noIndexSignatures) { |
| 61761 | // no mapping and no filtering required, just quickly bail to returning the constraint in the common case |
| 61762 | return constraintType; |
| 61763 | } |
| 61764 | var keyTypes = []; |
| 61765 | if (isMappedTypeWithKeyofConstraintDeclaration(type)) { |
| 61766 | // We have a { [P in keyof T]: X } |
| 61767 | // `getApparentType` on the T in a generic mapped type can trigger a circularity |
| 61768 | // (conditionals and `infer` types create a circular dependency in the constraint resolution) |
| 61769 | // so we only eagerly manifest the keys if the constraint is nongeneric |
| 61770 | if (!isGenericIndexType(constraintType)) { |
| 61771 | var modifiersType = getApparentType(getModifiersTypeFromMappedType(type)); // The 'T' in 'keyof T' |
| 61772 | forEachMappedTypePropertyKeyTypeAndIndexSignatureKeyType(modifiersType, 8576 /* TypeFlags.StringOrNumberLiteralOrUnique */, stringsOnly, addMemberForKeyType); |
| 61773 | } |
| 61774 | else { |
| 61775 | // we have a generic index and a homomorphic mapping (but a distributive key remapping) - we need to defer the whole `keyof whatever` for later |
| 61776 | // since it's not safe to resolve the shape of modifier type |
| 61777 | return getIndexTypeForGenericType(type, stringsOnly); |
| 61778 | } |
| 61779 | } |
| 61780 | else { |
| 61781 | forEachType(getLowerBoundOfKeyType(constraintType), addMemberForKeyType); |
| 61782 | } |
| 61783 | if (isGenericIndexType(constraintType)) { // include the generic component in the resulting type |
| 61784 | forEachType(constraintType, addMemberForKeyType); |
| 61785 | } |
| 61786 | // we had to pick apart the constraintType to potentially map/filter it - compare the final resulting list with the original constraintType, |
| 61787 | // so we can return the union that preserves aliases/origin data if possible |
| 61788 | var result = noIndexSignatures ? filterType(getUnionType(keyTypes), function (t) { return !(t.flags & (1 /* TypeFlags.Any */ | 4 /* TypeFlags.String */)); }) : getUnionType(keyTypes); |
| 61789 | if (result.flags & 1048576 /* TypeFlags.Union */ && constraintType.flags & 1048576 /* TypeFlags.Union */ && getTypeListId(result.types) === getTypeListId(constraintType.types)) { |
| 61790 | return constraintType; |
| 61791 | } |
| 61792 | return result; |
| 61793 | function addMemberForKeyType(keyType) { |
| 61794 | var propNameType = nameType ? instantiateType(nameType, appendTypeMapping(type.mapper, typeParameter, keyType)) : keyType; |
| 61795 | // `keyof` currently always returns `string | number` for concrete `string` index signatures - the below ternary keeps that behavior for mapped types |
| 61796 | // See `getLiteralTypeFromProperties` where there's a similar ternary to cause the same behavior. |
| 61797 | keyTypes.push(propNameType === stringType ? stringOrNumberType : propNameType); |
| 61798 | } |
| 61799 | } |
| 61800 | // Ordinarily we reduce a keyof M, where M is a mapped type { [P in K as N<P>]: X }, to simply N<K>. This however presumes |
| 61801 | // that N distributes over union types, i.e. that N<A | B | C> is equivalent to N<A> | N<B> | N<C>. Specifically, we only |
| 61802 | // want to perform the reduction when the name type of a mapped type is distributive with respect to the type variable |
no test coverage detected
searching dependent graphs…