Skip to content

Commit 8891d4f

Browse files
committed
Permit reverse mapped types to be created from partially inferable types
1 parent a86fa20 commit 8891d4f

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

src/compiler/checker.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14887,10 +14887,22 @@ namespace ts {
1488714887
return type;
1488814888
}
1488914889

14890+
// We consider a type to be partially inferable if it isn't marked non-inferable or if it is
14891+
// an object literal type with at least one property of an inferable type. For example, an object
14892+
// literal { a: 123, b: x => true } is marked non-inferable because it contains a context sensitive
14893+
// arrow function, but is considered partially inferable because property 'a' has an inferable type.
14894+
function isPartiallyInferableType(type: Type): boolean {
14895+
return !(getObjectFlags(type) & ObjectFlags.NonInferrableType) ||
14896+
isObjectLiteralType(type) && some(getPropertiesOfType(type), prop => isPartiallyInferableType(getTypeOfSymbol(prop)));
14897+
}
14898+
1489014899
function createReverseMappedType(source: Type, target: MappedType, constraint: IndexType) {
14891-
// If any property contains context sensitive functions that have been skipped, the source type
14892-
// is incomplete and we can't infer a meaningful input type.
14893-
if (getObjectFlags(source) & ObjectFlags.NonInferrableType || getPropertiesOfType(source).length === 0 && !getIndexInfoOfType(source, IndexKind.String)) {
14900+
// We consider a source type reverse mappable if it has a string index signature or if
14901+
// it has one or more properties and all properties have inferable types.
14902+
const properties = getPropertiesOfType(source);
14903+
const isReverseMappable = getIndexInfoOfType(source, IndexKind.String) ||
14904+
properties.length !== 0 && every(properties, prop => isPartiallyInferableType(getTypeOfSymbol(prop)));
14905+
if (!isReverseMappable) {
1489414906
return undefined;
1489514907
}
1489614908
// For arrays and tuples we infer new arrays and tuples where the reverse mapping has been

0 commit comments

Comments
 (0)