diff --git a/tsc/internal/checker/checker.go b/tsc/internal/checker/checker.go index d689a6453e8a6..8f70e2dda17f5 100644 --- a/tsc/internal/checker/checker.go +++ b/tsc/internal/checker/checker.go @@ -774,6 +774,7 @@ type Checker struct { reportUnmeasurableMapper *TypeMapper restrictiveMapper *TypeMapper permissiveMapper *TypeMapper + rigidInferenceMapper *TypeMapper emptyObjectType *Type emptyJsxObjectType *Type emptyFreshJsxObjectType *Type @@ -857,6 +858,7 @@ type Checker struct { assignableRelation *Relation comparableRelation *Relation identityRelation *Relation + inferencePatternRelation *Relation enumRelation map[EnumRelationKey]RelationComparisonResult getGlobalESSymbolType func() *Type getGlobalBigIntType func() *Type @@ -926,6 +928,7 @@ type Checker struct { packagesMap map[string]bool activeMappers []*TypeMapper activeTypeMappersCaches []map[CacheHashKey]*Type + activeTypeMapperStateCaches []map[CacheHashKey]typeMapperCacheState ambientModulesOnce sync.Once ambientModules []*ast.Symbol withinUnreachableCode bool @@ -1052,11 +1055,12 @@ func NewChecker(program Program, tracer *Tracer) (*Checker, *sync.Mutex) { c.numericStringType = c.getTemplateLiteralType([]string{"", ""}, []*Type{c.numberType}) // The `${number}` type c.templateConstraintType = c.getUnionType([]*Type{c.stringType, c.numberType, c.booleanType, c.bigintType, c.nullType, c.undefinedType}) c.uniqueLiteralType = c.newIntrinsicType(TypeFlagsNever, "never") // Special `never` flagged by union reduction to behave as a literal - c.uniqueLiteralMapper = newFunctionTypeMapper(c.getUniqueLiteralTypeForTypeParameter) - c.reportUnreliableMapper = newFunctionTypeMapper(c.reportUnreliableWorker) - c.reportUnmeasurableMapper = newFunctionTypeMapper(c.reportUnmeasurableWorker) - c.restrictiveMapper = newFunctionTypeMapper(c.restrictiveMapperWorker) - c.permissiveMapper = newFunctionTypeMapper(c.permissiveMapperWorker) + c.uniqueLiteralMapper = newFunctionTypeMapper(c.getUniqueLiteralTypeForTypeParameter, nil, nil) + c.reportUnreliableMapper = newFunctionTypeMapper(c.reportUnreliableWorker, nil, nil) + c.reportUnmeasurableMapper = newFunctionTypeMapper(c.reportUnmeasurableWorker, nil, nil) + c.restrictiveMapper = newFunctionTypeMapper(c.restrictiveMapperWorker, nil, nil) + c.permissiveMapper = newFunctionTypeMapper(c.permissiveMapperWorker, nil, nil) + c.rigidInferenceMapper = newFunctionTypeMapper(c.rigidInferenceMapperWorker, nil, nil) c.emptyObjectType = c.newAnonymousType(nil /*symbol*/, nil, nil, nil, nil) c.emptyJsxObjectType = c.newAnonymousType(nil /*symbol*/, nil, nil, nil, nil) c.emptyFreshJsxObjectType = c.newAnonymousType(nil /*symbol*/, nil, nil, nil, nil) @@ -1097,6 +1101,7 @@ func NewChecker(program Program, tracer *Tracer) (*Checker, *sync.Mutex) { c.assignableRelation = &Relation{} c.comparableRelation = &Relation{} c.identityRelation = &Relation{} + c.inferencePatternRelation = &Relation{} c.enumRelation = make(map[EnumRelationKey]RelationComparisonResult) c.moduleImportAttributesTypes = make(map[*ast.Symbol]*Type) c.getGlobalESSymbolType = c.getGlobalTypeResolver("Symbol", 0 /*arity*/, false /*reportErrors*/) @@ -18060,13 +18065,14 @@ func getConditionalTypeKey(typeArguments []*Type, alias *TypeAlias, forConstrain return b.hash() } -func getRelationKey(source *Type, target *Type, intersectionState IntersectionState, isIdentity bool, ignoreConstraints bool) (CacheHashKey, bool) { - if isIdentity && source.id > target.id { +func (c *Checker) getRelationKey(source *Type, target *Type, intersectionState IntersectionState, relation *Relation, ignoreConstraints bool) (CacheHashKey, bool) { + if relation == c.identityRelation && source.id > target.id { source, target = target, source } var b keyBuilder var constrained bool - if isTypeReferenceWithGenericArguments(source) && isTypeReferenceWithGenericArguments(target) { + // Inference-pattern cache effects identify particular binders, so their keys cannot alpha-rename type parameters. + if relation != c.inferencePatternRelation && isTypeReferenceWithGenericArguments(source) && isTypeReferenceWithGenericArguments(target) { b.writeByte('g') constrained = b.writeGenericTypeReferences(source, target, ignoreConstraints) } else { @@ -22748,10 +22754,19 @@ func (c *Checker) instantiateType(t *Type, m *TypeMapper) *Type { } func (c *Checker) instantiateTypeWithAlias(t *Type, m *TypeMapper, alias *TypeAlias) *Type { + if t == nil || m == nil { + return t + } + if m.Store(nil) { + mapped := getMappedType(t, m) + if mapped != t || t.flags&TypeFlagsTypeParameter != 0 { + return mapped + } + } // Check for type variables in the alias, so things like `type Brand = number & {}` can potentially be copied with new alias type args, despite them being unreferenced. // This is the behavior most people using aliases expect, and prevents the cache from leaking type parameters outside their scope of validity. // tests/cases/compiler/declarationEmitArrowFunctionNoRenaming.ts contains an example of this, which previously only worked in strada via some input node reuse logic instead. - if t == nil || m == nil || !(c.couldContainTypeVariables(t) || (t.alias != nil && len(t.alias.typeArguments) > 0 && core.Some(t.alias.typeArguments, c.couldContainTypeVariables))) { + if !(c.couldContainTypeVariables(t) || (t.alias != nil && len(t.alias.typeArguments) > 0 && core.Some(t.alias.typeArguments, c.couldContainTypeVariables))) { return t } if len(c.instantiationStack) == 100 || c.instantiationCount >= 5_000_000 { @@ -22780,10 +22795,17 @@ func (c *Checker) instantiateTypeWithAlias(t *Type, m *TypeMapper, alias *TypeAl b.writeType(t) b.writeAlias(alias) key := b.hash() - cache := c.activeTypeMappersCaches[core.IfElse(index != -1, index, len(c.activeTypeMappersCaches)-1)] + cacheIndex := core.IfElse(index != -1, index, len(c.activeTypeMappersCaches)-1) + cache := c.activeTypeMappersCaches[cacheIndex] if cachedType, ok := cache[key]; ok { + state := c.activeTypeMapperStateCaches[cacheIndex][key] + m.Load(state.before, state.after) return cachedType } + var before TypeMapperState + if index != -1 { + m.Store(&before) + } c.TotalInstantiationCount++ c.instantiationCount++ c.instantiationStack = append(c.instantiationStack, t) @@ -22792,6 +22814,18 @@ func (c *Checker) instantiateTypeWithAlias(t *Type, m *TypeMapper, alias *TypeAl c.popActiveMapper() } else { cache[key] = result + stateCache := c.activeTypeMapperStateCaches[cacheIndex] + if len(before) != 0 { + var after TypeMapperState + m.Store(&after) + if stateCache == nil { + stateCache = make(map[CacheHashKey]typeMapperCacheState) + c.activeTypeMapperStateCaches[cacheIndex] = stateCache + } + stateCache[key] = typeMapperCacheState{before: before, after: after} + } else { + delete(stateCache, key) + } } c.instantiationStack[len(c.instantiationStack)-1] = nil c.instantiationStack = c.instantiationStack[:len(c.instantiationStack)-1] @@ -22831,6 +22865,11 @@ func (c *Checker) pushActiveMapper(mapper *TypeMapper) { } else { c.activeTypeMappersCaches = append(c.activeTypeMappersCaches, make(map[CacheHashKey]*Type, 1)) } + if cap(c.activeTypeMapperStateCaches) > lastIndex { + c.activeTypeMapperStateCaches = c.activeTypeMapperStateCaches[:lastIndex+1] + } else { + c.activeTypeMapperStateCaches = append(c.activeTypeMapperStateCaches, nil) + } } func (c *Checker) popActiveMapper() { @@ -22841,6 +22880,8 @@ func (c *Checker) popActiveMapper() { lastIndex := len(c.activeTypeMappersCaches) - 1 clear(c.activeTypeMappersCaches[lastIndex]) c.activeTypeMappersCaches = c.activeTypeMappersCaches[:lastIndex] + clear(c.activeTypeMapperStateCaches[lastIndex]) + c.activeTypeMapperStateCaches = c.activeTypeMapperStateCaches[:lastIndex] } func (c *Checker) findActiveMapper(mapper *TypeMapper) int { @@ -22851,6 +22892,9 @@ func (c *Checker) clearActiveMapperCaches() { for _, cache := range c.activeTypeMappersCaches { clear(cache) } + for _, cache := range c.activeTypeMapperStateCaches { + clear(cache) + } } // Return true if the given type could possibly reference a type parameter for which @@ -25257,6 +25301,22 @@ func (c *Checker) permissiveMapperWorker(t *Type) *Type { return t } +func isInferencePatternParameter(t *Type) bool { + return t.flags&TypeFlagsTypeParameter != 0 && t.AsTypeParameter().isInferencePattern +} + +func (c *Checker) rigidInferenceMapperWorker(t *Type) *Type { + if isInferencePatternParameter(t) { + d := t.AsTypeParameter() + if d.rigidInferenceType == nil { + d.rigidInferenceType = c.cloneTypeParameter(t) + d.rigidInferenceType.AsTypeParameter().mapper = c.rigidInferenceMapper + } + return d.rigidInferenceType + } + return t +} + func (c *Checker) getTrueTypeFromConditionalType(t *Type) *Type { d := t.AsConditionalType() if d.resolvedTrueType == nil { diff --git a/tsc/internal/checker/mapper.go b/tsc/internal/checker/mapper.go index cb900e7372f89..52f80859ec344 100644 --- a/tsc/internal/checker/mapper.go +++ b/tsc/internal/checker/mapper.go @@ -72,6 +72,67 @@ func (m *TypeMapper) Map(t *Type) *Type { func (m *TypeMapper) Kind() TypeMapperKind { return m.kind } +// Store(nil) reports whether this mapper has state; otherwise Store captures it. +func (m *TypeMapper) Store(state *TypeMapperState) bool { + if m == nil { + return false + } + switch m.kind { + case TypeMapperKindSimple: + return castTypeMapper[SimpleTypeMapper](m, TypeMapperKindSimple).Store(state) + case TypeMapperKindArray: + return castTypeMapper[ArrayTypeMapper](m, TypeMapperKindArray).Store(state) + case TypeMapperKindArrayToSingle: + return castTypeMapper[ArrayToSingleTypeMapper](m, TypeMapperKindArrayToSingle).Store(state) + case TypeMapperKindDeferred: + return castTypeMapper[DeferredTypeMapper](m, TypeMapperKindDeferred).Store(state) + case TypeMapperKindFunction: + return castTypeMapper[FunctionTypeMapper](m, TypeMapperKindFunction).Store(state) + case TypeMapperKindMerged: + return castTypeMapper[MergedTypeMapper](m, TypeMapperKindMerged).Store(state) + case TypeMapperKindComposite: + return castTypeMapper[CompositeTypeMapper](m, TypeMapperKindComposite).Store(state) + case TypeMapperKindInference: + return castTypeMapper[InferenceTypeMapper](m, TypeMapperKindInference).Store(state) + default: + panic("Invalid type mapper kind") + } +} + +func (m *TypeMapper) Load(before, after TypeMapperState) { + if m == nil { + return + } + switch m.kind { + case TypeMapperKindSimple: + castTypeMapper[SimpleTypeMapper](m, TypeMapperKindSimple).Load(before, after) + case TypeMapperKindArray: + castTypeMapper[ArrayTypeMapper](m, TypeMapperKindArray).Load(before, after) + case TypeMapperKindArrayToSingle: + castTypeMapper[ArrayToSingleTypeMapper](m, TypeMapperKindArrayToSingle).Load(before, after) + case TypeMapperKindDeferred: + castTypeMapper[DeferredTypeMapper](m, TypeMapperKindDeferred).Load(before, after) + case TypeMapperKindFunction: + castTypeMapper[FunctionTypeMapper](m, TypeMapperKindFunction).Load(before, after) + case TypeMapperKindMerged: + castTypeMapper[MergedTypeMapper](m, TypeMapperKindMerged).Load(before, after) + case TypeMapperKindComposite: + castTypeMapper[CompositeTypeMapper](m, TypeMapperKindComposite).Load(before, after) + case TypeMapperKindInference: + castTypeMapper[InferenceTypeMapper](m, TypeMapperKindInference).Load(before, after) + default: + panic("Invalid type mapper kind") + } +} + +// TypeMapperState stores a snapshot for each mapper participating in cache bookkeeping. +type TypeMapperState map[*TypeMapper]any + +type typeMapperCacheState struct { + before TypeMapperState + after TypeMapperState +} + func (m *TypeMapper) MapsThisOnly() bool { switch m.kind { case TypeMapperKindSimple: @@ -108,6 +169,9 @@ func (c *Checker) combineTypeMappers(m1 *TypeMapper, m2 *TypeMapper) *TypeMapper } func (c *Checker) mapTypeWithCompositeMapper(t *Type, m1 *TypeMapper, m2 *TypeMapper) *Type { + if m2 == nil { + return getMappedType(t, m1) + } if m1 == nil { return getMappedType(t, m2) } @@ -157,6 +221,10 @@ type TypeMapperBase struct { func (m *TypeMapperBase) Map(t *Type) *Type { return t } func (m *TypeMapperBase) MapsThisOnly() bool { return false } +func (m *TypeMapperBase) Store(*TypeMapperState) bool { + return false +} +func (m *TypeMapperBase) Load(TypeMapperState, TypeMapperState) {} // SimpleTypeMapper @@ -274,13 +342,23 @@ func (m *DeferredTypeMapper) MapsThisOnly() bool { type FunctionTypeMapper struct { TypeMapperBase - fn func(*Type) *Type + fn func(*Type) *Type + store func() any + load func(before, after any) } -func newFunctionTypeMapper(fn func(*Type) *Type) *TypeMapper { +// Cache handlers are optional as a pair. Store captures an immutable snapshot; +// load replays the effect between two snapshots. Hooked mappers also observe whole types. +// Tracked state must not change the mapped result. +func newFunctionTypeMapper(fn func(*Type) *Type, store func() any, load func(before, after any)) *TypeMapper { + if (store == nil) != (load == nil) { + panic("Function mapper cache handlers must be provided together") + } m := &FunctionTypeMapper{} m.kind = TypeMapperKindFunction m.fn = fn + m.store = store + m.load = load return &m.TypeMapper } @@ -288,6 +366,27 @@ func (m *FunctionTypeMapper) Map(t *Type) *Type { return m.fn(t) } +func (m *FunctionTypeMapper) Store(state *TypeMapperState) bool { + if m.store == nil { + return false + } + if state != nil { + if *state == nil { + *state = make(TypeMapperState) + } + if _, stored := (*state)[&m.TypeMapper]; !stored { + (*state)[&m.TypeMapper] = m.store() + } + } + return true +} + +func (m *FunctionTypeMapper) Load(before, after TypeMapperState) { + if value, stored := after[&m.TypeMapper]; stored { + m.load(before[&m.TypeMapper], value) + } +} + // MergedTypeMapper type MergedTypeMapper struct { @@ -308,6 +407,19 @@ func (m *MergedTypeMapper) Map(t *Type) *Type { return m.m2.Map(m.m1.Map(t)) } +func (m *MergedTypeMapper) Store(state *TypeMapperState) bool { + left := m.m1.Store(state) + right := m.m2.Store(state) + return left || right +} + +func (m *MergedTypeMapper) Load(before, after TypeMapperState) { + // Shared children occur only once in the stored state. + for mapper := range after { + mapper.Load(before, after) + } +} + // CompositeTypeMapper type CompositeTypeMapper struct { @@ -327,6 +439,9 @@ func newCompositeTypeMapper(c *Checker, m1 *TypeMapper, m2 *TypeMapper) *TypeMap } func (m *CompositeTypeMapper) Map(t *Type) *Type { + if m.m2 == nil { + return m.m1.Map(t) + } t1 := m.m1.Map(t) if t1 != t { return m.c.instantiateType(t1, m.m2) @@ -334,6 +449,18 @@ func (m *CompositeTypeMapper) Map(t *Type) *Type { return m.m2.Map(t) } +func (m *CompositeTypeMapper) Store(state *TypeMapperState) bool { + left := m.m1.Store(state) + right := m.m2.Store(state) + return left || right +} + +func (m *CompositeTypeMapper) Load(before, after TypeMapperState) { + for mapper := range after { + mapper.Load(before, after) + } +} + // InferenceTypeMapper type InferenceTypeMapper struct { diff --git a/tsc/internal/checker/relater.go b/tsc/internal/checker/relater.go index 562ae0451cb43..8f020352e7b63 100644 --- a/tsc/internal/checker/relater.go +++ b/tsc/internal/checker/relater.go @@ -1,6 +1,7 @@ package checker import ( + "maps" "math/bits" "slices" "strconv" @@ -19,13 +20,14 @@ import ( type SignatureCheckMode uint32 const ( - SignatureCheckModeNone SignatureCheckMode = 0 - SignatureCheckModeBivariantCallback SignatureCheckMode = 1 << 0 - SignatureCheckModeStrictCallback SignatureCheckMode = 1 << 1 - SignatureCheckModeIgnoreReturnTypes SignatureCheckMode = 1 << 2 - SignatureCheckModeStrictArity SignatureCheckMode = 1 << 3 - SignatureCheckModeStrictTopSignature SignatureCheckMode = 1 << 4 - SignatureCheckModeCallback SignatureCheckMode = SignatureCheckModeBivariantCallback | SignatureCheckModeStrictCallback + SignatureCheckModeNone SignatureCheckMode = 0 + SignatureCheckModeBivariantCallback SignatureCheckMode = 1 << 0 + SignatureCheckModeStrictCallback SignatureCheckMode = 1 << 1 + SignatureCheckModeIgnoreReturnTypes SignatureCheckMode = 1 << 2 + SignatureCheckModeStrictArity SignatureCheckMode = 1 << 3 + SignatureCheckModeStrictTopSignature SignatureCheckMode = 1 << 4 + SignatureCheckModeIgnoreParameterTypes SignatureCheckMode = 1 << 5 + SignatureCheckModeCallback SignatureCheckMode = SignatureCheckModeBivariantCallback | SignatureCheckModeStrictCallback ) type MinArgumentCountFlags uint32 @@ -97,7 +99,8 @@ func asRecursionId[T *ast.Node | *ast.Symbol | *Type](value T) RecursionId { } type Relation struct { - results map[CacheHashKey]RelationComparisonResult + results map[CacheHashKey]RelationComparisonResult + inferenceUsages map[CacheHashKey]map[*Type]inferenceUsage } func (r *Relation) get(key CacheHashKey) RelationComparisonResult { @@ -168,36 +171,48 @@ func (c *Checker) areTypesComparable(type1 *Type, type2 *Type) bool { } func (c *Checker) isTypeRelatedTo(source *Type, target *Type, relation *Relation) bool { - if isFreshLiteralType(source) { - source = source.AsLiteralType().regularType - } - if isFreshLiteralType(target) { - target = target.AsLiteralType().regularType - } - if source == target { - return true - } - if relation != c.identityRelation { - if relation == c.comparableRelation && target.flags&TypeFlagsNever == 0 && c.isSimpleTypeRelatedTo(target, source, relation, nil) || c.isSimpleTypeRelatedTo(source, target, relation, nil) { - return true + // Pattern matching needs rigid source parameters and infer-usage observations, + // which identity/simple compatibility shortcuts cannot provide. The result + // cache below can still be used by replaying its recorded effects. + if relation != c.inferencePatternRelation { + if isFreshLiteralType(source) { + source = source.AsLiteralType().regularType } - } else if !((source.flags|target.flags)&(TypeFlagsUnionOrIntersection|TypeFlagsIndexedAccess|TypeFlagsConditional|TypeFlagsSubstitution) != 0) { - // We have excluded types that may simplify to other forms, so types must have identical flags - if source.flags != target.flags { - return false + if isFreshLiteralType(target) { + target = target.AsLiteralType().regularType } - if source.flags&TypeFlagsSingleton != 0 { + if source == target { return true } + if relation != c.identityRelation { + if relation == c.comparableRelation && target.flags&TypeFlagsNever == 0 && c.isSimpleTypeRelatedTo(target, source, relation, nil) || c.isSimpleTypeRelatedTo(source, target, relation, nil) { + return true + } + } else if !((source.flags|target.flags)&(TypeFlagsUnionOrIntersection|TypeFlagsIndexedAccess|TypeFlagsConditional|TypeFlagsSubstitution) != 0) { + // We have excluded types that may simplify to other forms, so types must have identical flags + if source.flags != target.flags { + return false + } + if source.flags&TypeFlagsSingleton != 0 { + return true + } + } } if source.flags&TypeFlagsObject != 0 && target.flags&TypeFlagsObject != 0 { - id, _ := getRelationKey(source, target, IntersectionStateNone, relation == c.identityRelation, false) + id, _ := c.getRelationKey(source, target, IntersectionStateNone, relation, false) related := relation.get(id) if related != RelationComparisonResultNone { + // Pattern results are checked before caching. At the top level only + // reliability effects need replaying; no enclosing infer uses can conflict. + if relation == c.inferencePatternRelation { + c.reliabilityFlags |= related & (RelationComparisonResultReportsUnmeasurable | RelationComparisonResultReportsUnreliable) + } return related&RelationComparisonResultSucceeded != 0 } } - if source.flags&TypeFlagsStructuredOrInstantiable != 0 || target.flags&TypeFlagsStructuredOrInstantiable != 0 { + // Scalar patterns also need the inference-pattern rules, not the compatibility + // shortcuts above (for example, never succeeds without an inference candidate). + if relation == c.inferencePatternRelation || source.flags&TypeFlagsStructuredOrInstantiable != 0 || target.flags&TypeFlagsStructuredOrInstantiable != 0 { return c.checkTypeRelatedTo(source, target, relation, nil /*errorNode*/) } return false @@ -369,9 +384,14 @@ func (c *Checker) checkTypeRelatedToEx( r.errorNode = errorNode r.relationCount = (16_000_000 - relation.size()) / 8 result := r.isRelatedToEx(source, target, RecursionFlagsBoth, errorNode != nil /*reportErrors*/, headMessage, IntersectionStateNone) + // Subcomparisons cache only their local infer uses. Combining them can introduce + // conflicting binder choices, and coinductive success is not branch reachability. + if relation == c.inferencePatternRelation { + result = checkInferencePatternResult(result, r.inferenceUsages) + } if r.overflow { // Record this relation as having failed such that we don't attempt the overflowing operation again. - id, _ := getRelationKey(source, target, IntersectionStateNone, relation == c.identityRelation, false /*ignoreConstraints*/) + id, _ := c.getRelationKey(source, target, IntersectionStateNone, relation, false /*ignoreConstraints*/) relation.set(id, RelationComparisonResultFailed|RelationComparisonResultComplexityOverflow) if tr := c.tracer; tr != nil { tr.Instant(tracing.PhaseCheckTypes, "checkTypeRelatedTo_DepthLimit", map[string]any{"sourceId": source.id, "targetId": target.id, "depth": len(r.sourceStack), "targetDepth": len(r.targetStack)}) @@ -1491,6 +1511,11 @@ func (c *Checker) isSignatureAssignableTo(source *Signature, target *Signature, return c.compareSignaturesRelated(source, target, core.IfElse(ignoreReturnTypes, SignatureCheckModeIgnoreReturnTypes, SignatureCheckModeNone), false /*reportErrors*/, nil /*errorReporter*/, c.compareTypesAssignable, nil /*reportUnreliableMarkers*/) != TernaryFalse } +func isBivariantSignature(signature *Signature) bool { + return signature.declaration != nil && (signature.declaration.Kind == ast.KindMethodDeclaration || + signature.declaration.Kind == ast.KindMethodSignature || signature.declaration.Kind == ast.KindConstructor) +} + func (c *Checker) compareSignaturesRelated(source *Signature, target *Signature, checkMode SignatureCheckMode, reportErrors bool, errorReporter ErrorReporter, compareTypes TypeComparer, reportUnreliableMarkers *TypeMapper) Ternary { if source == target { return TernaryTrue @@ -1528,11 +1553,7 @@ func (c *Checker) compareSignaturesRelated(source *Signature, target *Signature, if sourceRestType != nil || targetRestType != nil { c.instantiateType(core.IfElse(sourceRestType != nil, sourceRestType, targetRestType), reportUnreliableMarkers) } - kind := ast.KindUnknown - if target.declaration != nil { - kind = target.declaration.Kind - } - strictVariance := checkMode&SignatureCheckModeCallback == 0 && c.strictFunctionTypes && kind != ast.KindMethodDeclaration && kind != ast.KindMethodSignature && kind != ast.KindConstructor + strictVariance := checkMode&SignatureCheckModeCallback == 0 && c.strictFunctionTypes && !isBivariantSignature(target) result := TernaryTrue sourceThisType := c.getThisTypeOfSignature(source) if sourceThisType != nil && sourceThisType != c.voidType { @@ -1568,6 +1589,9 @@ func (c *Checker) compareSignaturesRelated(source *Signature, target *Signature, restIndex = -1 } for i := range paramCount { + if checkMode&SignatureCheckModeIgnoreParameterTypes != 0 { + break + } var sourceType *Type if i == restIndex { sourceType = c.getRestOrAnyTypeAtPosition(source, i) @@ -2582,22 +2606,28 @@ type ErrorChain struct { } type Relater struct { - c *Checker - relation *Relation - errorNode *ast.Node - errorChain *ErrorChain - relatedInfo []*ast.Diagnostic - maybeKeys []CacheHashKey - maybeKeysSet collections.Set[CacheHashKey] - sourceStack []*Type - targetStack []*Type - maybeCount int - sourceDepth int - targetDepth int - expandingFlags ExpandingFlags - overflow bool - relationCount int - next *Relater + c *Checker + relation *Relation + errorNode *ast.Node + errorChain *ErrorChain + relatedInfo []*ast.Diagnostic + maybeKeys []CacheHashKey + maybeKeysSet collections.Set[CacheHashKey] + sourceStack []*Type + targetStack []*Type + maybeCount int + sourceDepth int + targetDepth int + expandingFlags ExpandingFlags + overflow bool + relationCount int + inferenceUsages map[*Type]inferenceUsage + next *Relater +} + +type inferenceUsage struct { + covariant int + contravariant int } func (c *Checker) getRelater() *Relater { @@ -2611,17 +2641,49 @@ func (c *Checker) getRelater() *Relater { func (c *Checker) putRelater(r *Relater) { r.maybeKeysSet.Clear() + clear(r.inferenceUsages) *r = Relater{ - c: c, - maybeKeys: r.maybeKeys[:0], - maybeKeysSet: r.maybeKeysSet, - sourceStack: r.sourceStack[:0], - targetStack: r.targetStack[:0], - next: c.freeRelater, + c: c, + maybeKeys: r.maybeKeys[:0], + maybeKeysSet: r.maybeKeysSet, + sourceStack: r.sourceStack[:0], + targetStack: r.targetStack[:0], + inferenceUsages: r.inferenceUsages, + next: c.freeRelater, } c.freeRelater = r } +func (r *Relater) recordInferenceUsage(parameter *Type, usage inferenceUsage) { + if r.inferenceUsages == nil { + r.inferenceUsages = make(map[*Type]inferenceUsage) + } + current := r.inferenceUsages[parameter] + current.covariant += usage.covariant + current.contravariant += usage.contravariant + r.inferenceUsages[parameter] = current +} + +func (r *Relater) loadCachedRelationResult(id CacheHashKey, entry RelationComparisonResult) Ternary { + for parameter, usage := range r.relation.inferenceUsages[id] { + r.recordInferenceUsage(parameter, usage) + } + r.c.reliabilityFlags |= entry & (RelationComparisonResultReportsUnmeasurable | RelationComparisonResultReportsUnreliable) + return core.IfElse(entry&RelationComparisonResultSucceeded != 0, TernaryTrue, TernaryFalse) +} + +func checkInferencePatternResult(result Ternary, usages map[*Type]inferenceUsage) Ternary { + if result != TernaryTrue { + return TernaryFalse + } + for _, usage := range usages { + if usage.covariant != 0 && usage.contravariant != 0 { + return TernaryFalse + } + } + return TernaryTrue +} + func (r *Relater) isRelatedToSimple(source *Type, target *Type) Ternary { return r.isRelatedToEx(source, target, RecursionFlagsBoth, false /*reportErrors*/, nil /*headMessage*/, IntersectionStateNone) } @@ -2635,6 +2697,31 @@ func (r *Relater) isRelatedTo(source *Type, target *Type, recursionFlags Recursi } func (r *Relater) isRelatedToEx(originalSource *Type, originalTarget *Type, recursionFlags RecursionFlags, reportErrors bool, headMessage *diagnostics.Message, intersectionState IntersectionState) Ternary { + // Marked parameters are existential only in the target; captured source uses + // must be rigid. Handle binders before identity/constraint shortcuts so their + // variance and bounds are checked rather than treating them as ordinary parameters. + if r.relation == r.c.inferencePatternRelation { + originalSource = r.c.instantiateType(originalSource, r.c.rigidInferenceMapper) + if originalSource.flags&TypeFlagsNever != 0 { + // never supplies no inference candidate, so it must not record a variance. + return TernaryTrue + } + if isInferencePatternParameter(originalTarget) { + r.recordInferenceUsage(originalTarget, inferenceUsage{covariant: 1}) + constraint := r.c.getConstraintOfTypeParameter(originalTarget) + if constraint == nil { + return TernaryTrue + } + // A referenced infer binder constrains this match; it is not another inference site. + constraint = r.c.instantiateType(constraint, r.c.rigidInferenceMapper) + return r.isRelatedToEx(originalSource, constraint, recursionFlags, false, nil, intersectionState) + } + // Intersection inference removes matching constituents before selecting candidates. + // Independent wildcard matches do not establish that the resulting binding works. + if originalTarget.flags&TypeFlagsIntersection != 0 && core.Some(originalTarget.Types(), isInferencePatternParameter) { + return TernaryFalse + } + } if originalSource == originalTarget { return TernaryTrue } @@ -2659,6 +2746,24 @@ func (r *Relater) isRelatedToEx(originalSource *Type, originalTarget *Type, recu if source == target { return TernaryTrue } + // Compatibility of a bound is weaker than matching every argument it admits. + // Leave unresolved operations and mapped patterns unmatched: instantiation can + // change their shape or inference sites beyond what this structural matcher handles. + if r.relation == r.c.inferencePatternRelation { + if target.flags&TypeFlagsAnyOrUnknown != 0 { + // Top targets impose no matching requirement, even when the source is any. + return TernaryTrue + } + // any admits arguments that fail nontrivial patterns. Likewise, a structural + // object bound can admit primitives, so it does not establish the object keyword. + if source.flags&TypeFlagsAny != 0 || + target.flags&(TypeFlagsConditional|TypeFlagsIndexedAccess|TypeFlagsIndex|TypeFlagsTemplateLiteral|TypeFlagsStringMapping) != 0 || + source.flags&TypeFlagsConditional != 0 || + target.objectFlags&ObjectFlagsMapped != 0 || + source.flags&TypeFlagsObject != 0 && target.flags&TypeFlagsNonPrimitive != 0 { + return TernaryFalse + } + } if r.relation == r.c.identityRelation { if source.flags != target.flags { return TernaryFalse @@ -3120,20 +3225,19 @@ func (r *Relater) recursiveTypeRelatedTo(source *Type, target *Type, reportError // important to have well-defined behavior even in cases that shouldn't normally occur. return TernaryFalse } - id, constrained := getRelationKey(source, target, intersectionState, r.relation == r.c.identityRelation, false /*ignoreConstraints*/) + id, constrained := r.c.getRelationKey(source, target, intersectionState, r.relation, false /*ignoreConstraints*/) if entry := r.relation.get(id); entry != RelationComparisonResultNone { if reportErrors && entry&RelationComparisonResultFailed != 0 && entry&RelationComparisonResultOverflow == 0 { // We are elaborating errors and the cached result is a failure not due to a comparison overflow, // so we will do the comparison again to generate an error message. } else { - r.c.reliabilityFlags |= entry & (RelationComparisonResultReportsUnmeasurable | RelationComparisonResultReportsUnreliable) + // A cached pattern result must replay binder observations; otherwise an + // enclosing comparison could miss a conflicting use in the opposite variance. + result := r.loadCachedRelationResult(id, entry) if reportErrors && entry&RelationComparisonResultOverflow != 0 { r.reportError(diagnostics.Excessive_complexity_comparing_types_0_and_1, r.c.TypeToString(source), r.c.TypeToString(target)) } - if entry&RelationComparisonResultSucceeded != 0 { - return TernaryTrue - } - return TernaryFalse + return result } } if r.relationCount <= 0 { @@ -3142,14 +3246,24 @@ func (r *Relater) recursiveTypeRelatedTo(source *Type, target *Type, reportError } // If source and target are already being compared, consider them related with assumptions if r.maybeKeysSet.Has(id) { + // A structural cycle can close coinductively for assignment/subtyping, but + // assuming it matches could skip inference sites needed for pattern matching. + if r.relation == r.c.inferencePatternRelation { + return TernaryUnknown + } return TernaryMaybe } // A constrained key indicates that we have type references that reference constrained // type parameters. For such keys we also check against the key we would have gotten if all type parameters // were unconstrained. if constrained { - broadestEquivalentId, _ := getRelationKey(source, target, intersectionState, r.relation == r.c.identityRelation, true /*ignoreConstraints*/) + broadestEquivalentId, _ := r.c.getRelationKey(source, target, intersectionState, r.relation, true /*ignoreConstraints*/) if r.maybeKeysSet.Has(broadestEquivalentId) { + // An equivalent recursive comparison is still only an assumption, not + // evidence that every argument satisfies the pattern and its infer bounds. + if r.relation == r.c.inferencePatternRelation { + return TernaryUnknown + } return TernaryMaybe } } @@ -3157,7 +3271,8 @@ func (r *Relater) recursiveTypeRelatedTo(source *Type, target *Type, reportError // We stop relating if we reach 100 levels of nesting. This is a backstop to catch infinite recursion // that wasn't caught by isDeeplyNestedType. It will also stop relating types that truly are over 100 // levels deep, but those are exceedingly rare. - return TernaryMaybe + // Depth exhaustion must not become positive evidence of an irrefutable match. + return core.IfElse(r.relation == r.c.inferencePatternRelation, TernaryUnknown, TernaryMaybe) } maybeStart := len(r.maybeKeys) r.maybeKeys = append(r.maybeKeys, id) @@ -3177,18 +3292,62 @@ func (r *Relater) recursiveTypeRelatedTo(source *Type, target *Type, reportError } saveReliabilityFlags := r.c.reliabilityFlags r.c.reliabilityFlags = 0 + var inferenceBefore map[*Type]inferenceUsage + // Pattern-cache entries need the binder effects of this comparison, not just + // truth/reliability flags. Counters preserve effects already seen by the caller. + if r.relation == r.c.inferencePatternRelation { + inferenceBefore = maps.Clone(r.inferenceUsages) + } var result Ternary if r.expandingFlags == ExpandingFlagsBoth { if tr := r.c.tracer; tr != nil { tr.Instant(tracing.PhaseCheckTypes, "recursiveTypeRelatedTo_DepthLimit", map[string]any{"sourceId": source.id, "targetId": target.id, "depth": len(r.sourceStack), "targetDepth": len(r.targetStack)}) } - result = TernaryMaybe + // Expanding structures can justify a coinductive relation, but do not + // establish that all admitted arguments match an inference pattern. + result = core.IfElse(r.relation == r.c.inferencePatternRelation, TernaryUnknown, TernaryMaybe) } else { if tr := r.c.tracer; tr != nil { defer tr.Push(tracing.PhaseCheckTypes, "structuredTypeRelatedTo", map[string]any{"sourceId": source.id, "targetId": target.id}, false)() } result = r.structuredTypeRelatedTo(source, target, reportErrors, intersectionState) } + // Ordinary relations may commit assumed successes while unwinding recursion. + // Pattern matching must leave those results unknown and cache binder effects with + // definite results so a later cache hit cannot silently bypass inference consistency. + if r.relation == r.c.inferencePatternRelation { + if result == TernaryMaybe { + result = TernaryUnknown + } + if result == TernaryTrue || result == TernaryFalse { + var usages map[*Type]inferenceUsage + for parameter, usage := range r.inferenceUsages { + // Replay each observed direction once; multiplicity does not affect consistency. + var cachedUsage inferenceUsage + if usage.covariant != inferenceBefore[parameter].covariant { + cachedUsage.covariant = 1 + } + if usage.contravariant != inferenceBefore[parameter].contravariant { + cachedUsage.contravariant = 1 + } + if cachedUsage != (inferenceUsage{}) { + if usages == nil { + usages = make(map[*Type]inferenceUsage) + } + usages[parameter] = cachedUsage + } + } + if len(usages) != 0 { + if r.relation.inferenceUsages == nil { + r.relation.inferenceUsages = make(map[CacheHashKey]map[*Type]inferenceUsage) + } + r.relation.inferenceUsages[id] = usages + } + // Check only this pair's observations before caching its result. + // Uses from earlier siblings are not part of this pair's cache key. + result = checkInferencePatternResult(result, usages) + } + } propagatingVarianceFlags := r.c.reliabilityFlags r.c.reliabilityFlags |= saveReliabilityFlags if recursionFlags&RecursionFlagsSource != 0 { @@ -3199,7 +3358,9 @@ func (r *Relater) recursiveTypeRelatedTo(source *Type, target *Type, reportError } r.expandingFlags = saveExpandingFlags if result != TernaryFalse { - if result == TernaryTrue || (len(r.sourceStack) == 0 && len(r.targetStack) == 0) { + // Unresolved pattern comparisons must discard pending keys immediately; a successful + // ancestor must not promote an unchecked comparison into a cached success. + if result == TernaryTrue || (len(r.sourceStack) == 0 && len(r.targetStack) == 0) || r.relation == r.c.inferencePatternRelation { if result == TernaryTrue || result == TernaryMaybe { // If result is definitely true, record all maybe keys as having succeeded. Also, record Ternary.Maybe // results as having succeeded once we reach depth 0, but never record Ternary.Unknown results. @@ -3444,7 +3605,9 @@ func (r *Relater) structuredTypeRelatedToWorker(source *Type, target *Type, repo // We limit alias variance probing to only object and conditional types since their alias behavior // is more predictable than other, interned types, which may or may not have an alias depending on // the order in which things were checked. - if source.flags&(TypeFlagsObject|TypeFlagsConditional) != 0 && source.alias != nil && len(source.alias.typeArguments) != 0 && + // Alias variances describe ordinary type arguments, not existential binders. + // Inspect the body to observe each infer site's variance and detect conflicts. + if r.relation != r.c.inferencePatternRelation && source.flags&(TypeFlagsObject|TypeFlagsConditional) != 0 && source.alias != nil && len(source.alias.typeArguments) != 0 && target.alias != nil && source.alias.symbol == target.alias.symbol && !(r.c.isMarkerType(source) || r.c.isMarkerType(target)) { variances := r.c.getAliasVariances(source.alias.symbol) if len(variances) == 0 { @@ -3598,31 +3761,9 @@ func (r *Relater) structuredTypeRelatedToWorker(source *Type, target *Type, repo if r.c.isDeeplyNestedType(target, r.targetStack, 10) { return TernaryMaybe } - c := target.AsConditionalType() - // We check for a relationship to a conditional type target only when the conditional type has no - // 'infer' positions, is not distributive or is distributive but doesn't reference the check type - // parameter in either of the result types, and the source isn't an instantiation of the same - // conditional type (as happens when computing variance). - if c.root.inferTypeParameters == nil && !r.c.isDistributionDependent(c.root) && !(source.flags&TypeFlagsConditional != 0 && source.AsConditionalType().root == c.root) { - // Check if the conditional is always true or always false but still deferred for distribution purposes. - skipTrue := !r.c.isTypeAssignableTo(r.c.getPermissiveInstantiation(c.checkType), r.c.getPermissiveInstantiation(c.extendsType)) - skipFalse := !skipTrue && r.c.isTypeAssignableTo(r.c.getRestrictiveInstantiation(c.checkType), r.c.getRestrictiveInstantiation(c.extendsType)) - // TODO: Find a nice way to include potential conditional type breakdowns in error output, if they seem good (they usually don't) - if skipTrue { - result = TernaryTrue - } else { - result = r.isRelatedToEx(source, r.c.getTrueTypeFromConditionalType(target), RecursionFlagsTarget, false /*reportErrors*/, nil /*headMessage*/, intersectionState) - } - if result != TernaryFalse { - if skipFalse { - result &= TernaryTrue - } else { - result &= r.isRelatedToEx(source, r.c.getFalseTypeFromConditionalType(target), RecursionFlagsTarget, false /*reportErrors*/, nil /*headMessage*/, intersectionState) - } - if result != TernaryFalse { - return result - } - } + result = r.typeRelatedToConditionalType(source, target, intersectionState) + if result != TernaryFalse { + return result } case target.flags&TypeFlagsTemplateLiteral != 0: if source.flags&TypeFlagsTemplateLiteral != 0 { @@ -3873,7 +4014,9 @@ func (r *Relater) structuredTypeRelatedToWorker(source *Type, target *Type, repo return TernaryFalse } switch { - case source.objectFlags&ObjectFlagsReference != 0 && target.objectFlags&ObjectFlagsReference != 0 && source.Target() == target.Target() && !isTupleType(source) && !r.c.isMarkerType(source) && !r.c.isMarkerType(target): + // Same-target argument comparison can hide mixed infer positions inside + // members; inference-pattern comparisons must traverse those members instead. + case r.relation != r.c.inferencePatternRelation && source.objectFlags&ObjectFlagsReference != 0 && target.objectFlags&ObjectFlagsReference != 0 && source.Target() == target.Target() && !isTupleType(source) && !r.c.isMarkerType(source) && !r.c.isMarkerType(target): // When strictNullChecks is disabled, the element type of the empty array literal is undefinedWideningType, // and an empty array literal wouldn't be assignable to a `never[]` without this check. if r.c.isEmptyArrayLiteralType(source) { @@ -4284,7 +4427,11 @@ func (r *Relater) propertiesRelatedTo(source *Type, target *Type, reportErrors b return TernaryFalse } } - requireOptionalProperties := (r.relation == r.c.subtypeRelation || r.relation == r.c.strictSubtypeRelation) && !isObjectLiteralType(source) && !r.c.isEmptyArrayLiteralType(source) && !isTupleType(source) + // An optional property omitted by a bound can be added with an incompatible + // type by an admitted argument. Require it even for fresh objects and tuples, + // where assignment/subtyping can otherwise allow omission. + requireOptionalProperties := r.relation == r.c.inferencePatternRelation || + (r.relation == r.c.subtypeRelation || r.relation == r.c.strictSubtypeRelation) && !isObjectLiteralType(source) && !r.c.isEmptyArrayLiteralType(source) && !isTupleType(source) unmatchedProperty := r.c.getUnmatchedProperty(source, target, requireOptionalProperties, false /*matchDiscriminantProperties*/) if unmatchedProperty != nil { if reportErrors && r.c.shouldReportUnmatchedPropertyError(source, target) { @@ -4498,7 +4645,9 @@ func (r *Relater) signaturesRelatedTo(source *Type, target *Type, kind Signature return r.signaturesIdenticalTo(source, target, kind) } // With respect to signatures, the anyFunctionType wildcard is a subtype of every other function type. - if source == r.c.anyFunctionType { + // That compatibility wildcard supplies no concrete signature guaranteeing that + // every admitted argument matches an inference pattern. + if source == r.c.anyFunctionType && r.relation != r.c.inferencePatternRelation { return TernaryTrue } if target == r.c.anyFunctionType { @@ -4602,6 +4751,45 @@ func (r *Relater) signatureRelatedTo(source *Signature, target *Signature, erase checkMode = SignatureCheckModeStrictTopSignature case r.relation == r.c.strictSubtypeRelation: checkMode = SignatureCheckModeStrictTopSignature | SignatureCheckModeStrictArity + // Assignment/subtyping compare the displayed signatures. A pattern match must + // remain valid with narrowed parameter domains and added overloads in admitted arguments. + case r.relation == r.c.inferencePatternRelation: + // Optional/rest domains, this parameters, and predicates need additional + // universal reasoning; ordinary signature compatibility is not sufficient. + if r.c.hasEffectiveRestParameter(target) || r.c.getThisTypeOfSignature(target) != nil || r.c.getTypePredicateOfSignature(target) != nil || + r.c.getMinArgumentCount(target) != r.c.getParameterCount(target) { + return TernaryFalse + } + // Do not infer source generics from the target as assignment does: that + // could introduce the target's existential binders into the source. + source = r.c.getBaseSignature(source) + for i := range r.c.getParameterCount(target) { + parameterType := r.c.getTypeAtPosition(target, i) + if isInferencePatternParameter(parameterType) || parameterType.flags&TypeFlagsUnknown != 0 { + if isInferencePatternParameter(parameterType) { + r.recordInferenceUsage(parameterType, inferenceUsage{contravariant: 1}) + } + // An ignored argument accepts every domain. Otherwise only a strict, + // non-bivariant unknown bound prevents admitted functions narrowing it. + if i < r.c.getParameterCount(source) && (!r.c.strictFunctionTypes || isBivariantSignature(source) || + r.c.getTypeAtPosition(source, i).flags&TypeFlagsUnknown == 0) { + return TernaryFalse + } + } else if parameterType.flags&(TypeFlagsAny|TypeFlagsNever) == 0 { + return TernaryFalse + } else if parameterType.flags&TypeFlagsAny != 0 && i < r.c.getParameterCount(source) && + r.c.strictFunctionTypes && isBivariantSignature(source) && !isBivariantSignature(target) { + // A bivariant method bound admits a never parameter, which cannot + // satisfy an any parameter in a strict function pattern. + return TernaryFalse + } + } + // An admissible type can add overloads that drive inference. The signature + // guaranteed by the constraint must work for any resulting infer bindings. + target = r.c.instantiateSignature(target, r.c.rigidInferenceMapper) + // Admitted functions can make optional source parameters required, so check + // total arity, not assignment's minimum arity. Domains were validated above. + checkMode = SignatureCheckModeStrictTopSignature | SignatureCheckModeStrictArity | SignatureCheckModeIgnoreParameterTypes } if erase { source = r.c.getErasedSignature(source) @@ -4640,9 +4828,13 @@ func (r *Relater) indexSignaturesRelatedTo(source *Type, target *Type, sourceIsP for _, targetInfo := range indexInfos { var related Ternary switch { - case r.relation != r.c.strictSubtypeRelation && !sourceIsPrimitive && targetHasStringIndex && targetInfo.valueType.flags&TypeFlagsAny != 0: + // An any-valued index shortcut does not guarantee that every argument + // admitted by a structural bound actually has the required index signature. + case r.relation != r.c.strictSubtypeRelation && r.relation != r.c.inferencePatternRelation && !sourceIsPrimitive && targetHasStringIndex && targetInfo.valueType.flags&TypeFlagsAny != 0: related = TernaryTrue - case r.c.isGenericMappedType(source) && targetHasStringIndex: + // Comparing only a mapped template skips the key domain; it cannot establish + // a string index signature on every admitted instantiation. + case r.relation != r.c.inferencePatternRelation && r.c.isGenericMappedType(source) && targetHasStringIndex: related = r.isRelatedTo(r.c.getTemplateTypeFromMappedType(source), targetInfo.valueType, RecursionFlagsBoth, reportErrors) default: related = r.typeRelatedToIndexInfo(source, targetInfo, reportErrors, intersectionState) @@ -4663,7 +4855,10 @@ func (r *Relater) typeRelatedToIndexInfo(source *Type, targetInfo *IndexInfo, re // Intersection constituents are never considered to have an inferred index signature. Also, in the strict subtype relation, // only fresh object literals are considered to have inferred index signatures. This ensures { [x: string]: xxx } <: {} but // not vice-versa. Without this rule, those types would be mutual strict subtypes. - if intersectionState&IntersectionStateSource == 0 && (r.relation != r.c.strictSubtypeRelation || source.objectFlags&ObjectFlagsFreshLiteral != 0) && r.c.isObjectTypeWithInferableIndex(source) { + // An index signature inferred from compatible known members permits assignment + // of this object, but admitted arguments can add incompatible members. + // Pattern matching therefore requires an explicit source index signature. + if r.relation != r.c.inferencePatternRelation && intersectionState&IntersectionStateSource == 0 && (r.relation != r.c.strictSubtypeRelation || source.objectFlags&ObjectFlagsFreshLiteral != 0) && r.c.isObjectTypeWithInferableIndex(source) { return r.membersRelatedToIndexInfo(source, targetInfo, reportErrors, intersectionState) } if reportErrors { @@ -5046,8 +5241,150 @@ func (c *Checker) isTypeDerivedFrom(source *Type, target *Type) bool { } } -func (c *Checker) isDistributionDependent(root *ConditionalRoot) bool { - return root.isDistributive && (c.isTypeParameterPossiblyReferenced(root.checkType, root.node.TrueType) || c.isTypeParameterPossiblyReferenced(root.checkType, root.node.FalseType)) +// These types cannot acquire a top-level union or become never through instantiation. +// Homomorphic mapped types and variadic tuples are exceptions to the object case. +func isFixedConditionalCheckType(t *Type) bool { + return t.flags&(TypeFlagsAnyOrUnknown|TypeFlagsPrimitive|TypeFlagsNonPrimitive) != 0 && t.flags&(TypeFlagsTemplateLiteral|TypeFlagsStringMapping) == 0 || + t.flags&TypeFlagsObject != 0 && t.objectFlags&ObjectFlagsMapped == 0 && !isGenericTupleType(t) +} + +func (c *Checker) isDefinitelyNonNeverType(t *Type, depth int) bool { + if isFixedConditionalCheckType(t) { + return true + } + if depth == 10 { + return false + } + isNonNever := func(t *Type) bool { return c.isDefinitelyNonNeverType(t, depth+1) } + switch { + case t.flags&TypeFlagsUnion != 0: + return core.Some(t.Types(), isNonNever) + case t.flags&TypeFlagsConditional != 0: + d := t.AsConditionalType() + return (!d.root.isDistributive || isNonNever(d.checkType)) && + isNonNever(c.getTrueTypeFromConditionalType(t)) && isNonNever(c.getFalseTypeFromConditionalType(t)) + case t.flags&TypeFlagsTemplateLiteral != 0: + return core.Every(t.AsTemplateLiteralType().types, isNonNever) + case t.flags&TypeFlagsStringMapping != 0: + return isNonNever(t.AsStringMappingType().target) + } + return false +} + +func (r *Relater) typeRelatedToConditionalType(source *Type, target *Type, intersectionState IntersectionState) Ternary { + d := target.AsConditionalType() + // In particular, do not use branch-wise reasoning to establish alias variance. + if source.flags&TypeFlagsConditional != 0 && source.AsConditionalType().root == d.root { + return TernaryFalse + } + trueSource, falseSource := source, source + var isValidSource func() bool + mapper := d.mapper + checkType, extendsType := d.checkType, d.extendsType + // Preserve branch-wise comparison for independent branches, but inferred + // results must still account for an empty distributive check type. + if d.root.isDistributive && !isFixedConditionalCheckType(d.checkType) && + (len(d.root.inferTypeParameters) != 0 && !r.c.isDefinitelyNonNeverType(d.checkType, 0) || + r.c.isTypeParameterPossiblyReferenced(d.root.checkType, d.root.node.TrueType) || + r.c.isTypeParameterPossiblyReferenced(d.root.checkType, d.root.node.FalseType)) { + if d.resolvedDistributiveCheckType == nil { + d.resolvedDistributiveCheckType = r.c.newTypeParameter(nil) + d.resolvedDistributiveCheckType.AsTypeParameter().constraint = d.checkType + } + constituent := d.resolvedDistributiveCheckType + sourceCheckType := getNonDistributedTypeParameter(d.checkType) + type sourceMapperState struct { + occurrences int + invalidations int + } + var state sourceMapperState + sourceMapper := newFunctionTypeMapper(func(t *Type) *Type { + if getNonDistributedTypeParameter(t) == sourceCheckType { + state.occurrences++ + return constituent + } + if t.flags&TypeFlagsConditional != 0 { + conditional := t.AsConditionalType() + if !conditional.root.isDistributive || getNonDistributedTypeParameter(conditional.checkType) != sourceCheckType { + state.invalidations++ + } + } + return t + }, func() any { + return state + }, func(before, after any) { + previous := before.(sourceMapperState) + current := after.(sourceMapperState) + // Only zero, one, and multiple occurrences matter to this distributivity check. + state.occurrences += min(2, current.occurrences-previous.occurrences) + state.invalidations += min(1, current.invalidations-previous.invalidations) + }) + falseSource = r.c.instantiateType(source, sourceMapper) + isValidSource = func() bool { return state.occurrences == 1 && state.invalidations == 0 } + distributedSource := isValidSource() && + r.c.isTypeAssignableTo(r.c.instantiateType(falseSource, newSimpleTypeMapper(constituent, r.c.neverType)), r.c.neverType) && + isValidSource() + if !distributedSource { + return TernaryFalse + } + checkType = constituent + mapper = prependTypeMapping(d.root.checkType, checkType, mapper) + extendsType = r.c.instantiateType(d.root.extendsType, mapper) + trueSource = r.c.instantiateType(falseSource, newSimpleTypeMapper(checkType, r.c.getSubstitutionType(checkType, extendsType))) + } + // Inferred parameters remain universally quantified in the result type. An + // irrefutable inference pattern justifies checking only the true branch, but + // must not make a narrower inferred result writable through its constraint. + skipTrue := !r.c.isTypeAssignableTo(r.c.getPermissiveInstantiation(checkType), r.c.getPermissiveInstantiation(extendsType)) + skipFalse := !skipTrue && (r.c.isTypeAssignableTo(r.c.getRestrictiveInstantiation(checkType), r.c.getRestrictiveInstantiation(extendsType)) || + len(d.root.inferTypeParameters) != 0 && r.c.isConditionalInferencePatternIrrefutable(checkType, d, mapper)) + result := TernaryTrue + if !skipTrue { + trueType := r.c.instantiateType(r.c.getTypeFromTypeNode(d.root.node.TrueType), mapper) + result = r.isRelatedToEx(trueSource, trueType, RecursionFlagsBoth, false /*reportErrors*/, nil /*headMessage*/, intersectionState) + } + if result != TernaryFalse && !skipFalse { + falseType := r.c.instantiateType(r.c.getTypeFromTypeNode(d.root.node.FalseType), mapper) + result &= r.isRelatedToEx(falseSource, falseType, RecursionFlagsBoth, false /*reportErrors*/, nil /*headMessage*/, intersectionState) + } + if isValidSource != nil && !isValidSource() { + return TernaryFalse + } + return result +} + +// The inference-pattern relation checks whether every admissible source instantiation +// matches a target pattern, allowing its infer parameters to vary between matches. +// This identifies a lower bound for a conditional type's true branch. Unlike +// assignment/subtyping, it rejects compatibility coercions and unstable input +// domains but can match marked infer parameters. +// +// This conservative pattern-matching relation is not transitive: replacing a never type +// parameter with never can erase an inference observation. Do not chain successful matches +// or use them for variance/type reduction. Without binders, the intended relationship is +// inclusion of admissible-type sets, a transitive preorder. +func (c *Checker) isConditionalInferencePatternIrrefutable(source *Type, target *ConditionalType, mapper *TypeMapper) bool { + if !target.inferencePatternResolved { + target.inferencePatternResolved = true + parameters := core.Map(target.root.inferTypeParameters, c.cloneTypeParameter) + inferenceMapper := c.combineTypeMappers(newTypeMapper(target.root.inferTypeParameters, parameters), mapper) + inferenceMapper = c.combineTypeMappers(inferenceMapper, c.rigidInferenceMapper) + for _, parameter := range parameters { + parameter.AsTypeParameter().mapper = inferenceMapper + } + pattern := c.instantiateType(c.getTypeFromTypeNode(target.root.node.ExtendsType), inferenceMapper) + // Resolve captured outer binders before introducing this pattern's marked clones. + binders := core.Map(parameters, c.cloneTypeParameter) + binderMapper := newTypeMapper(parameters, binders) + for _, binder := range binders { + binder.AsTypeParameter().isInferencePattern = true + binder.AsTypeParameter().mapper = binderMapper + } + target.resolvedInferencePattern = c.instantiateType(pattern, binderMapper) + } + // Assignment of the instantiated bound alone does not establish that all + // admitted arguments match; use the inference-pattern relation and its infer-usage checks. + return target.resolvedInferencePattern != nil && c.isTypeRelatedTo(source, target.resolvedInferencePattern, c.inferencePatternRelation) } func (r *Relater) traceUnionsOrIntersectionsTooLarge(source *Type, target *Type) { diff --git a/tsc/internal/checker/types.go b/tsc/internal/checker/types.go index 36400b0fd3c70..471340ffb84a7 100644 --- a/tsc/internal/checker/types.go +++ b/tsc/internal/checker/types.go @@ -1498,8 +1498,10 @@ type TypeParameter struct { mapper *TypeMapper isThisType bool isDistributed bool + isInferencePattern bool resolvedDefaultType *Type distributedType *Type + rigidInferenceType *Type } func (t *TypeParameter) IsThisType() bool { return t.isThisType } @@ -1583,6 +1585,9 @@ type ConditionalType struct { resolvedInferredTrueType *Type // The `trueType` instantiated with the `combinedMapper`, if present resolvedDefaultConstraint *Type resolvedConstraintOfDistributive *Type + resolvedDistributiveCheckType *Type + resolvedInferencePattern *Type + inferencePatternResolved bool mapper *TypeMapper combinedMapper *TypeMapper } diff --git a/tsc/internal/checker/types_test.go b/tsc/internal/checker/types_test.go index 3cf3588b7c32c..a28aa1cbfb9fd 100644 --- a/tsc/internal/checker/types_test.go +++ b/tsc/internal/checker/types_test.go @@ -417,12 +417,17 @@ func TestTypeMapperDispatch(t *testing.T) { return target } return typ - }), TypeMapperKindFunction, target}, + }, nil, nil), TypeMapperKindFunction, target}, {newCompositeTypeMapper(c, newSimpleTypeMapper(other, target), simple), TypeMapperKindComposite, target}, } { assert.Equal(t, test.mapper.Kind(), test.kind) assert.Equal(t, test.mapper.Map(source), test.result) assert.Assert(t, !test.mapper.MapsThisOnly()) + var state TypeMapperState + assert.Assert(t, !test.mapper.Store(nil)) + assert.Assert(t, !test.mapper.Store(&state)) + assert.Equal(t, len(state), 0) + test.mapper.Load(nil, nil) } assert.Equal(t, compareTypeMappers(simple, newSimpleTypeMapper(source, target)), 0) assert.Equal(t, compareTypeMappers(array, newArrayTypeMapper([]*Type{source, other}, []*Type{target, source})), 0) @@ -457,6 +462,461 @@ func TestCompactTypeMapperLayout(t *testing.T) { runtime.KeepAlive(c) } +func TestStrictSubtypeDoesNotProveConditionalInference(t *testing.T) { + t.Parallel() + c, primitives := newCompositeTestChecker() + c.strictFunctionTypes = true + c.anyType = c.newIntrinsicType(TypeFlagsAny, "any") + c.assignableRelation = &Relation{} + c.strictSubtypeRelation = &Relation{} + c.identityRelation = &Relation{} + c.emptyObjectType = c.newAnonymousType(nil, nil, nil, nil, nil) + c.globalStringType = c.emptyObjectType + c.globalNumberType = c.emptyObjectType + c.getGlobalNonNullableTypeAliasOrNil = func() *ast.Symbol { return nil } + functionType := func(parameterType *Type) *Type { + parameter := c.createSymbolWithType(c.newSymbol(ast.SymbolFlagsFunctionScopedVariable, "value"), parameterType) + signature := c.newSignature(SignatureFlagsNone, nil, nil, nil, []*ast.Symbol{parameter}, c.unknownType, nil, 1) + return c.newAnonymousType(nil, nil, []*Signature{signature}, nil, nil) + } + constraint := functionType(c.anyType) + pattern := functionType(primitives[0]) + instantiation := functionType(primitives[1]) + assert.Assert(t, c.isTypeStrictSubtypeOf(constraint, pattern)) + assert.Assert(t, c.isTypeAssignableTo(instantiation, constraint)) + assert.Assert(t, !c.isTypeStrictSubtypeOf(instantiation, constraint)) + assert.Assert(t, !c.isTypeAssignableTo(instantiation, pattern)) +} + +func newInferenceRelationTestChecker() (*Checker, []*Type) { + c, primitives := newCompositeTestChecker() + c.strictFunctionTypes = true + c.couldContainTypeVariables = c.couldContainTypeVariablesWorker + c.cachedTypes = make(map[CachedTypeKey]*Type) + c.anyType = c.newIntrinsicType(TypeFlagsAny, "any") + c.voidType = c.newIntrinsicType(TypeFlagsVoid, "void") + c.undefinedType = c.newIntrinsicType(TypeFlagsUndefined, "undefined") + c.nullType = c.newIntrinsicType(TypeFlagsNull, "null") + c.noConstraintType = c.newAnonymousType(nil, nil, nil, nil, nil) + c.circularConstraintType = c.newAnonymousType(nil, nil, nil, nil, nil) + c.emptyObjectType = c.newAnonymousType(nil, nil, nil, nil, nil) + c.emptyTypeLiteralType = c.emptyObjectType + c.globalStringType = c.emptyObjectType + c.globalNumberType = c.emptyObjectType + c.getGlobalNonNullableTypeAliasOrNil = func() *ast.Symbol { return nil } + c.assignableRelation = &Relation{} + c.subtypeRelation = &Relation{} + c.strictSubtypeRelation = &Relation{} + c.identityRelation = &Relation{} + c.comparableRelation = &Relation{} + c.inferencePatternRelation = &Relation{} + c.rigidInferenceMapper = newFunctionTypeMapper(c.rigidInferenceMapperWorker, nil, nil) + return c, primitives +} + +func newInferencePatternTestParameter(c *Checker) *Type { + parameter := c.cloneTypeParameter(c.newTypeParameter(nil)) + parameter.AsTypeParameter().isInferencePattern = true + return parameter +} + +func TestInferencePatternRelationContext(t *testing.T) { + t.Parallel() + c, primitives := newInferenceRelationTestChecker() + first := newInferencePatternTestParameter(c) + second := newInferencePatternTestParameter(c) + relation := c.inferencePatternRelation + assert.Assert(t, c.isTypeRelatedTo(primitives[0], first, relation)) + assert.Assert(t, !c.isTypeRelatedTo(primitives[0], first.AsTypeParameter().target, relation)) + assert.Assert(t, !c.isTypeRelatedTo(primitives[0], first, c.assignableRelation)) + // Captured binders are rigid, even though all comparisons use the same relation. + rigidFirst := c.instantiateType(first, c.rigidInferenceMapper) + assert.Assert(t, !isInferencePatternParameter(rigidFirst)) + assert.Equal(t, c.instantiateType(first, c.rigidInferenceMapper), rigidFirst) + assert.Assert(t, c.isTypeRelatedTo(first, rigidFirst, relation)) + assert.Assert(t, c.isTypeRelatedTo(c.unknownType, first, relation)) + assert.Assert(t, c.isTypeRelatedTo(first, second, relation)) + assert.Assert(t, !c.isTypeRelatedTo(c.unknownType, c.instantiateType(second, c.rigidInferenceMapper), relation)) +} + +func TestInferencePatternRelationRigidConstraints(t *testing.T) { + t.Parallel() + c, primitives := newInferenceRelationTestChecker() + outer := newInferencePatternTestParameter(c) + inner := newInferencePatternTestParameter(c) + inner.AsTypeParameter().constraint = outer + rigidInner := c.instantiateType(inner, c.rigidInferenceMapper) + rigidOuter := c.instantiateType(outer, c.rigidInferenceMapper) + assert.Equal(t, c.getConstraintOfTypeParameter(rigidInner), rigidOuter) + assert.Assert(t, !isInferencePatternParameter(rigidOuter)) + assert.Assert(t, c.isTypeRelatedTo(inner, rigidOuter, c.inferencePatternRelation)) + assert.Assert(t, c.isTypeRelatedTo(outer, inner, c.inferencePatternRelation)) + assert.Assert(t, !c.isTypeRelatedTo(primitives[0], inner, c.inferencePatternRelation)) + assert.Assert(t, !c.isTypeRelatedTo(primitives[1], inner, c.inferencePatternRelation)) + assert.Assert(t, c.isTypeRelatedTo(c.neverType, inner, c.inferencePatternRelation)) +} + +func TestInferencePatternRelationCacheEffects(t *testing.T) { + t.Parallel() + c, primitives := newInferenceRelationTestChecker() + inferred := newInferencePatternTestParameter(c) + relation := c.inferencePatternRelation + objectType := func(properties map[string]*Type) *Type { + members := ast.NewSymbolTable() + for name, typ := range properties { + members.Set(name, c.createSymbolWithType(c.newSymbol(ast.SymbolFlagsProperty, name), typ)) + } + return c.newAnonymousType(nil, members, nil, nil, nil) + } + functionType := func(parameterType *Type) *Type { + parameter := c.createSymbolWithType(c.newSymbol(ast.SymbolFlagsFunctionScopedVariable, "value"), parameterType) + signature := c.newSignature(SignatureFlagsNone, nil, nil, nil, []*ast.Symbol{parameter}, c.unknownType, nil, 1) + return c.newAnonymousType(nil, nil, []*Signature{signature}, nil, nil) + } + source := objectType(map[string]*Type{"value": primitives[0]}) + target := objectType(map[string]*Type{"value": inferred}) + // Record an effect even when that polarity was already observed by the caller. + r := c.getRelater() + r.relation = relation + r.relationCount = 1_000_000 + r.inferenceUsages = map[*Type]inferenceUsage{inferred: {covariant: 1}} + assert.Equal(t, r.isRelatedToSimple(source, target), TernaryTrue) + assert.Equal(t, r.inferenceUsages[inferred].covariant, 2) + c.putRelater(r) + assert.Equal(t, len(r.inferenceUsages), 0) + key, _ := c.getRelationKey(source, target, IntersectionStateNone, relation, false) + assert.Assert(t, relation.get(key)&RelationComparisonResultSucceeded != 0) + assert.Equal(t, relation.inferenceUsages[key][inferred], inferenceUsage{covariant: 1}) + instantiationProbes := 0 + couldContainTypeVariables := c.couldContainTypeVariables + c.couldContainTypeVariables = func(typ *Type) bool { + instantiationProbes++ + return couldContainTypeVariables(typ) + } + relation.set(key, relation.get(key)|RelationComparisonResultReportsUnreliable) + c.reliabilityFlags = 0 + freeRelater := c.freeRelater + c.freeRelater = nil + assert.Assert(t, c.isTypeRelatedTo(source, target, relation)) + assert.Assert(t, c.freeRelater == nil) + c.freeRelater = freeRelater + assert.Equal(t, instantiationProbes, 0) + assert.Assert(t, c.reliabilityFlags&RelationComparisonResultReportsUnreliable != 0) + mixedSource := objectType(map[string]*Type{"child": source, "input": functionType(c.unknownType)}) + mixedTarget := objectType(map[string]*Type{"child": target, "input": functionType(inferred)}) + assert.Assert(t, !c.isTypeRelatedTo(mixedSource, mixedTarget, relation)) + mixedKey, _ := c.getRelationKey(mixedSource, mixedTarget, IntersectionStateNone, relation, false) + assert.Assert(t, relation.get(mixedKey)&RelationComparisonResultFailed != 0) + assert.Equal(t, relation.inferenceUsages[mixedKey][inferred], inferenceUsage{covariant: 1, contravariant: 1}) + probesBeforeCacheHit := instantiationProbes + freeRelater = c.freeRelater + c.freeRelater = nil + assert.Assert(t, !c.isTypeRelatedTo(mixedSource, mixedTarget, relation)) + assert.Assert(t, c.freeRelater == nil) + c.freeRelater = freeRelater + assert.Equal(t, instantiationProbes, probesBeforeCacheHit) + second := newInferencePatternTestParameter(c) + secondTarget := objectType(map[string]*Type{"value": second}) + assert.Assert(t, c.isTypeRelatedTo(source, secondTarget, relation)) + secondKey, _ := c.getRelationKey(source, secondTarget, IntersectionStateNone, relation, false) + assert.Equal(t, relation.inferenceUsages[secondKey][second], inferenceUsage{covariant: 1}) + assert.Equal(t, relation.inferenceUsages[secondKey][inferred], inferenceUsage{}) + independentTarget := objectType(map[string]*Type{"child": target, "input": functionType(second)}) + assert.Assert(t, c.isTypeRelatedTo(mixedSource, independentTarget, relation)) + secondMixedTarget := objectType(map[string]*Type{"child": secondTarget, "input": functionType(second)}) + assert.Assert(t, !c.isTypeRelatedTo(mixedSource, secondMixedTarget, relation)) + + childSource := objectType(map[string]*Type{"value": primitives[0]}) + childTarget := objectType(map[string]*Type{"value": inferred}) + r = c.getRelater() + r.relation = relation + r.relationCount = 1_000_000 + r.inferenceUsages = map[*Type]inferenceUsage{inferred: {contravariant: 1}} + result := r.isRelatedToSimple(childSource, childTarget) + assert.Equal(t, result, TernaryTrue) + assert.Equal(t, checkInferencePatternResult(result, r.inferenceUsages), TernaryFalse) + c.putRelater(r) + childKey, _ := c.getRelationKey(childSource, childTarget, IntersectionStateNone, relation, false) + assert.Assert(t, relation.get(childKey)&RelationComparisonResultSucceeded != 0) + assert.Equal(t, relation.inferenceUsages[childKey][inferred], inferenceUsage{covariant: 1}) + assert.Assert(t, c.isTypeRelatedTo(childSource, childTarget, relation)) +} + +func TestInferencePatternRelationGenericCacheKeys(t *testing.T) { + t.Parallel() + c, _ := newInferenceRelationTestChecker() + sourceParameter := c.newTypeParameter(nil) + first := newInferencePatternTestParameter(c) + second := newInferencePatternTestParameter(c) + generic := c.newObjectType(ObjectFlagsInterface, nil) + generic.AsInterfaceType().instantiations = make(map[CacheHashKey]*Type) + source := c.createTypeReference(generic, []*Type{sourceParameter}) + firstTarget := c.createTypeReference(generic, []*Type{first}) + secondTarget := c.createTypeReference(generic, []*Type{second}) + firstKey, _ := c.getRelationKey(source, firstTarget, IntersectionStateNone, c.assignableRelation, false) + secondKey, _ := c.getRelationKey(source, secondTarget, IntersectionStateNone, c.assignableRelation, false) + assert.Equal(t, firstKey, secondKey) + firstKey, _ = c.getRelationKey(source, firstTarget, IntersectionStateNone, c.inferencePatternRelation, false) + secondKey, _ = c.getRelationKey(source, secondTarget, IntersectionStateNone, c.inferencePatternRelation, false) + assert.Assert(t, firstKey != secondKey) +} + +func TestInferencePatternRelationUnknownIsNotCached(t *testing.T) { + t.Parallel() + c, _ := newInferenceRelationTestChecker() + relation := c.inferencePatternRelation + source := c.newAnonymousType(nil, nil, nil, nil, nil) + target := c.newAnonymousType(nil, nil, nil, nil, nil) + r := c.getRelater() + r.relation = relation + r.relationCount = 1_000_000 + r.sourceStack = []*Type{c.emptyObjectType} + r.targetStack = []*Type{c.emptyObjectType} + r.expandingFlags = ExpandingFlagsBoth + assert.Equal(t, r.recursiveTypeRelatedTo(source, target, false, IntersectionStateNone, RecursionFlagsNone), TernaryUnknown) + assert.Equal(t, len(r.maybeKeys), 0) + key, _ := c.getRelationKey(source, target, IntersectionStateNone, relation, false) + assert.Assert(t, !r.maybeKeysSet.Has(key)) + assert.Equal(t, relation.get(key), RelationComparisonResultNone) + c.putRelater(r) +} + +func TestInferencePatternRelationGroundEntailment(t *testing.T) { + t.Parallel() + c, primitives := newInferenceRelationTestChecker() + relation := c.inferencePatternRelation + objectType := func(properties map[string]*Type) *Type { + members := ast.NewSymbolTable() + for name, typ := range properties { + members.Set(name, c.createSymbolWithType(c.newSymbol(ast.SymbolFlagsProperty, name), typ)) + } + return c.newAnonymousType(nil, members, nil, nil, nil) + } + first := objectType(map[string]*Type{"value": primitives[0], "extra": primitives[1]}) + second := objectType(map[string]*Type{"value": primitives[0]}) + third := objectType(map[string]*Type{"value": c.unknownType}) + assert.Assert(t, c.isTypeRelatedTo(first, second, relation)) + assert.Assert(t, c.isTypeRelatedTo(second, third, relation)) + assert.Assert(t, c.isTypeRelatedTo(first, third, relation)) +} + +func TestInferencePatternRelationIsNotTransitive(t *testing.T) { + t.Parallel() + c, _ := newInferenceRelationTestChecker() + inferred := newInferencePatternTestParameter(c) + empty := c.newTypeParameter(nil) + empty.AsTypeParameter().constraint = c.neverType + relation := c.inferencePatternRelation + functionType := func(parameterType *Type) *Type { + parameter := c.createSymbolWithType(c.newSymbol(ast.SymbolFlagsFunctionScopedVariable, "value"), parameterType) + signature := c.newSignature(SignatureFlagsNone, nil, nil, nil, []*ast.Symbol{parameter}, c.unknownType, nil, 1) + return c.newAnonymousType(nil, nil, []*Signature{signature}, nil, nil) + } + objectType := func(valueType *Type, inputType *Type) *Type { + members := ast.NewSymbolTable() + members.Set("value", c.createSymbolWithType(c.newSymbol(ast.SymbolFlagsProperty, "value"), valueType)) + members.Set("input", c.createSymbolWithType(c.newSymbol(ast.SymbolFlagsProperty, "input"), inputType)) + return c.newAnonymousType(nil, members, nil, nil, nil) + } + input := functionType(c.unknownType) + first := objectType(empty, input) + second := objectType(c.neverType, input) + third := objectType(inferred, functionType(inferred)) + assert.Assert(t, c.isTypeRelatedTo(first, second, relation)) + assert.Assert(t, c.isTypeRelatedTo(second, third, relation)) + assert.Assert(t, !c.isTypeRelatedTo(first, third, relation)) +} + +type functionMapperCacheTestState struct { + occurrences int + invalidations int +} + +func newFunctionMapperCacheTest(source *Type, target *Type) (*TypeMapper, *functionMapperCacheTestState) { + state := &functionMapperCacheTestState{} + mapper := newFunctionTypeMapper(func(typ *Type) *Type { + if typ == source { + state.occurrences++ + return target + } + return typ + }, func() any { + return *state + }, func(before, after any) { + previous := before.(functionMapperCacheTestState) + current := after.(functionMapperCacheTestState) + state.occurrences += min(2, current.occurrences-previous.occurrences) + state.invalidations += min(1, current.invalidations-previous.invalidations) + }) + return mapper, state +} + +func TestFunctionMapperCacheHooks(t *testing.T) { + t.Parallel() + c, _ := newCompositeTestChecker() + c.couldContainTypeVariables = c.couldContainTypeVariablesWorker + c.templateLiteralTypes = make(map[CacheHashKey]*Type) + source := c.newTypeParameter(nil) + target := c.newTypeParameter(nil) + other := c.newTypeParameter(nil) + inner := c.newTemplateLiteralType([]string{"inner-", ""}, []*Type{source}) + outer := c.newTemplateLiteralType([]string{"", "-", ""}, []*Type{inner, inner}) + observer, observerState := newFunctionMapperCacheTest(source, target) + second, secondState := newFunctionMapperCacheTest(target, other) + identity := newSimpleTypeMapper(target, target) + for _, test := range []struct { + mapper *TypeMapper + secondaryOccurrences int + }{ + {observer, 0}, + {newMergedTypeMapper(identity, observer), 0}, + {newMergedTypeMapper(observer, observer), 0}, + {newCompositeTypeMapper(c, identity, observer), 0}, + {newCompositeTypeMapper(c, observer, observer), 0}, + {newCompositeTypeMapper(c, observer, nil), 0}, + {newMergedTypeMapper(observer, second), 2}, + {newCompositeTypeMapper(c, observer, second), 2}, + } { + observerState.occurrences = 0 + secondState.occurrences = 0 + assert.Assert(t, test.mapper.Store(nil)) + instantiations := c.TotalInstantiationCount + result := c.instantiateType(outer, test.mapper) + assert.Equal(t, observerState.occurrences, 2) + assert.Equal(t, secondState.occurrences, test.secondaryOccurrences) + assert.Equal(t, c.TotalInstantiationCount-instantiations, uint32(2)) + assert.Assert(t, result != outer) + } + wholeType, _ := newFunctionMapperCacheTest(inner, c.neverType) + assert.Equal(t, c.instantiateType(outer, wholeType), c.neverType) +} + +func TestFunctionMapperCachedInvalidations(t *testing.T) { + t.Parallel() + c, _ := newCompositeTestChecker() + c.couldContainTypeVariables = c.couldContainTypeVariablesWorker + c.templateLiteralTypes = make(map[CacheHashKey]*Type) + source := c.newTypeParameter(nil) + target := c.newTypeParameter(nil) + inner := c.newTemplateLiteralType([]string{"inner-", ""}, []*Type{source}) + outer := c.newTemplateLiteralType([]string{"", "-", ""}, []*Type{inner, inner}) + observer, state := newFunctionMapperCacheTest(source, target) + // Cache invalidation effects even when the mapper was already invalid. + state.invalidations = 1 + invalidations := 0 + hook := newFunctionTypeMapper(func(typ *Type) *Type { + if typ == source { + invalidations++ + state.invalidations++ + } + return typ + }, nil, nil) + mapper := newMergedTypeMapper(hook, observer) + c.instantiateType(outer, mapper) + assert.Equal(t, state.occurrences, 2) + assert.Equal(t, state.invalidations, 3) + assert.Equal(t, invalidations, 1) + + c.pushActiveMapper(mapper) + c.instantiateType(inner, mapper) + assert.Equal(t, len(c.activeTypeMappersCaches[0]), 1) + assert.Equal(t, len(c.activeTypeMapperStateCaches[0]), 1) + c.clearActiveMapperCaches() + assert.Equal(t, len(c.activeTypeMappersCaches[0]), 0) + assert.Equal(t, len(c.activeTypeMapperStateCaches[0]), 0) + c.instantiateType(inner, mapper) + state.occurrences = 0 + state.invalidations = 0 + instantiations := c.TotalInstantiationCount + c.instantiateType(inner, mapper) + assert.Equal(t, c.TotalInstantiationCount, instantiations) + assert.Equal(t, state.occurrences, 1) + assert.Equal(t, state.invalidations, 1) + c.popActiveMapper() + assert.Equal(t, len(c.activeTypeMappersCaches), 0) + assert.Equal(t, len(c.activeTypeMapperStateCaches), 0) + + other, otherState := newFunctionMapperCacheTest(source, c.newTypeParameter(nil)) + c.pushActiveMapper(other) + c.instantiateType(inner, other) + assert.Equal(t, otherState.occurrences, 1) + assert.Equal(t, state.occurrences, 1) + assert.Equal(t, state.invalidations, 1) + c.popActiveMapper() +} + +func TestFunctionMapperNilCacheState(t *testing.T) { + t.Parallel() + c, _ := newCompositeTestChecker() + c.couldContainTypeVariables = c.couldContainTypeVariablesWorker + c.templateLiteralTypes = make(map[CacheHashKey]*Type) + source := c.newTypeParameter(nil) + target := c.newTypeParameter(nil) + inner := c.newTemplateLiteralType([]string{"inner-", ""}, []*Type{source}) + outer := c.newTemplateLiteralType([]string{"", "-", ""}, []*Type{inner, inner}) + stores, loads := 0, 0 + mapper := newFunctionTypeMapper(func(typ *Type) *Type { + if typ == source { + return target + } + return typ + }, func() any { + stores++ + return nil + }, func(before, after any) { + assert.Assert(t, before == nil && after == nil) + loads++ + }) + c.instantiateType(outer, mapper) + assert.Equal(t, stores, 2) + assert.Equal(t, loads, 1) +} + +func TestFunctionMapperRequiresPairedHandlers(t *testing.T) { + t.Parallel() + for _, test := range []struct { + store func() any + load func(any, any) + }{ + {store: func() any { return nil }}, + {load: func(any, any) {}}, + } { + func() { + defer func() { assert.Assert(t, recover() != nil) }() + newFunctionTypeMapper(func(typ *Type) *Type { return typ }, test.store, test.load) + }() + } +} + +func TestTypeMapperStoreLoadDispatch(t *testing.T) { + t.Parallel() + stores := [2]int{} + loads := [2]int{} + newMapper := func(index int) *TypeMapper { + return newFunctionTypeMapper(func(typ *Type) *Type { return typ }, func() any { + stores[index]++ + return nil + }, func(before, after any) { + assert.Assert(t, before == nil && after == nil) + loads[index]++ + }) + } + first, second := newMapper(0), newMapper(1) + c, _ := newCompositeTestChecker() + mapper := newCompositeTypeMapper(c, newMergedTypeMapper(first, second), first) + assert.Assert(t, mapper.Store(nil)) + assert.Equal(t, stores, [2]int{}) + var before, after TypeMapperState + assert.Assert(t, mapper.Store(&before)) + assert.Assert(t, mapper.Store(&after)) + assert.Equal(t, stores, [2]int{2, 2}) + assert.Equal(t, len(before), 2) + assert.Equal(t, len(after), 2) + mapper.Load(before, after) + mapper.Load(before, after) + assert.Equal(t, loads, [2]int{2, 2}) +} + func TestInferenceContextMappers(t *testing.T) { //nolint:paralleltest c, types := newCompositeTestChecker() c.errorType = types[0] @@ -464,6 +924,10 @@ func TestInferenceContextMappers(t *testing.T) { //nolint:paralleltest info.inferredType = types[1] info.candidates = []*Type{types[1]} context := c.newInferenceContextWorker([]*InferenceInfo{info}, nil, InferenceFlagsNone, nil) + var state TypeMapperState + assert.Assert(t, !context.nonFixingMapper.Store(&state)) + assert.Equal(t, len(state), 0) + context.nonFixingMapper.Load(nil, nil) assert.Equal(t, context.nonFixingMapper.Map(types[0]), types[1]) assert.Assert(t, !info.isFixed) clone := c.cloneInferenceContext(context, InferenceFlagsNone) diff --git a/tsc/testdata/baselines/reference/compiler/conditionalTypeAssignabilityWhenDeferred.errors.txt b/tsc/testdata/baselines/reference/compiler/conditionalTypeAssignabilityWhenDeferred.errors.txt index c81b39e8f03a4..6b803a579fcde 100644 --- a/tsc/testdata/baselines/reference/compiler/conditionalTypeAssignabilityWhenDeferred.errors.txt +++ b/tsc/testdata/baselines/reference/compiler/conditionalTypeAssignabilityWhenDeferred.errors.txt @@ -3,10 +3,10 @@ conditionalTypeAssignabilityWhenDeferred.ts(29,21): error TS2345: Argument of ty conditionalTypeAssignabilityWhenDeferred.ts(39,3): error TS2322: Type '{ x: T; y: T; }' is not assignable to type 'T extends T ? { x: T; y: T; } : never'. conditionalTypeAssignabilityWhenDeferred.ts(46,3): error TS2322: Type 'string' is not assignable to type 'Foo'. conditionalTypeAssignabilityWhenDeferred.ts(63,9): error TS2322: Type '{ a: number; b: number; }' is not assignable to type '[T] extends [[infer U]] ? U : { b: number; }'. -conditionalTypeAssignabilityWhenDeferred.ts(95,23): error TS2345: Argument of type 'string' is not assignable to parameter of type 'Unwrap'. -conditionalTypeAssignabilityWhenDeferred.ts(106,3): error TS2322: Type 'Q' is not assignable to type 'InferBecauseWhyNot'. +conditionalTypeAssignabilityWhenDeferred.ts(90,23): error TS2345: Argument of type 'string' is not assignable to parameter of type 'Unwrap'. +conditionalTypeAssignabilityWhenDeferred.ts(101,3): error TS2322: Type 'Q' is not assignable to type 'InferBecauseWhyNot'. Type '(arg: any) => any' is not assignable to type 'InferBecauseWhyNot'. -conditionalTypeAssignabilityWhenDeferred.ts(116,3): error TS2322: Type 'Q' is not assignable to type 'InferBecauseWhyNotDistributive'. +conditionalTypeAssignabilityWhenDeferred.ts(111,3): error TS2322: Type 'Q' is not assignable to type 'InferBecauseWhyNotDistributive'. Type '(arg: any) => any' is not assignable to type 'InferBecauseWhyNotDistributive'. @@ -61,7 +61,7 @@ conditionalTypeAssignabilityWhenDeferred.ts(116,3): error TS2322: Type 'Q' is no type Foo = T extends true ? string : "a"; function test(x: Foo, s: string) { - x = "a"; // Currently an error, should be ok + x = "a"; // OK: both branches accept "a". x = s; // Error ~ !!! error TS2322: Type 'string' is not assignable to type 'Foo'. @@ -85,20 +85,15 @@ conditionalTypeAssignabilityWhenDeferred.ts(116,3): error TS2322: Type 'Q' is no ~~ !!! error TS2322: Type '{ a: number; b: number; }' is not assignable to type '[T] extends [[infer U]] ? U : { b: number; }'. - // The next 4 are arguable - if you choose to ignore the `never` distribution case, - // then they're all good. The `never` case _is_ a bit of an outlier - we say distributive types - // look approximately like the sum of their branches, but the `never` case bucks that. - // There's an argument for the result of dumping `never` into a distributive conditional - // being not `never`, but instead the intersection of the branches - a much more precise bound - // on that "impossible" input. + // Independent branches retain branch-wise checking without a distributive source. - // Distributive where T might instantiate to never: no good + // Distributive with independent branches: OK const o3: Distributive = o; - // Distributive where T & string might instantiate to never: also no good + // Intersection check type with independent branches: OK const o4: Distributive = o; // Distributive where {a: T} cannot instantiate to never: OK const o5: Distributive<{ a: T }> = o; - // Distributive where check type is a conditional which returns a non-never type upon instantiation with `never` but can still return never otherwise: no good + // Conditional check type with independent branches: OK const o6: Distributive<[T] extends [never] ? { a: number } : never> = o; } @@ -128,7 +123,7 @@ conditionalTypeAssignabilityWhenDeferred.ts(116,3): error TS2322: Type 'Q' is no : never; function f3 any>(x: Q): InferBecauseWhyNot { - return x; + return x; // Error: Q can add overloads that change the inferred parameter. ~~~~~~ !!! error TS2322: Type 'Q' is not assignable to type 'InferBecauseWhyNot'. !!! error TS2322: Type '(arg: any) => any' is not assignable to type 'InferBecauseWhyNot'. @@ -141,7 +136,7 @@ conditionalTypeAssignabilityWhenDeferred.ts(116,3): error TS2322: Type 'Q' is no function f4 any>( x: Q ): InferBecauseWhyNotDistributive { - return x; // should fail + return x; // Error: additional overloads can make the extends check fail. ~~~~~~ !!! error TS2322: Type 'Q' is not assignable to type 'InferBecauseWhyNotDistributive'. !!! error TS2322: Type '(arg: any) => any' is not assignable to type 'InferBecauseWhyNotDistributive'. diff --git a/tsc/testdata/baselines/reference/compiler/conditionalTypeAssignabilityWhenDeferred.js b/tsc/testdata/baselines/reference/compiler/conditionalTypeAssignabilityWhenDeferred.js index 8a994135eb158..65022a50ce166 100644 --- a/tsc/testdata/baselines/reference/compiler/conditionalTypeAssignabilityWhenDeferred.js +++ b/tsc/testdata/baselines/reference/compiler/conditionalTypeAssignabilityWhenDeferred.js @@ -45,7 +45,7 @@ function f2(t1: { x: T; y: T }, t2: T extends T ? { x: T; y: T } : never) { type Foo = T extends true ? string : "a"; function test(x: Foo, s: string) { - x = "a"; // Currently an error, should be ok + x = "a"; // OK: both branches accept "a". x = s; // Error } @@ -65,20 +65,15 @@ function testAssignabilityToConditionalType() { // Infer type parameters: no good const o2: [T] extends [[infer U]] ? U : { b: number } = o; - // The next 4 are arguable - if you choose to ignore the `never` distribution case, - // then they're all good. The `never` case _is_ a bit of an outlier - we say distributive types - // look approximately like the sum of their branches, but the `never` case bucks that. - // There's an argument for the result of dumping `never` into a distributive conditional - // being not `never`, but instead the intersection of the branches - a much more precise bound - // on that "impossible" input. + // Independent branches retain branch-wise checking without a distributive source. - // Distributive where T might instantiate to never: no good + // Distributive with independent branches: OK const o3: Distributive = o; - // Distributive where T & string might instantiate to never: also no good + // Intersection check type with independent branches: OK const o4: Distributive = o; // Distributive where {a: T} cannot instantiate to never: OK const o5: Distributive<{ a: T }> = o; - // Distributive where check type is a conditional which returns a non-never type upon instantiation with `never` but can still return never otherwise: no good + // Conditional check type with independent branches: OK const o6: Distributive<[T] extends [never] ? { a: number } : never> = o; } @@ -106,7 +101,7 @@ type InferBecauseWhyNot = [T] extends [(p: infer P1) => any] : never; function f3 any>(x: Q): InferBecauseWhyNot { - return x; + return x; // Error: Q can add overloads that change the inferred parameter. } type InferBecauseWhyNotDistributive = T extends (p: infer P1) => any @@ -116,7 +111,7 @@ type InferBecauseWhyNotDistributive = T extends (p: infer P1) => any function f4 any>( x: Q ): InferBecauseWhyNotDistributive { - return x; // should fail + return x; // Error: additional overloads can make the extends check fail. } @@ -139,7 +134,7 @@ function f2(t1, t2) { t2 = t1; // should fail } function test(x, s) { - x = "a"; // Currently an error, should be ok + x = "a"; // OK: both branches accept "a". x = s; // Error } function testAssignabilityToConditionalType() { @@ -151,19 +146,14 @@ function testAssignabilityToConditionalType() { const x1 = x; // Infer type parameters: no good const o2 = o; - // The next 4 are arguable - if you choose to ignore the `never` distribution case, - // then they're all good. The `never` case _is_ a bit of an outlier - we say distributive types - // look approximately like the sum of their branches, but the `never` case bucks that. - // There's an argument for the result of dumping `never` into a distributive conditional - // being not `never`, but instead the intersection of the branches - a much more precise bound - // on that "impossible" input. - // Distributive where T might instantiate to never: no good + // Independent branches retain branch-wise checking without a distributive source. + // Distributive with independent branches: OK const o3 = o; - // Distributive where T & string might instantiate to never: also no good + // Intersection check type with independent branches: OK const o4 = o; // Distributive where {a: T} cannot instantiate to never: OK const o5 = o; - // Distributive where check type is a conditional which returns a non-never type upon instantiation with `never` but can still return never otherwise: no good + // Conditional check type with independent branches: OK const o6 = o; } class Foo2 { @@ -173,8 +163,8 @@ class Foo2 { } set(new Foo2(), "prop", "hi"); // <-- typechecks function f3(x) { - return x; + return x; // Error: Q can add overloads that change the inferred parameter. } function f4(x) { - return x; // should fail + return x; // Error: additional overloads can make the extends check fail. } diff --git a/tsc/testdata/baselines/reference/compiler/conditionalTypeAssignabilityWhenDeferred.symbols b/tsc/testdata/baselines/reference/compiler/conditionalTypeAssignabilityWhenDeferred.symbols index 03fd3dbe0cf5b..953f629a2541f 100644 --- a/tsc/testdata/baselines/reference/compiler/conditionalTypeAssignabilityWhenDeferred.symbols +++ b/tsc/testdata/baselines/reference/compiler/conditionalTypeAssignabilityWhenDeferred.symbols @@ -160,7 +160,7 @@ function test(x: Foo, s: string) { >T : Symbol(T, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 43, 14)) >s : Symbol(s, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 43, 27)) - x = "a"; // Currently an error, should be ok + x = "a"; // OK: both branches accept "a". >x : Symbol(x, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 43, 17)) x = s; // Error @@ -231,157 +231,152 @@ function testAssignabilityToConditionalType() { >b : Symbol(b, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 62, 43)) >o : Symbol(o, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 51, 7)) - // The next 4 are arguable - if you choose to ignore the `never` distribution case, - // then they're all good. The `never` case _is_ a bit of an outlier - we say distributive types - // look approximately like the sum of their branches, but the `never` case bucks that. - // There's an argument for the result of dumping `never` into a distributive conditional - // being not `never`, but instead the intersection of the branches - a much more precise bound - // on that "impossible" input. + // Independent branches retain branch-wise checking without a distributive source. - // Distributive where T might instantiate to never: no good + // Distributive with independent branches: OK const o3: Distributive = o; ->o3 : Symbol(o3, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 72, 7)) +>o3 : Symbol(o3, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 67, 7)) >Distributive : Symbol(Distributive, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 46, 1)) >T : Symbol(T, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 50, 44)) >o : Symbol(o, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 51, 7)) - // Distributive where T & string might instantiate to never: also no good + // Intersection check type with independent branches: OK const o4: Distributive = o; ->o4 : Symbol(o4, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 74, 7)) +>o4 : Symbol(o4, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 69, 7)) >Distributive : Symbol(Distributive, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 46, 1)) >T : Symbol(T, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 50, 44)) >o : Symbol(o, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 51, 7)) // Distributive where {a: T} cannot instantiate to never: OK const o5: Distributive<{ a: T }> = o; ->o5 : Symbol(o5, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 76, 7)) +>o5 : Symbol(o5, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 71, 7)) >Distributive : Symbol(Distributive, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 46, 1)) ->a : Symbol(a, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 76, 26)) +>a : Symbol(a, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 71, 26)) >T : Symbol(T, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 50, 44)) >o : Symbol(o, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 51, 7)) - // Distributive where check type is a conditional which returns a non-never type upon instantiation with `never` but can still return never otherwise: no good + // Conditional check type with independent branches: OK const o6: Distributive<[T] extends [never] ? { a: number } : never> = o; ->o6 : Symbol(o6, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 78, 7)) +>o6 : Symbol(o6, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 73, 7)) >Distributive : Symbol(Distributive, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 46, 1)) >T : Symbol(T, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 50, 44)) ->a : Symbol(a, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 78, 48)) +>a : Symbol(a, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 73, 48)) >o : Symbol(o, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 51, 7)) } type Wrapped = { ___secret: T }; ->Wrapped : Symbol(Wrapped, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 79, 1)) ->T : Symbol(T, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 81, 13)) ->___secret : Symbol(___secret, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 81, 19)) ->T : Symbol(T, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 81, 13)) +>Wrapped : Symbol(Wrapped, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 74, 1)) +>T : Symbol(T, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 76, 13)) +>___secret : Symbol(___secret, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 76, 19)) +>T : Symbol(T, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 76, 13)) type Unwrap = T extends Wrapped ? U : T; ->Unwrap : Symbol(Unwrap, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 81, 35)) ->T : Symbol(T, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 82, 12)) ->T : Symbol(T, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 82, 12)) ->Wrapped : Symbol(Wrapped, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 79, 1)) ->U : Symbol(U, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 82, 40)) ->U : Symbol(U, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 82, 40)) ->T : Symbol(T, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 82, 12)) +>Unwrap : Symbol(Unwrap, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 76, 35)) +>T : Symbol(T, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 77, 12)) +>T : Symbol(T, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 77, 12)) +>Wrapped : Symbol(Wrapped, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 74, 1)) +>U : Symbol(U, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 77, 40)) +>U : Symbol(U, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 77, 40)) +>T : Symbol(T, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 77, 12)) declare function set( ->set : Symbol(set, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 82, 52)) ->T : Symbol(T, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 84, 21)) ->K : Symbol(K, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 84, 23)) ->T : Symbol(T, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 84, 21)) +>set : Symbol(set, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 77, 52)) +>T : Symbol(T, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 79, 21)) +>K : Symbol(K, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 79, 23)) +>T : Symbol(T, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 79, 21)) obj: T, ->obj : Symbol(obj, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 84, 43)) ->T : Symbol(T, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 84, 21)) +>obj : Symbol(obj, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 79, 43)) +>T : Symbol(T, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 79, 21)) key: K, ->key : Symbol(key, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 85, 9)) ->K : Symbol(K, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 84, 23)) +>key : Symbol(key, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 80, 9)) +>K : Symbol(K, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 79, 23)) value: Unwrap ->value : Symbol(value, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 86, 9)) ->Unwrap : Symbol(Unwrap, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 81, 35)) ->T : Symbol(T, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 84, 21)) ->K : Symbol(K, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 84, 23)) +>value : Symbol(value, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 81, 9)) +>Unwrap : Symbol(Unwrap, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 76, 35)) +>T : Symbol(T, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 79, 21)) +>K : Symbol(K, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 79, 23)) ): Unwrap; ->Unwrap : Symbol(Unwrap, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 81, 35)) ->T : Symbol(T, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 84, 21)) ->K : Symbol(K, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 84, 23)) +>Unwrap : Symbol(Unwrap, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 76, 35)) +>T : Symbol(T, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 79, 21)) +>K : Symbol(K, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 79, 23)) class Foo2 { ->Foo2 : Symbol(Foo2, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 88, 16)) +>Foo2 : Symbol(Foo2, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 83, 16)) prop!: Wrapped; ->prop : Symbol(Foo2.prop, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 90, 12)) ->Wrapped : Symbol(Wrapped, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 79, 1)) +>prop : Symbol(Foo2.prop, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 85, 12)) +>Wrapped : Symbol(Wrapped, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 74, 1)) method() { ->method : Symbol(Foo2.method, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 91, 25)) +>method : Symbol(Foo2.method, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 86, 25)) set(this, "prop", "hi"); // <-- type error ->set : Symbol(set, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 82, 52)) ->this : Symbol(Foo2, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 88, 16)) +>set : Symbol(set, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 77, 52)) +>this : Symbol(Foo2, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 83, 16)) } } set(new Foo2(), "prop", "hi"); // <-- typechecks ->set : Symbol(set, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 82, 52)) ->Foo2 : Symbol(Foo2, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 88, 16)) +>set : Symbol(set, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 77, 52)) +>Foo2 : Symbol(Foo2, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 83, 16)) type InferBecauseWhyNot = [T] extends [(p: infer P1) => any] ->InferBecauseWhyNot : Symbol(InferBecauseWhyNot, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 98, 30)) ->T : Symbol(T, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 100, 24)) ->T : Symbol(T, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 100, 24)) ->p : Symbol(p, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 100, 43)) ->P1 : Symbol(P1, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 100, 51)) +>InferBecauseWhyNot : Symbol(InferBecauseWhyNot, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 93, 30)) +>T : Symbol(T, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 95, 24)) +>T : Symbol(T, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 95, 24)) +>p : Symbol(p, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 95, 43)) +>P1 : Symbol(P1, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 95, 51)) ? P1 | T ->P1 : Symbol(P1, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 100, 51)) ->T : Symbol(T, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 100, 24)) +>P1 : Symbol(P1, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 95, 51)) +>T : Symbol(T, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 95, 24)) : never; function f3 any>(x: Q): InferBecauseWhyNot { ->f3 : Symbol(f3, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 102, 10)) ->Q : Symbol(Q, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 104, 12)) ->arg : Symbol(arg, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 104, 23)) ->x : Symbol(x, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 104, 41)) ->Q : Symbol(Q, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 104, 12)) ->InferBecauseWhyNot : Symbol(InferBecauseWhyNot, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 98, 30)) ->Q : Symbol(Q, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 104, 12)) - - return x; ->x : Symbol(x, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 104, 41)) +>f3 : Symbol(f3, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 97, 10)) +>Q : Symbol(Q, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 99, 12)) +>arg : Symbol(arg, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 99, 23)) +>x : Symbol(x, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 99, 41)) +>Q : Symbol(Q, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 99, 12)) +>InferBecauseWhyNot : Symbol(InferBecauseWhyNot, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 93, 30)) +>Q : Symbol(Q, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 99, 12)) + + return x; // Error: Q can add overloads that change the inferred parameter. +>x : Symbol(x, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 99, 41)) } type InferBecauseWhyNotDistributive = T extends (p: infer P1) => any ->InferBecauseWhyNotDistributive : Symbol(InferBecauseWhyNotDistributive, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 106, 1)) ->T : Symbol(T, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 108, 36)) ->T : Symbol(T, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 108, 36)) ->p : Symbol(p, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 108, 52)) ->P1 : Symbol(P1, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 108, 60)) +>InferBecauseWhyNotDistributive : Symbol(InferBecauseWhyNotDistributive, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 101, 1)) +>T : Symbol(T, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 103, 36)) +>T : Symbol(T, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 103, 36)) +>p : Symbol(p, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 103, 52)) +>P1 : Symbol(P1, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 103, 60)) ? P1 | T ->P1 : Symbol(P1, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 108, 60)) ->T : Symbol(T, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 108, 36)) +>P1 : Symbol(P1, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 103, 60)) +>T : Symbol(T, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 103, 36)) : never; function f4 any>( ->f4 : Symbol(f4, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 110, 10)) ->Q : Symbol(Q, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 112, 12)) ->arg : Symbol(arg, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 112, 23)) +>f4 : Symbol(f4, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 105, 10)) +>Q : Symbol(Q, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 107, 12)) +>arg : Symbol(arg, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 107, 23)) x: Q ->x : Symbol(x, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 112, 41)) ->Q : Symbol(Q, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 112, 12)) +>x : Symbol(x, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 107, 41)) +>Q : Symbol(Q, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 107, 12)) ): InferBecauseWhyNotDistributive { ->InferBecauseWhyNotDistributive : Symbol(InferBecauseWhyNotDistributive, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 106, 1)) ->Q : Symbol(Q, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 112, 12)) +>InferBecauseWhyNotDistributive : Symbol(InferBecauseWhyNotDistributive, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 101, 1)) +>Q : Symbol(Q, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 107, 12)) - return x; // should fail ->x : Symbol(x, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 112, 41)) + return x; // Error: additional overloads can make the extends check fail. +>x : Symbol(x, Decl(conditionalTypeAssignabilityWhenDeferred.ts, 107, 41)) } diff --git a/tsc/testdata/baselines/reference/compiler/conditionalTypeAssignabilityWhenDeferred.types b/tsc/testdata/baselines/reference/compiler/conditionalTypeAssignabilityWhenDeferred.types index a41a067df7452..ddd4d544bcf11 100644 --- a/tsc/testdata/baselines/reference/compiler/conditionalTypeAssignabilityWhenDeferred.types +++ b/tsc/testdata/baselines/reference/compiler/conditionalTypeAssignabilityWhenDeferred.types @@ -128,7 +128,7 @@ function test(x: Foo, s: string) { >x : Foo >s : string - x = "a"; // Currently an error, should be ok + x = "a"; // OK: both branches accept "a". >x = "a" : "a" >x : Foo >"a" : "a" @@ -195,19 +195,14 @@ function testAssignabilityToConditionalType() { >b : number >o : { a: number; b: number; } - // The next 4 are arguable - if you choose to ignore the `never` distribution case, - // then they're all good. The `never` case _is_ a bit of an outlier - we say distributive types - // look approximately like the sum of their branches, but the `never` case bucks that. - // There's an argument for the result of dumping `never` into a distributive conditional - // being not `never`, but instead the intersection of the branches - a much more precise bound - // on that "impossible" input. + // Independent branches retain branch-wise checking without a distributive source. - // Distributive where T might instantiate to never: no good + // Distributive with independent branches: OK const o3: Distributive = o; >o3 : Distributive >o : { a: number; b: number; } - // Distributive where T & string might instantiate to never: also no good + // Intersection check type with independent branches: OK const o4: Distributive = o; >o4 : Distributive >o : { a: number; b: number; } @@ -218,7 +213,7 @@ function testAssignabilityToConditionalType() { >a : T >o : { a: number; b: number; } - // Distributive where check type is a conditional which returns a non-never type upon instantiation with `never` but can still return never otherwise: no good + // Conditional check type with independent branches: OK const o6: Distributive<[T] extends [never] ? { a: number } : never> = o; >o6 : Distributive<[T] extends [never] ? { a: number; } : never> >a : number @@ -284,7 +279,7 @@ function f3 any>(x: Q): InferBecauseWhyNot { >arg : any >x : Q - return x; + return x; // Error: Q can add overloads that change the inferred parameter. >x : Q } @@ -303,7 +298,7 @@ function f4 any>( >x : Q ): InferBecauseWhyNotDistributive { - return x; // should fail + return x; // Error: additional overloads can make the extends check fail. >x : Q } diff --git a/tsc/testdata/baselines/reference/compiler/distributiveConditionalTypeInferenceNonStrict.errors.txt b/tsc/testdata/baselines/reference/compiler/distributiveConditionalTypeInferenceNonStrict.errors.txt new file mode 100644 index 0000000000000..dad5a7a810dcc --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/distributiveConditionalTypeInferenceNonStrict.errors.txt @@ -0,0 +1,24 @@ +distributiveConditionalTypeInferenceNonStrict.ts(4,11): error TS2322: Type 'T' is not assignable to type 'InferredParameter'. + Type '(value: unknown) => unknown' is not assignable to type 'InferredParameter'. +distributiveConditionalTypeInferenceNonStrict.ts(12,7): error TS2322: Type '{ (value: string): void; (left: number, right: number): void; }' is not assignable to type 'never'. + + +==== distributiveConditionalTypeInferenceNonStrict.ts (2 errors) ==== + type InferredParameter = T extends (value: infer P) => unknown ? T | P : never; + + function inference unknown>(value: T) { + const result: InferredParameter = value; // Error: bivariant constraints admit narrower parameters. + ~~~~~~ +!!! error TS2322: Type 'T' is not assignable to type 'InferredParameter'. +!!! error TS2322: Type '(value: unknown) => unknown' is not assignable to type 'InferredParameter'. + } + + declare const overloaded: { + (value: string): void; + (left: number, right: number): void; + }; + const constraint: (value: unknown) => unknown = overloaded; + const rejected: InferredParameter = overloaded; // Error + ~~~~~~~~ +!!! error TS2322: Type '{ (value: string): void; (left: number, right: number): void; }' is not assignable to type 'never'. + \ No newline at end of file diff --git a/tsc/testdata/baselines/reference/compiler/distributiveConditionalTypeInferenceNonStrict.symbols b/tsc/testdata/baselines/reference/compiler/distributiveConditionalTypeInferenceNonStrict.symbols new file mode 100644 index 0000000000000..1a84001f8c2ed --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/distributiveConditionalTypeInferenceNonStrict.symbols @@ -0,0 +1,48 @@ +//// [tests/cases/compiler/distributiveConditionalTypeInferenceNonStrict.ts] //// + +=== distributiveConditionalTypeInferenceNonStrict.ts === +type InferredParameter = T extends (value: infer P) => unknown ? T | P : never; +>InferredParameter : Symbol(InferredParameter, Decl(distributiveConditionalTypeInferenceNonStrict.ts, 0, 0)) +>T : Symbol(T, Decl(distributiveConditionalTypeInferenceNonStrict.ts, 0, 23)) +>T : Symbol(T, Decl(distributiveConditionalTypeInferenceNonStrict.ts, 0, 23)) +>value : Symbol(value, Decl(distributiveConditionalTypeInferenceNonStrict.ts, 0, 39)) +>P : Symbol(P, Decl(distributiveConditionalTypeInferenceNonStrict.ts, 0, 51)) +>T : Symbol(T, Decl(distributiveConditionalTypeInferenceNonStrict.ts, 0, 23)) +>P : Symbol(P, Decl(distributiveConditionalTypeInferenceNonStrict.ts, 0, 51)) + +function inference unknown>(value: T) { +>inference : Symbol(inference, Decl(distributiveConditionalTypeInferenceNonStrict.ts, 0, 82)) +>T : Symbol(T, Decl(distributiveConditionalTypeInferenceNonStrict.ts, 2, 19)) +>value : Symbol(value, Decl(distributiveConditionalTypeInferenceNonStrict.ts, 2, 30)) +>value : Symbol(value, Decl(distributiveConditionalTypeInferenceNonStrict.ts, 2, 58)) +>T : Symbol(T, Decl(distributiveConditionalTypeInferenceNonStrict.ts, 2, 19)) + + const result: InferredParameter = value; // Error: bivariant constraints admit narrower parameters. +>result : Symbol(result, Decl(distributiveConditionalTypeInferenceNonStrict.ts, 3, 9)) +>InferredParameter : Symbol(InferredParameter, Decl(distributiveConditionalTypeInferenceNonStrict.ts, 0, 0)) +>T : Symbol(T, Decl(distributiveConditionalTypeInferenceNonStrict.ts, 2, 19)) +>value : Symbol(value, Decl(distributiveConditionalTypeInferenceNonStrict.ts, 2, 58)) +} + +declare const overloaded: { +>overloaded : Symbol(overloaded, Decl(distributiveConditionalTypeInferenceNonStrict.ts, 6, 13)) + + (value: string): void; +>value : Symbol(value, Decl(distributiveConditionalTypeInferenceNonStrict.ts, 7, 5)) + + (left: number, right: number): void; +>left : Symbol(left, Decl(distributiveConditionalTypeInferenceNonStrict.ts, 8, 5)) +>right : Symbol(right, Decl(distributiveConditionalTypeInferenceNonStrict.ts, 8, 18)) + +}; +const constraint: (value: unknown) => unknown = overloaded; +>constraint : Symbol(constraint, Decl(distributiveConditionalTypeInferenceNonStrict.ts, 10, 5)) +>value : Symbol(value, Decl(distributiveConditionalTypeInferenceNonStrict.ts, 10, 19)) +>overloaded : Symbol(overloaded, Decl(distributiveConditionalTypeInferenceNonStrict.ts, 6, 13)) + +const rejected: InferredParameter = overloaded; // Error +>rejected : Symbol(rejected, Decl(distributiveConditionalTypeInferenceNonStrict.ts, 11, 5)) +>InferredParameter : Symbol(InferredParameter, Decl(distributiveConditionalTypeInferenceNonStrict.ts, 0, 0)) +>overloaded : Symbol(overloaded, Decl(distributiveConditionalTypeInferenceNonStrict.ts, 6, 13)) +>overloaded : Symbol(overloaded, Decl(distributiveConditionalTypeInferenceNonStrict.ts, 6, 13)) + diff --git a/tsc/testdata/baselines/reference/compiler/distributiveConditionalTypeInferenceNonStrict.types b/tsc/testdata/baselines/reference/compiler/distributiveConditionalTypeInferenceNonStrict.types new file mode 100644 index 0000000000000..0aeb9e729d872 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/distributiveConditionalTypeInferenceNonStrict.types @@ -0,0 +1,38 @@ +//// [tests/cases/compiler/distributiveConditionalTypeInferenceNonStrict.ts] //// + +=== distributiveConditionalTypeInferenceNonStrict.ts === +type InferredParameter = T extends (value: infer P) => unknown ? T | P : never; +>InferredParameter : InferredParameter +>value : P + +function inference unknown>(value: T) { +>inference : unknown>(value: T) => void +>value : unknown +>value : T + + const result: InferredParameter = value; // Error: bivariant constraints admit narrower parameters. +>result : InferredParameter +>value : T +} + +declare const overloaded: { +>overloaded : { (value: string): void; (left: number, right: number): void; } + + (value: string): void; +>value : string + + (left: number, right: number): void; +>left : number +>right : number + +}; +const constraint: (value: unknown) => unknown = overloaded; +>constraint : (value: unknown) => unknown +>value : unknown +>overloaded : { (value: string): void; (left: number, right: number): void; } + +const rejected: InferredParameter = overloaded; // Error +>rejected : never +>overloaded : { (value: string): void; (left: number, right: number): void; } +>overloaded : { (value: string): void; (left: number, right: number): void; } + diff --git a/tsc/testdata/baselines/reference/compiler/distributiveConditionalTypeInferencePatterns.errors.txt b/tsc/testdata/baselines/reference/compiler/distributiveConditionalTypeInferencePatterns.errors.txt new file mode 100644 index 0000000000000..727b477e3384c --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/distributiveConditionalTypeInferencePatterns.errors.txt @@ -0,0 +1,158 @@ +distributiveConditionalTypeInferencePatterns.ts(6,9): error TS2322: Type 'T' is not assignable to type 'Result'. + Type '{ a: string; b: number; }' is not assignable to type 'Result'. +distributiveConditionalTypeInferencePatterns.ts(10,9): error TS2322: Type 'T' is not assignable to type 'NonDistributive'. + Type '{ a: string; b: number; }' is not assignable to type 'NonDistributive'. +distributiveConditionalTypeInferencePatterns.ts(13,18): error TS2322: Type '{ a: string; b: number; }' is not assignable to type 'never'. +distributiveConditionalTypeInferencePatterns.ts(38,9): error TS2322: Type 'T' is not assignable to type 'Result'. + Type '{ a: string; b: number; }' is not assignable to type 'Result'. +distributiveConditionalTypeInferencePatterns.ts(42,9): error TS2322: Type 'T' is not assignable to type 'NonDistributive'. + Type '{ a: string; b: number; }' is not assignable to type 'NonDistributive'. +distributiveConditionalTypeInferencePatterns.ts(45,18): error TS2322: Type '{ a: string; b: number; }' is not assignable to type 'never'. +distributiveConditionalTypeInferencePatterns.ts(66,9): error TS2322: Type '{}' is not assignable to type 'Box'. +distributiveConditionalTypeInferencePatterns.ts(70,9): error TS2322: Type '{}' is not assignable to type 'PropertyBox'. +distributiveConditionalTypeInferencePatterns.ts(74,9): error TS2322: Type '{ done: true; }' is not assignable to type 'Constant'. +distributiveConditionalTypeInferencePatterns.ts(77,18): error TS2322: Type '{}' is not assignable to type 'never'. +distributiveConditionalTypeInferencePatterns.ts(78,18): error TS2322: Type '{}' is not assignable to type 'never'. +distributiveConditionalTypeInferencePatterns.ts(79,18): error TS2322: Type '{ done: boolean; }' is not assignable to type 'never'. + + +==== distributiveConditionalTypeInferencePatterns.ts (12 errors) ==== + namespace IntersectionInference { + type Result = T extends { a: infer U & string; b: infer U } ? T : never; + type NonDistributive = [T] extends [{ a: infer U & string; b: infer U }] ? T : never; + + export function generic(value: T): Result { + return value; // Error: string is removed before inferring U from b. + ~~~~~~ +!!! error TS2322: Type 'T' is not assignable to type 'Result'. +!!! error TS2322: Type '{ a: string; b: number; }' is not assignable to type 'Result'. + } + + export function nonDistributive(value: T): NonDistributive { + return value; // Error + ~~~~~~ +!!! error TS2322: Type 'T' is not assignable to type 'NonDistributive'. +!!! error TS2322: Type '{ a: string; b: number; }' is not assignable to type 'NonDistributive'. + } + + export const rejected: Result<{ a: string; b: number }> = { a: "", b: 1 }; // Error + ~~~~~~~~ +!!! error TS2322: Type '{ a: string; b: number; }' is not assignable to type 'never'. + export const accepted: Result<{ a: string; b: string }> = { a: "", b: "" }; + + type Covariant = T extends { a: infer U; b: infer U } ? T : never; + type ObjectIntersection = T extends { a: infer U } & { b: number } ? T : never; + + export function covariant(value: T): Covariant { + return value; + } + + export function objectIntersection(value: T): ObjectIntersection { + return value; + } + } + + namespace DependentConstraints { + interface Dep { + a: A; + b: B; + } + + type Result = T extends Dep ? T : never; + type NonDistributive = [T] extends [Dep] ? T : never; + + export function generic(value: T): Result { + return value; // Error: the inferred B must satisfy the same A. + ~~~~~~ +!!! error TS2322: Type 'T' is not assignable to type 'Result'. +!!! error TS2322: Type '{ a: string; b: number; }' is not assignable to type 'Result'. + } + + export function nonDistributive(value: T): NonDistributive { + return value; // Error + ~~~~~~ +!!! error TS2322: Type 'T' is not assignable to type 'NonDistributive'. +!!! error TS2322: Type '{ a: string; b: number; }' is not assignable to type 'NonDistributive'. + } + + export const rejected: Result<{ a: string; b: number }> = { a: "", b: 1 }; // Error + ~~~~~~~~ +!!! error TS2322: Type '{ a: string; b: number; }' is not assignable to type 'never'. + export const accepted: Result<{ a: string; b: string }> = { a: "", b: "" }; + + interface Independent { + a: A; + b: B; + } + + type IndependentResult = T extends Independent ? T : never; + + export function independent(value: T): IndependentResult { + return value; + } + } + + namespace InferredBranchDependence { + type Box = T extends infer U ? { value?: U } : never; + type PropertyBox = T extends { value: infer U } ? { copy?: U } : never; + type Constant = T extends infer U ? { done: true } : never; + + export function box(): Box { + return {}; // Error: Box is never even though the branch only references U. + ~~~~~~ +!!! error TS2322: Type '{}' is not assignable to type 'Box'. + } + + export function propertyBox(): PropertyBox { + return {}; // Error + ~~~~~~ +!!! error TS2322: Type '{}' is not assignable to type 'PropertyBox'. + } + + export function constant(): Constant { + return { done: true }; // Error: an unused infer binder still requires a nonempty check type. + ~~~~~~ +!!! error TS2322: Type '{ done: true; }' is not assignable to type 'Constant'. + } + + export const rejectedBox: Box = {}; // Error + ~~~~~~~~~~~ +!!! error TS2322: Type '{}' is not assignable to type 'never'. + export const rejectedProperty: PropertyBox = {}; // Error + ~~~~~~~~~~~~~~~~ +!!! error TS2322: Type '{}' is not assignable to type 'never'. + export const rejectedConstant: Constant = { done: true }; // Error + ~~~~~~~~~~~~~~~~ +!!! error TS2322: Type '{ done: boolean; }' is not assignable to type 'never'. + + type NonDistributive = [T] extends [infer U] ? { value?: U } : never; + + export function nonDistributive(): NonDistributive { + return {}; + } + + export const emptyNonDistributive: NonDistributive = {}; + + export function nonNeverUnion(): Box { + return {}; + } + + export function nonNeverProperty(): PropertyBox { + return {}; + } + + export function nonNeverObject(): Box<{ value: T }> { + return {}; + } + + export function nonNeverConditional(): Box<[T] extends [never] ? { value: T } : number> { + return {}; + } + + type Identity = T extends infer U ? U : never; + + export function identity(value: T): Identity { + return value; + } + } + \ No newline at end of file diff --git a/tsc/testdata/baselines/reference/compiler/distributiveConditionalTypeInferencePatterns.symbols b/tsc/testdata/baselines/reference/compiler/distributiveConditionalTypeInferencePatterns.symbols new file mode 100644 index 0000000000000..e59b7dfee628d --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/distributiveConditionalTypeInferencePatterns.symbols @@ -0,0 +1,387 @@ +//// [tests/cases/compiler/distributiveConditionalTypeInferencePatterns.ts] //// + +=== distributiveConditionalTypeInferencePatterns.ts === +namespace IntersectionInference { +>IntersectionInference : Symbol(IntersectionInference, Decl(distributiveConditionalTypeInferencePatterns.ts, 0, 0)) + + type Result = T extends { a: infer U & string; b: infer U } ? T : never; +>Result : Symbol(Result, Decl(distributiveConditionalTypeInferencePatterns.ts, 0, 33)) +>T : Symbol(T, Decl(distributiveConditionalTypeInferencePatterns.ts, 1, 16)) +>T : Symbol(T, Decl(distributiveConditionalTypeInferencePatterns.ts, 1, 16)) +>a : Symbol(a, Decl(distributiveConditionalTypeInferencePatterns.ts, 1, 32)) +>U : Symbol(U, Decl(distributiveConditionalTypeInferencePatterns.ts, 1, 41), Decl(distributiveConditionalTypeInferencePatterns.ts, 1, 62)) +>b : Symbol(b, Decl(distributiveConditionalTypeInferencePatterns.ts, 1, 53)) +>U : Symbol(U, Decl(distributiveConditionalTypeInferencePatterns.ts, 1, 41), Decl(distributiveConditionalTypeInferencePatterns.ts, 1, 62)) +>T : Symbol(T, Decl(distributiveConditionalTypeInferencePatterns.ts, 1, 16)) + + type NonDistributive = [T] extends [{ a: infer U & string; b: infer U }] ? T : never; +>NonDistributive : Symbol(NonDistributive, Decl(distributiveConditionalTypeInferencePatterns.ts, 1, 79)) +>T : Symbol(T, Decl(distributiveConditionalTypeInferencePatterns.ts, 2, 25)) +>T : Symbol(T, Decl(distributiveConditionalTypeInferencePatterns.ts, 2, 25)) +>a : Symbol(a, Decl(distributiveConditionalTypeInferencePatterns.ts, 2, 44)) +>U : Symbol(U, Decl(distributiveConditionalTypeInferencePatterns.ts, 2, 53), Decl(distributiveConditionalTypeInferencePatterns.ts, 2, 74)) +>b : Symbol(b, Decl(distributiveConditionalTypeInferencePatterns.ts, 2, 65)) +>U : Symbol(U, Decl(distributiveConditionalTypeInferencePatterns.ts, 2, 53), Decl(distributiveConditionalTypeInferencePatterns.ts, 2, 74)) +>T : Symbol(T, Decl(distributiveConditionalTypeInferencePatterns.ts, 2, 25)) + + export function generic(value: T): Result { +>generic : Symbol(generic, Decl(distributiveConditionalTypeInferencePatterns.ts, 2, 92)) +>T : Symbol(T, Decl(distributiveConditionalTypeInferencePatterns.ts, 4, 28)) +>a : Symbol(a, Decl(distributiveConditionalTypeInferencePatterns.ts, 4, 39)) +>b : Symbol(b, Decl(distributiveConditionalTypeInferencePatterns.ts, 4, 50)) +>value : Symbol(value, Decl(distributiveConditionalTypeInferencePatterns.ts, 4, 64)) +>T : Symbol(T, Decl(distributiveConditionalTypeInferencePatterns.ts, 4, 28)) +>Result : Symbol(Result, Decl(distributiveConditionalTypeInferencePatterns.ts, 0, 33)) +>T : Symbol(T, Decl(distributiveConditionalTypeInferencePatterns.ts, 4, 28)) + + return value; // Error: string is removed before inferring U from b. +>value : Symbol(value, Decl(distributiveConditionalTypeInferencePatterns.ts, 4, 64)) + } + + export function nonDistributive(value: T): NonDistributive { +>nonDistributive : Symbol(nonDistributive, Decl(distributiveConditionalTypeInferencePatterns.ts, 6, 5)) +>T : Symbol(T, Decl(distributiveConditionalTypeInferencePatterns.ts, 8, 36)) +>a : Symbol(a, Decl(distributiveConditionalTypeInferencePatterns.ts, 8, 47)) +>b : Symbol(b, Decl(distributiveConditionalTypeInferencePatterns.ts, 8, 58)) +>value : Symbol(value, Decl(distributiveConditionalTypeInferencePatterns.ts, 8, 72)) +>T : Symbol(T, Decl(distributiveConditionalTypeInferencePatterns.ts, 8, 36)) +>NonDistributive : Symbol(NonDistributive, Decl(distributiveConditionalTypeInferencePatterns.ts, 1, 79)) +>T : Symbol(T, Decl(distributiveConditionalTypeInferencePatterns.ts, 8, 36)) + + return value; // Error +>value : Symbol(value, Decl(distributiveConditionalTypeInferencePatterns.ts, 8, 72)) + } + + export const rejected: Result<{ a: string; b: number }> = { a: "", b: 1 }; // Error +>rejected : Symbol(rejected, Decl(distributiveConditionalTypeInferencePatterns.ts, 12, 16)) +>Result : Symbol(Result, Decl(distributiveConditionalTypeInferencePatterns.ts, 0, 33)) +>a : Symbol(a, Decl(distributiveConditionalTypeInferencePatterns.ts, 12, 35)) +>b : Symbol(b, Decl(distributiveConditionalTypeInferencePatterns.ts, 12, 46)) +>a : Symbol(a, Decl(distributiveConditionalTypeInferencePatterns.ts, 12, 63)) +>b : Symbol(b, Decl(distributiveConditionalTypeInferencePatterns.ts, 12, 70)) + + export const accepted: Result<{ a: string; b: string }> = { a: "", b: "" }; +>accepted : Symbol(accepted, Decl(distributiveConditionalTypeInferencePatterns.ts, 13, 16)) +>Result : Symbol(Result, Decl(distributiveConditionalTypeInferencePatterns.ts, 0, 33)) +>a : Symbol(a, Decl(distributiveConditionalTypeInferencePatterns.ts, 13, 35)) +>b : Symbol(b, Decl(distributiveConditionalTypeInferencePatterns.ts, 13, 46)) +>a : Symbol(a, Decl(distributiveConditionalTypeInferencePatterns.ts, 13, 63)) +>b : Symbol(b, Decl(distributiveConditionalTypeInferencePatterns.ts, 13, 70)) + + type Covariant = T extends { a: infer U; b: infer U } ? T : never; +>Covariant : Symbol(Covariant, Decl(distributiveConditionalTypeInferencePatterns.ts, 13, 79)) +>T : Symbol(T, Decl(distributiveConditionalTypeInferencePatterns.ts, 15, 19)) +>T : Symbol(T, Decl(distributiveConditionalTypeInferencePatterns.ts, 15, 19)) +>a : Symbol(a, Decl(distributiveConditionalTypeInferencePatterns.ts, 15, 35)) +>U : Symbol(U, Decl(distributiveConditionalTypeInferencePatterns.ts, 15, 44), Decl(distributiveConditionalTypeInferencePatterns.ts, 15, 56)) +>b : Symbol(b, Decl(distributiveConditionalTypeInferencePatterns.ts, 15, 47)) +>U : Symbol(U, Decl(distributiveConditionalTypeInferencePatterns.ts, 15, 44), Decl(distributiveConditionalTypeInferencePatterns.ts, 15, 56)) +>T : Symbol(T, Decl(distributiveConditionalTypeInferencePatterns.ts, 15, 19)) + + type ObjectIntersection = T extends { a: infer U } & { b: number } ? T : never; +>ObjectIntersection : Symbol(ObjectIntersection, Decl(distributiveConditionalTypeInferencePatterns.ts, 15, 73)) +>T : Symbol(T, Decl(distributiveConditionalTypeInferencePatterns.ts, 16, 28)) +>T : Symbol(T, Decl(distributiveConditionalTypeInferencePatterns.ts, 16, 28)) +>a : Symbol(a, Decl(distributiveConditionalTypeInferencePatterns.ts, 16, 44)) +>U : Symbol(U, Decl(distributiveConditionalTypeInferencePatterns.ts, 16, 53)) +>b : Symbol(b, Decl(distributiveConditionalTypeInferencePatterns.ts, 16, 61)) +>T : Symbol(T, Decl(distributiveConditionalTypeInferencePatterns.ts, 16, 28)) + + export function covariant(value: T): Covariant { +>covariant : Symbol(covariant, Decl(distributiveConditionalTypeInferencePatterns.ts, 16, 86)) +>T : Symbol(T, Decl(distributiveConditionalTypeInferencePatterns.ts, 18, 30)) +>a : Symbol(a, Decl(distributiveConditionalTypeInferencePatterns.ts, 18, 41)) +>b : Symbol(b, Decl(distributiveConditionalTypeInferencePatterns.ts, 18, 52)) +>value : Symbol(value, Decl(distributiveConditionalTypeInferencePatterns.ts, 18, 66)) +>T : Symbol(T, Decl(distributiveConditionalTypeInferencePatterns.ts, 18, 30)) +>Covariant : Symbol(Covariant, Decl(distributiveConditionalTypeInferencePatterns.ts, 13, 79)) +>T : Symbol(T, Decl(distributiveConditionalTypeInferencePatterns.ts, 18, 30)) + + return value; +>value : Symbol(value, Decl(distributiveConditionalTypeInferencePatterns.ts, 18, 66)) + } + + export function objectIntersection(value: T): ObjectIntersection { +>objectIntersection : Symbol(objectIntersection, Decl(distributiveConditionalTypeInferencePatterns.ts, 20, 5)) +>T : Symbol(T, Decl(distributiveConditionalTypeInferencePatterns.ts, 22, 39)) +>a : Symbol(a, Decl(distributiveConditionalTypeInferencePatterns.ts, 22, 50)) +>b : Symbol(b, Decl(distributiveConditionalTypeInferencePatterns.ts, 22, 61)) +>value : Symbol(value, Decl(distributiveConditionalTypeInferencePatterns.ts, 22, 75)) +>T : Symbol(T, Decl(distributiveConditionalTypeInferencePatterns.ts, 22, 39)) +>ObjectIntersection : Symbol(ObjectIntersection, Decl(distributiveConditionalTypeInferencePatterns.ts, 15, 73)) +>T : Symbol(T, Decl(distributiveConditionalTypeInferencePatterns.ts, 22, 39)) + + return value; +>value : Symbol(value, Decl(distributiveConditionalTypeInferencePatterns.ts, 22, 75)) + } +} + +namespace DependentConstraints { +>DependentConstraints : Symbol(DependentConstraints, Decl(distributiveConditionalTypeInferencePatterns.ts, 25, 1)) + + interface Dep { +>Dep : Symbol(Dep, Decl(distributiveConditionalTypeInferencePatterns.ts, 27, 32)) +>A : Symbol(A, Decl(distributiveConditionalTypeInferencePatterns.ts, 28, 18)) +>B : Symbol(B, Decl(distributiveConditionalTypeInferencePatterns.ts, 28, 20)) +>A : Symbol(A, Decl(distributiveConditionalTypeInferencePatterns.ts, 28, 18)) + + a: A; +>a : Symbol(Dep.a, Decl(distributiveConditionalTypeInferencePatterns.ts, 28, 35)) +>A : Symbol(A, Decl(distributiveConditionalTypeInferencePatterns.ts, 28, 18)) + + b: B; +>b : Symbol(Dep.b, Decl(distributiveConditionalTypeInferencePatterns.ts, 29, 13)) +>B : Symbol(B, Decl(distributiveConditionalTypeInferencePatterns.ts, 28, 20)) + } + + type Result = T extends Dep ? T : never; +>Result : Symbol(Result, Decl(distributiveConditionalTypeInferencePatterns.ts, 31, 5)) +>T : Symbol(T, Decl(distributiveConditionalTypeInferencePatterns.ts, 33, 16)) +>T : Symbol(T, Decl(distributiveConditionalTypeInferencePatterns.ts, 33, 16)) +>Dep : Symbol(Dep, Decl(distributiveConditionalTypeInferencePatterns.ts, 27, 32)) +>A : Symbol(A, Decl(distributiveConditionalTypeInferencePatterns.ts, 33, 40)) +>B : Symbol(B, Decl(distributiveConditionalTypeInferencePatterns.ts, 33, 49)) +>T : Symbol(T, Decl(distributiveConditionalTypeInferencePatterns.ts, 33, 16)) + + type NonDistributive = [T] extends [Dep] ? T : never; +>NonDistributive : Symbol(NonDistributive, Decl(distributiveConditionalTypeInferencePatterns.ts, 33, 65)) +>T : Symbol(T, Decl(distributiveConditionalTypeInferencePatterns.ts, 34, 25)) +>T : Symbol(T, Decl(distributiveConditionalTypeInferencePatterns.ts, 34, 25)) +>Dep : Symbol(Dep, Decl(distributiveConditionalTypeInferencePatterns.ts, 27, 32)) +>A : Symbol(A, Decl(distributiveConditionalTypeInferencePatterns.ts, 34, 52)) +>B : Symbol(B, Decl(distributiveConditionalTypeInferencePatterns.ts, 34, 61)) +>T : Symbol(T, Decl(distributiveConditionalTypeInferencePatterns.ts, 34, 25)) + + export function generic(value: T): Result { +>generic : Symbol(generic, Decl(distributiveConditionalTypeInferencePatterns.ts, 34, 78)) +>T : Symbol(T, Decl(distributiveConditionalTypeInferencePatterns.ts, 36, 28)) +>a : Symbol(a, Decl(distributiveConditionalTypeInferencePatterns.ts, 36, 39)) +>b : Symbol(b, Decl(distributiveConditionalTypeInferencePatterns.ts, 36, 50)) +>value : Symbol(value, Decl(distributiveConditionalTypeInferencePatterns.ts, 36, 64)) +>T : Symbol(T, Decl(distributiveConditionalTypeInferencePatterns.ts, 36, 28)) +>Result : Symbol(Result, Decl(distributiveConditionalTypeInferencePatterns.ts, 31, 5)) +>T : Symbol(T, Decl(distributiveConditionalTypeInferencePatterns.ts, 36, 28)) + + return value; // Error: the inferred B must satisfy the same A. +>value : Symbol(value, Decl(distributiveConditionalTypeInferencePatterns.ts, 36, 64)) + } + + export function nonDistributive(value: T): NonDistributive { +>nonDistributive : Symbol(nonDistributive, Decl(distributiveConditionalTypeInferencePatterns.ts, 38, 5)) +>T : Symbol(T, Decl(distributiveConditionalTypeInferencePatterns.ts, 40, 36)) +>a : Symbol(a, Decl(distributiveConditionalTypeInferencePatterns.ts, 40, 47)) +>b : Symbol(b, Decl(distributiveConditionalTypeInferencePatterns.ts, 40, 58)) +>value : Symbol(value, Decl(distributiveConditionalTypeInferencePatterns.ts, 40, 72)) +>T : Symbol(T, Decl(distributiveConditionalTypeInferencePatterns.ts, 40, 36)) +>NonDistributive : Symbol(NonDistributive, Decl(distributiveConditionalTypeInferencePatterns.ts, 33, 65)) +>T : Symbol(T, Decl(distributiveConditionalTypeInferencePatterns.ts, 40, 36)) + + return value; // Error +>value : Symbol(value, Decl(distributiveConditionalTypeInferencePatterns.ts, 40, 72)) + } + + export const rejected: Result<{ a: string; b: number }> = { a: "", b: 1 }; // Error +>rejected : Symbol(rejected, Decl(distributiveConditionalTypeInferencePatterns.ts, 44, 16)) +>Result : Symbol(Result, Decl(distributiveConditionalTypeInferencePatterns.ts, 31, 5)) +>a : Symbol(a, Decl(distributiveConditionalTypeInferencePatterns.ts, 44, 35)) +>b : Symbol(b, Decl(distributiveConditionalTypeInferencePatterns.ts, 44, 46)) +>a : Symbol(a, Decl(distributiveConditionalTypeInferencePatterns.ts, 44, 63)) +>b : Symbol(b, Decl(distributiveConditionalTypeInferencePatterns.ts, 44, 70)) + + export const accepted: Result<{ a: string; b: string }> = { a: "", b: "" }; +>accepted : Symbol(accepted, Decl(distributiveConditionalTypeInferencePatterns.ts, 45, 16)) +>Result : Symbol(Result, Decl(distributiveConditionalTypeInferencePatterns.ts, 31, 5)) +>a : Symbol(a, Decl(distributiveConditionalTypeInferencePatterns.ts, 45, 35)) +>b : Symbol(b, Decl(distributiveConditionalTypeInferencePatterns.ts, 45, 46)) +>a : Symbol(a, Decl(distributiveConditionalTypeInferencePatterns.ts, 45, 63)) +>b : Symbol(b, Decl(distributiveConditionalTypeInferencePatterns.ts, 45, 70)) + + interface Independent { +>Independent : Symbol(Independent, Decl(distributiveConditionalTypeInferencePatterns.ts, 45, 79)) +>A : Symbol(A, Decl(distributiveConditionalTypeInferencePatterns.ts, 47, 26)) +>B : Symbol(B, Decl(distributiveConditionalTypeInferencePatterns.ts, 47, 28)) + + a: A; +>a : Symbol(Independent.a, Decl(distributiveConditionalTypeInferencePatterns.ts, 47, 48)) +>A : Symbol(A, Decl(distributiveConditionalTypeInferencePatterns.ts, 47, 26)) + + b: B; +>b : Symbol(Independent.b, Decl(distributiveConditionalTypeInferencePatterns.ts, 48, 13)) +>B : Symbol(B, Decl(distributiveConditionalTypeInferencePatterns.ts, 47, 28)) + } + + type IndependentResult = T extends Independent ? T : never; +>IndependentResult : Symbol(IndependentResult, Decl(distributiveConditionalTypeInferencePatterns.ts, 50, 5)) +>T : Symbol(T, Decl(distributiveConditionalTypeInferencePatterns.ts, 52, 27)) +>T : Symbol(T, Decl(distributiveConditionalTypeInferencePatterns.ts, 52, 27)) +>Independent : Symbol(Independent, Decl(distributiveConditionalTypeInferencePatterns.ts, 45, 79)) +>A : Symbol(A, Decl(distributiveConditionalTypeInferencePatterns.ts, 52, 59)) +>B : Symbol(B, Decl(distributiveConditionalTypeInferencePatterns.ts, 52, 68)) +>T : Symbol(T, Decl(distributiveConditionalTypeInferencePatterns.ts, 52, 27)) + + export function independent(value: T): IndependentResult { +>independent : Symbol(independent, Decl(distributiveConditionalTypeInferencePatterns.ts, 52, 84)) +>T : Symbol(T, Decl(distributiveConditionalTypeInferencePatterns.ts, 54, 32)) +>a : Symbol(a, Decl(distributiveConditionalTypeInferencePatterns.ts, 54, 43)) +>b : Symbol(b, Decl(distributiveConditionalTypeInferencePatterns.ts, 54, 54)) +>value : Symbol(value, Decl(distributiveConditionalTypeInferencePatterns.ts, 54, 68)) +>T : Symbol(T, Decl(distributiveConditionalTypeInferencePatterns.ts, 54, 32)) +>IndependentResult : Symbol(IndependentResult, Decl(distributiveConditionalTypeInferencePatterns.ts, 50, 5)) +>T : Symbol(T, Decl(distributiveConditionalTypeInferencePatterns.ts, 54, 32)) + + return value; +>value : Symbol(value, Decl(distributiveConditionalTypeInferencePatterns.ts, 54, 68)) + } +} + +namespace InferredBranchDependence { +>InferredBranchDependence : Symbol(InferredBranchDependence, Decl(distributiveConditionalTypeInferencePatterns.ts, 57, 1)) + + type Box = T extends infer U ? { value?: U } : never; +>Box : Symbol(Box, Decl(distributiveConditionalTypeInferencePatterns.ts, 59, 36)) +>T : Symbol(T, Decl(distributiveConditionalTypeInferencePatterns.ts, 60, 13)) +>T : Symbol(T, Decl(distributiveConditionalTypeInferencePatterns.ts, 60, 13)) +>U : Symbol(U, Decl(distributiveConditionalTypeInferencePatterns.ts, 60, 33)) +>value : Symbol(value, Decl(distributiveConditionalTypeInferencePatterns.ts, 60, 39)) +>U : Symbol(U, Decl(distributiveConditionalTypeInferencePatterns.ts, 60, 33)) + + type PropertyBox = T extends { value: infer U } ? { copy?: U } : never; +>PropertyBox : Symbol(PropertyBox, Decl(distributiveConditionalTypeInferencePatterns.ts, 60, 60)) +>T : Symbol(T, Decl(distributiveConditionalTypeInferencePatterns.ts, 61, 21)) +>T : Symbol(T, Decl(distributiveConditionalTypeInferencePatterns.ts, 61, 21)) +>value : Symbol(value, Decl(distributiveConditionalTypeInferencePatterns.ts, 61, 37)) +>U : Symbol(U, Decl(distributiveConditionalTypeInferencePatterns.ts, 61, 50)) +>copy : Symbol(copy, Decl(distributiveConditionalTypeInferencePatterns.ts, 61, 58)) +>U : Symbol(U, Decl(distributiveConditionalTypeInferencePatterns.ts, 61, 50)) + + type Constant = T extends infer U ? { done: true } : never; +>Constant : Symbol(Constant, Decl(distributiveConditionalTypeInferencePatterns.ts, 61, 78)) +>T : Symbol(T, Decl(distributiveConditionalTypeInferencePatterns.ts, 62, 18)) +>T : Symbol(T, Decl(distributiveConditionalTypeInferencePatterns.ts, 62, 18)) +>U : Symbol(U, Decl(distributiveConditionalTypeInferencePatterns.ts, 62, 38)) +>done : Symbol(done, Decl(distributiveConditionalTypeInferencePatterns.ts, 62, 44)) + + export function box(): Box { +>box : Symbol(box, Decl(distributiveConditionalTypeInferencePatterns.ts, 62, 66)) +>T : Symbol(T, Decl(distributiveConditionalTypeInferencePatterns.ts, 64, 24)) +>Box : Symbol(Box, Decl(distributiveConditionalTypeInferencePatterns.ts, 59, 36)) +>T : Symbol(T, Decl(distributiveConditionalTypeInferencePatterns.ts, 64, 24)) + + return {}; // Error: Box is never even though the branch only references U. + } + + export function propertyBox(): PropertyBox { +>propertyBox : Symbol(propertyBox, Decl(distributiveConditionalTypeInferencePatterns.ts, 66, 5)) +>T : Symbol(T, Decl(distributiveConditionalTypeInferencePatterns.ts, 68, 32)) +>value : Symbol(value, Decl(distributiveConditionalTypeInferencePatterns.ts, 68, 43)) +>PropertyBox : Symbol(PropertyBox, Decl(distributiveConditionalTypeInferencePatterns.ts, 60, 60)) +>T : Symbol(T, Decl(distributiveConditionalTypeInferencePatterns.ts, 68, 32)) + + return {}; // Error + } + + export function constant(): Constant { +>constant : Symbol(constant, Decl(distributiveConditionalTypeInferencePatterns.ts, 70, 5)) +>T : Symbol(T, Decl(distributiveConditionalTypeInferencePatterns.ts, 72, 29)) +>Constant : Symbol(Constant, Decl(distributiveConditionalTypeInferencePatterns.ts, 61, 78)) +>T : Symbol(T, Decl(distributiveConditionalTypeInferencePatterns.ts, 72, 29)) + + return { done: true }; // Error: an unused infer binder still requires a nonempty check type. +>done : Symbol(done, Decl(distributiveConditionalTypeInferencePatterns.ts, 73, 16)) + } + + export const rejectedBox: Box = {}; // Error +>rejectedBox : Symbol(rejectedBox, Decl(distributiveConditionalTypeInferencePatterns.ts, 76, 16)) +>Box : Symbol(Box, Decl(distributiveConditionalTypeInferencePatterns.ts, 59, 36)) + + export const rejectedProperty: PropertyBox = {}; // Error +>rejectedProperty : Symbol(rejectedProperty, Decl(distributiveConditionalTypeInferencePatterns.ts, 77, 16)) +>PropertyBox : Symbol(PropertyBox, Decl(distributiveConditionalTypeInferencePatterns.ts, 60, 60)) + + export const rejectedConstant: Constant = { done: true }; // Error +>rejectedConstant : Symbol(rejectedConstant, Decl(distributiveConditionalTypeInferencePatterns.ts, 78, 16)) +>Constant : Symbol(Constant, Decl(distributiveConditionalTypeInferencePatterns.ts, 61, 78)) +>done : Symbol(done, Decl(distributiveConditionalTypeInferencePatterns.ts, 78, 54)) + + type NonDistributive = [T] extends [infer U] ? { value?: U } : never; +>NonDistributive : Symbol(NonDistributive, Decl(distributiveConditionalTypeInferencePatterns.ts, 78, 68)) +>T : Symbol(T, Decl(distributiveConditionalTypeInferencePatterns.ts, 80, 25)) +>T : Symbol(T, Decl(distributiveConditionalTypeInferencePatterns.ts, 80, 25)) +>U : Symbol(U, Decl(distributiveConditionalTypeInferencePatterns.ts, 80, 48)) +>value : Symbol(value, Decl(distributiveConditionalTypeInferencePatterns.ts, 80, 55)) +>U : Symbol(U, Decl(distributiveConditionalTypeInferencePatterns.ts, 80, 48)) + + export function nonDistributive(): NonDistributive { +>nonDistributive : Symbol(nonDistributive, Decl(distributiveConditionalTypeInferencePatterns.ts, 80, 76)) +>T : Symbol(T, Decl(distributiveConditionalTypeInferencePatterns.ts, 82, 36)) +>NonDistributive : Symbol(NonDistributive, Decl(distributiveConditionalTypeInferencePatterns.ts, 78, 68)) +>T : Symbol(T, Decl(distributiveConditionalTypeInferencePatterns.ts, 82, 36)) + + return {}; + } + + export const emptyNonDistributive: NonDistributive = {}; +>emptyNonDistributive : Symbol(emptyNonDistributive, Decl(distributiveConditionalTypeInferencePatterns.ts, 86, 16)) +>NonDistributive : Symbol(NonDistributive, Decl(distributiveConditionalTypeInferencePatterns.ts, 78, 68)) + + export function nonNeverUnion(): Box { +>nonNeverUnion : Symbol(nonNeverUnion, Decl(distributiveConditionalTypeInferencePatterns.ts, 86, 67)) +>T : Symbol(T, Decl(distributiveConditionalTypeInferencePatterns.ts, 88, 34)) +>Box : Symbol(Box, Decl(distributiveConditionalTypeInferencePatterns.ts, 59, 36)) +>T : Symbol(T, Decl(distributiveConditionalTypeInferencePatterns.ts, 88, 34)) + + return {}; + } + + export function nonNeverProperty(): PropertyBox { +>nonNeverProperty : Symbol(nonNeverProperty, Decl(distributiveConditionalTypeInferencePatterns.ts, 90, 5)) +>T : Symbol(T, Decl(distributiveConditionalTypeInferencePatterns.ts, 92, 37)) +>value : Symbol(value, Decl(distributiveConditionalTypeInferencePatterns.ts, 92, 48)) +>PropertyBox : Symbol(PropertyBox, Decl(distributiveConditionalTypeInferencePatterns.ts, 60, 60)) +>T : Symbol(T, Decl(distributiveConditionalTypeInferencePatterns.ts, 92, 37)) +>value : Symbol(value, Decl(distributiveConditionalTypeInferencePatterns.ts, 92, 87)) + + return {}; + } + + export function nonNeverObject(): Box<{ value: T }> { +>nonNeverObject : Symbol(nonNeverObject, Decl(distributiveConditionalTypeInferencePatterns.ts, 94, 5)) +>T : Symbol(T, Decl(distributiveConditionalTypeInferencePatterns.ts, 96, 35)) +>Box : Symbol(Box, Decl(distributiveConditionalTypeInferencePatterns.ts, 59, 36)) +>value : Symbol(value, Decl(distributiveConditionalTypeInferencePatterns.ts, 96, 46)) +>T : Symbol(T, Decl(distributiveConditionalTypeInferencePatterns.ts, 96, 35)) + + return {}; + } + + export function nonNeverConditional(): Box<[T] extends [never] ? { value: T } : number> { +>nonNeverConditional : Symbol(nonNeverConditional, Decl(distributiveConditionalTypeInferencePatterns.ts, 98, 5)) +>T : Symbol(T, Decl(distributiveConditionalTypeInferencePatterns.ts, 100, 40)) +>Box : Symbol(Box, Decl(distributiveConditionalTypeInferencePatterns.ts, 59, 36)) +>T : Symbol(T, Decl(distributiveConditionalTypeInferencePatterns.ts, 100, 40)) +>value : Symbol(value, Decl(distributiveConditionalTypeInferencePatterns.ts, 100, 73)) +>T : Symbol(T, Decl(distributiveConditionalTypeInferencePatterns.ts, 100, 40)) + + return {}; + } + + type Identity = T extends infer U ? U : never; +>Identity : Symbol(Identity, Decl(distributiveConditionalTypeInferencePatterns.ts, 102, 5)) +>T : Symbol(T, Decl(distributiveConditionalTypeInferencePatterns.ts, 104, 18)) +>T : Symbol(T, Decl(distributiveConditionalTypeInferencePatterns.ts, 104, 18)) +>U : Symbol(U, Decl(distributiveConditionalTypeInferencePatterns.ts, 104, 38)) +>U : Symbol(U, Decl(distributiveConditionalTypeInferencePatterns.ts, 104, 38)) + + export function identity(value: T): Identity { +>identity : Symbol(identity, Decl(distributiveConditionalTypeInferencePatterns.ts, 104, 53)) +>T : Symbol(T, Decl(distributiveConditionalTypeInferencePatterns.ts, 106, 29)) +>value : Symbol(value, Decl(distributiveConditionalTypeInferencePatterns.ts, 106, 32)) +>T : Symbol(T, Decl(distributiveConditionalTypeInferencePatterns.ts, 106, 29)) +>Identity : Symbol(Identity, Decl(distributiveConditionalTypeInferencePatterns.ts, 102, 5)) +>T : Symbol(T, Decl(distributiveConditionalTypeInferencePatterns.ts, 106, 29)) + + return value; +>value : Symbol(value, Decl(distributiveConditionalTypeInferencePatterns.ts, 106, 32)) + } +} + diff --git a/tsc/testdata/baselines/reference/compiler/distributiveConditionalTypeInferencePatterns.types b/tsc/testdata/baselines/reference/compiler/distributiveConditionalTypeInferencePatterns.types new file mode 100644 index 0000000000000..99b26421c3262 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/distributiveConditionalTypeInferencePatterns.types @@ -0,0 +1,280 @@ +//// [tests/cases/compiler/distributiveConditionalTypeInferencePatterns.ts] //// + +=== distributiveConditionalTypeInferencePatterns.ts === +namespace IntersectionInference { +>IntersectionInference : typeof IntersectionInference + + type Result = T extends { a: infer U & string; b: infer U } ? T : never; +>Result : Result +>a : U & string +>b : U + + type NonDistributive = [T] extends [{ a: infer U & string; b: infer U }] ? T : never; +>NonDistributive : NonDistributive +>a : U & string +>b : U + + export function generic(value: T): Result { +>generic : (value: T) => Result +>a : string +>b : number +>value : T + + return value; // Error: string is removed before inferring U from b. +>value : T + } + + export function nonDistributive(value: T): NonDistributive { +>nonDistributive : (value: T) => NonDistributive +>a : string +>b : number +>value : T + + return value; // Error +>value : T + } + + export const rejected: Result<{ a: string; b: number }> = { a: "", b: 1 }; // Error +>rejected : never +>a : string +>b : number +>{ a: "", b: 1 } : { a: string; b: number; } +>a : string +>"" : "" +>b : number +>1 : 1 + + export const accepted: Result<{ a: string; b: string }> = { a: "", b: "" }; +>accepted : { a: string; b: string; } +>a : string +>b : string +>{ a: "", b: "" } : { a: string; b: string; } +>a : string +>"" : "" +>b : string +>"" : "" + + type Covariant = T extends { a: infer U; b: infer U } ? T : never; +>Covariant : Covariant +>a : U +>b : U + + type ObjectIntersection = T extends { a: infer U } & { b: number } ? T : never; +>ObjectIntersection : ObjectIntersection +>a : U +>b : number + + export function covariant(value: T): Covariant { +>covariant : (value: T) => Covariant +>a : string +>b : number +>value : T + + return value; +>value : T + } + + export function objectIntersection(value: T): ObjectIntersection { +>objectIntersection : (value: T) => ObjectIntersection +>a : string +>b : number +>value : T + + return value; +>value : T + } +} + +namespace DependentConstraints { +>DependentConstraints : typeof DependentConstraints + + interface Dep { + a: A; +>a : A + + b: B; +>b : B + } + + type Result = T extends Dep ? T : never; +>Result : Result + + type NonDistributive = [T] extends [Dep] ? T : never; +>NonDistributive : NonDistributive + + export function generic(value: T): Result { +>generic : (value: T) => Result +>a : string +>b : number +>value : T + + return value; // Error: the inferred B must satisfy the same A. +>value : T + } + + export function nonDistributive(value: T): NonDistributive { +>nonDistributive : (value: T) => NonDistributive +>a : string +>b : number +>value : T + + return value; // Error +>value : T + } + + export const rejected: Result<{ a: string; b: number }> = { a: "", b: 1 }; // Error +>rejected : never +>a : string +>b : number +>{ a: "", b: 1 } : { a: string; b: number; } +>a : string +>"" : "" +>b : number +>1 : 1 + + export const accepted: Result<{ a: string; b: string }> = { a: "", b: "" }; +>accepted : { a: string; b: string; } +>a : string +>b : string +>{ a: "", b: "" } : { a: string; b: string; } +>a : string +>"" : "" +>b : string +>"" : "" + + interface Independent { + a: A; +>a : A + + b: B; +>b : B + } + + type IndependentResult = T extends Independent ? T : never; +>IndependentResult : IndependentResult + + export function independent(value: T): IndependentResult { +>independent : (value: T) => IndependentResult +>a : number +>b : string +>value : T + + return value; +>value : T + } +} + +namespace InferredBranchDependence { +>InferredBranchDependence : typeof InferredBranchDependence + + type Box = T extends infer U ? { value?: U } : never; +>Box : Box +>value : U | undefined + + type PropertyBox = T extends { value: infer U } ? { copy?: U } : never; +>PropertyBox : PropertyBox +>value : U +>copy : U | undefined + + type Constant = T extends infer U ? { done: true } : never; +>Constant : Constant +>done : true +>true : true + + export function box(): Box { +>box : () => Box + + return {}; // Error: Box is never even though the branch only references U. +>{} : {} + } + + export function propertyBox(): PropertyBox { +>propertyBox : () => PropertyBox +>value : unknown + + return {}; // Error +>{} : {} + } + + export function constant(): Constant { +>constant : () => Constant + + return { done: true }; // Error: an unused infer binder still requires a nonempty check type. +>{ done: true } : { done: true; } +>done : true +>true : true + } + + export const rejectedBox: Box = {}; // Error +>rejectedBox : never +>{} : {} + + export const rejectedProperty: PropertyBox = {}; // Error +>rejectedProperty : never +>{} : {} + + export const rejectedConstant: Constant = { done: true }; // Error +>rejectedConstant : never +>{ done: true } : { done: boolean; } +>done : boolean +>true : true + + type NonDistributive = [T] extends [infer U] ? { value?: U } : never; +>NonDistributive : NonDistributive +>value : U | undefined + + export function nonDistributive(): NonDistributive { +>nonDistributive : () => NonDistributive + + return {}; +>{} : {} + } + + export const emptyNonDistributive: NonDistributive = {}; +>emptyNonDistributive : { value?: undefined; } +>{} : {} + + export function nonNeverUnion(): Box { +>nonNeverUnion : () => Box + + return {}; +>{} : {} + } + + export function nonNeverProperty(): PropertyBox { +>nonNeverProperty : () => PropertyBox +>value : unknown +>value : number + + return {}; +>{} : {} + } + + export function nonNeverObject(): Box<{ value: T }> { +>nonNeverObject : () => Box<{ value: T; }> +>value : T + + return {}; +>{} : {} + } + + export function nonNeverConditional(): Box<[T] extends [never] ? { value: T } : number> { +>nonNeverConditional : () => Box<[T] extends [never] ? { value: T; } : number> +>value : T + + return {}; +>{} : {} + } + + type Identity = T extends infer U ? U : never; +>Identity : Identity + + export function identity(value: T): Identity { +>identity : (value: T) => Identity +>value : T + + return value; +>value : T + } +} + diff --git a/tsc/testdata/baselines/reference/compiler/distributiveConditionalTypeRelations.errors.txt b/tsc/testdata/baselines/reference/compiler/distributiveConditionalTypeRelations.errors.txt new file mode 100644 index 0000000000000..7a21f2ed18a39 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/distributiveConditionalTypeRelations.errors.txt @@ -0,0 +1,534 @@ +distributiveConditionalTypeRelations.ts(24,11): error TS2322: Type '`${T}-${T}`' is not assignable to type 'Repeated'. + Type '`${string}-${string}`' is not assignable to type 'Repeated'. +distributiveConditionalTypeRelations.ts(30,11): error TS2322: Type '`${T}-${T}`' is not assignable to type 'WithOuter'. + Type '`${string}-${string}`' is not assignable to type 'WithOuter'. +distributiveConditionalTypeRelations.ts(39,11): error TS2322: Type '`outer-${OuterSource}`' is not assignable to type 'OuterRepeated'. + Type '`outer-${string}-${string}`' is not assignable to type 'OuterRepeated'. +distributiveConditionalTypeRelations.ts(43,11): error TS2322: Type '`${Uppercase}-${Uppercase}`' is not assignable to type 'UpperRepeated'. + Type '`${Uppercase}-${Uppercase}`' is not assignable to type 'UpperRepeated'. + Type '`${string}-${string}`' is not assignable to type 'UpperRepeated'. +distributiveConditionalTypeRelations.ts(47,11): error TS2322: Type '`outer-${InferredRepeated}`' is not assignable to type 'OuterRepeated'. + Type '`outer-${string}-${string}`' is not assignable to type 'OuterRepeated'. +distributiveConditionalTypeRelations.ts(53,11): error TS2322: Type 'T & { value: T; }' is not assignable to type 'Linked'. +distributiveConditionalTypeRelations.ts(65,11): error TS2322: Type '{ x: T; y: T; }' is not assignable to type 'Pair'. +distributiveConditionalTypeRelations.ts(66,11): error TS2322: Type '[T, T]' is not assignable to type 'Tuple'. +distributiveConditionalTypeRelations.ts(67,11): error TS2322: Type '(value: T) => T' is not assignable to type 'Callback'. +distributiveConditionalTypeRelations.ts(117,11): error TS2322: Type 'T' is not assignable to type 'Inferred'. + Type '(value: any) => unknown' is not assignable to type 'Inferred'. +distributiveConditionalTypeRelations.ts(118,11): error TS2322: Type 'T' is not assignable to type 'InferredNonDistributive'. + Type '(value: any) => unknown' is not assignable to type 'InferredNonDistributive'. +distributiveConditionalTypeRelations.ts(134,11): error TS2322: Type 'T' is not assignable to type 'T extends (value: infer P) => infer R ? P | R | T : never'. + Type '(value: any) => unknown' is not assignable to type 'T extends (value: infer P) => infer R ? P | R | T : never'. +distributiveConditionalTypeRelations.ts(140,11): error TS2322: Type 'T' is not assignable to type 'InferredReturn'. + Type '() => unknown' is not assignable to type 'InferredReturn'. +distributiveConditionalTypeRelations.ts(157,11): error TS2322: Type 'T' is not assignable to type 'InferredMethod'. + Type '{ method(value: unknown): never; }' is not assignable to type 'InferredMethod'. +distributiveConditionalTypeRelations.ts(158,11): error TS2322: Type 'T' is not assignable to type 'InferredMethodReturn'. + Type '{ method(value: unknown): never; }' is not assignable to type 'InferredMethodReturn'. +distributiveConditionalTypeRelations.ts(162,11): error TS2322: Type 'string' is not assignable to type 'InferredValue'. +distributiveConditionalTypeRelations.ts(163,11): error TS2322: Type '(value: string) => string' is not assignable to type 'InferredCallback'. +distributiveConditionalTypeRelations.ts(167,11): error TS2322: Type 'T' is not assignable to type 'ConstrainedInfer'. + Type '{ value: any; }' is not assignable to type 'ConstrainedInfer'. +distributiveConditionalTypeRelations.ts(180,11): error TS2322: Type 'T' is not assignable to type 'SpecificParameter'. + Type '(value: any) => unknown' is not assignable to type 'SpecificParameter'. +distributiveConditionalTypeRelations.ts(181,11): error TS2322: Type 'T' is not assignable to type 'T extends (value: string) => unknown ? T : never'. + Type '(value: any) => unknown' is not assignable to type 'T extends (value: string) => unknown ? T : never'. +distributiveConditionalTypeRelations.ts(185,11): error TS2322: Type 'T' is not assignable to type 'SpecificMethod'. + Type '{ method(value: unknown): unknown; }' is not assignable to type 'SpecificMethod'. +distributiveConditionalTypeRelations.ts(189,11): error TS2322: Type 'T' is not assignable to type 'OptionalProperty'. + Type '{ value: unknown; }' is not assignable to type 'OptionalProperty'. +distributiveConditionalTypeRelations.ts(193,11): error TS2322: Type 'T' is not assignable to type 'MixedInference'. + Type '{ input: (value: any) => void; output: any; }' is not assignable to type 'MixedInference'. +distributiveConditionalTypeRelations.ts(204,11): error TS2322: Type 'T' is not assignable to type 'InvariantInference'. + Type 'InvariantInferencePattern' is not assignable to type 'InvariantInference'. +distributiveConditionalTypeRelations.ts(214,11): error TS2322: Type 'T' is not assignable to type 'NumericInference'. + Type '(value: unknown) => number' is not assignable to type 'NumericInference'. +distributiveConditionalTypeRelations.ts(218,11): error TS2322: Type 'T' is not assignable to type 'SingleParameter'. + Type '(...args: any[]) => unknown' is not assignable to type 'SingleParameter'. +distributiveConditionalTypeRelations.ts(222,11): error TS2322: Type 'T' is not assignable to type 'OptionalParameter'. + Type '(value?: any) => unknown' is not assignable to type 'OptionalParameter'. +distributiveConditionalTypeRelations.ts(226,11): error TS2322: Type 'T' is not assignable to type 'IntersectionPattern'. + Type '{ value: any; method(value: any): unknown; }' is not assignable to type 'IntersectionPattern'. +distributiveConditionalTypeRelations.ts(230,11): error TS2322: Type 'T' is not assignable to type 'PrimitiveBoundary'. + Type '{ toString(): string; }' is not assignable to type 'PrimitiveBoundary'. +distributiveConditionalTypeRelations.ts(239,11): error TS2322: Type 'T' is not assignable to type 'T extends Nominal ? P | T : never'. + Type '{ value: unknown; }' is not assignable to type 'T extends Nominal ? P | T : never'. +distributiveConditionalTypeRelations.ts(269,11): error TS2322: Type 'T & string' is not assignable to type 'Extract'. +distributiveConditionalTypeRelations.ts(273,7): error TS2322: Type '{ x: "a"; y: "b"; }' is not assignable to type '{ x: "a"; y: "a"; } | { x: "b"; y: "b"; }'. + Types of property 'y' are incompatible. + Type '"b"' is not assignable to type '"a"'. +distributiveConditionalTypeRelations.ts(274,7): error TS2322: Type '["a", "b"]' is not assignable to type '["a", "a"] | ["b", "b"]'. + Type '["a", "b"]' is not assignable to type '["a", "a"]'. + Type at position 1 in source is not compatible with type at position 1 in target. + Type '"b"' is not assignable to type '"a"'. +distributiveConditionalTypeRelations.ts(275,7): error TS2322: Type '"a-b"' is not assignable to type '"a-a" | "b-b"'. +distributiveConditionalTypeRelations.ts(276,7): error TS2322: Type '{ a: number; b: number; }' is not assignable to type 'never'. +distributiveConditionalTypeRelations.ts(277,7): error TS2322: Type '"b"' is not assignable to type '"a"'. +distributiveConditionalTypeRelations.ts(278,7): error TS2322: Type '{ value: number; }' is not assignable to type 'never'. +distributiveConditionalTypeRelations.ts(279,7): error TS2322: Type '(value: number) => boolean' is not assignable to type 'never'. +distributiveConditionalTypeRelations.ts(280,7): error TS2322: Type '{ method: (value: number) => boolean; }' is not assignable to type 'never'. +distributiveConditionalTypeRelations.ts(281,7): error TS2322: Type '{ optional: number; value: boolean; }' is not assignable to type 'never'. +distributiveConditionalTypeRelations.ts(282,7): error TS2322: Type '{ input: (value: number) => void; output: string; }' is not assignable to type 'never'. +distributiveConditionalTypeRelations.ts(283,7): error TS2322: Type '{ input: (value: number) => void; output: string; }' is not assignable to type 'never'. +distributiveConditionalTypeRelations.ts(284,7): error TS2322: Type '(value: unknown) => number' is not assignable to type 'never'. +distributiveConditionalTypeRelations.ts(285,7): error TS2322: Type '(left: number, right: number) => void' is not assignable to type 'never'. +distributiveConditionalTypeRelations.ts(286,7): error TS2322: Type '(value: string) => void' is not assignable to type 'never'. +distributiveConditionalTypeRelations.ts(287,7): error TS2322: Type '{ value: boolean; method: (value: number) => void; }' is not assignable to type 'never'. +distributiveConditionalTypeRelations.ts(288,7): error TS2322: Type '""' is not assignable to type 'never'. +distributiveConditionalTypeRelations.ts(295,7): error TS2322: Type '{ (value: string): void; (left: number, right: number): void; }' is not assignable to type 'never'. +distributiveConditionalTypeRelations.ts(296,7): error TS2322: Type '{ (value: string): void; (left: number, right: number): void; }' is not assignable to type 'never'. +distributiveConditionalTypeRelations.ts(303,7): error TS2322: Type '{ (): string; (value: number): number; }' is not assignable to type 'never'. +distributiveConditionalTypeRelations.ts(310,7): error TS2322: Type '{ method(value: string): never; method(left: number, right: number): void; }' is not assignable to type 'never'. +distributiveConditionalTypeRelations.ts(314,7): error TS2322: Type '{ method(value: never): never; }' is not assignable to type 'never'. + + +==== distributiveConditionalTypeRelations.ts (52 errors) ==== + // The original #27932 cases are in conditionalTypeAssignabilityWhenDeferred.ts. + + type Either = T extends U ? T | { matched: true } : T | { matched: false }; + type Both = T extends U ? T & U : T; + type Constant = T extends string ? { a: number } : { b: number }; + type Pair = T extends unknown ? { x: T; y: T } : never; + type Tuple = T extends unknown ? [T, T] : never; + type Callback = T extends unknown ? (value: T) => T : never; + type Repeated = T extends unknown ? `${T}-${T}` : never; + type Remapped = T extends unknown ? `key-${T}` : never; + type Upper = T extends unknown ? Uppercase : never; + + function identity(value: T, intersection: T & U) { + const either: Either = value; + const both: Both = value; + const intersected: Either = intersection; + const extracted: Extract = value; + const excluded: Exclude = value; + } + + function wrappers(value: `key-${T}`, upper: Uppercase, repeated: `${T}-${T}`) { + const remapped: Remapped = value; + const capitalized: Upper = upper; + const correlated: Repeated = repeated; // Error: cross products do not distribute. + ~~~~~~~~~~ +!!! error TS2322: Type '`${T}-${T}`' is not assignable to type 'Repeated'. +!!! error TS2322: Type '`${string}-${string}`' is not assignable to type 'Repeated'. + } + + type WithOuter = T extends unknown ? `${T}-${U}` : never; + + function retainOuterArgument(value: `${T}-${T}`) { + const result: WithOuter = value; // Error: the uniform-instantiation proof conservatively rejects repeated occurrences. + ~~~~~~ +!!! error TS2322: Type '`${T}-${T}`' is not assignable to type 'WithOuter'. +!!! error TS2322: Type '`${string}-${string}`' is not assignable to type 'WithOuter'. + } + + type OuterSource = T extends unknown ? `${T}-${U}` : never; + type OuterRepeated = T extends unknown ? `outer-${T}-${T}` : never; + type UpperRepeated = T extends unknown ? `${Uppercase}-${Uppercase}` : never; + type InferredRepeated = [T] extends [infer U extends string] ? `${U}-${U}` : never; + + function retainConditionalArgument(value: `outer-${OuterSource}`) { + const correlated: OuterRepeated = value; // Error: the second argument is not distributed. + ~~~~~~~~~~ +!!! error TS2322: Type '`outer-${OuterSource}`' is not assignable to type 'OuterRepeated'. +!!! error TS2322: Type '`outer-${string}-${string}`' is not assignable to type 'OuterRepeated'. + } + + function cachedOccurrences(value: `${Uppercase}-${Uppercase}`) { + const correlated: UpperRepeated = value; // Error: a cached inner type still occurs twice. + ~~~~~~~~~~ +!!! error TS2322: Type '`${Uppercase}-${Uppercase}`' is not assignable to type 'UpperRepeated'. +!!! error TS2322: Type '`${Uppercase}-${Uppercase}`' is not assignable to type 'UpperRepeated'. +!!! error TS2322: Type '`${string}-${string}`' is not assignable to type 'UpperRepeated'. + } + + function deferredOccurrences(value: `outer-${InferredRepeated}`) { + const correlated: OuterRepeated = value; // Error: deferred inference hides a non-distributive use. + ~~~~~~~~~~ +!!! error TS2322: Type '`outer-${InferredRepeated}`' is not assignable to type 'OuterRepeated'. +!!! error TS2322: Type '`outer-${string}-${string}`' is not assignable to type 'OuterRepeated'. + } + + type Linked = T extends unknown ? T & { value: T } : never; + + function retainIntersectionComponent(value: T & { value: T }) { + const correlated: Linked = value; // Error: the property can come from a different constituent. + ~~~~~~~~~~ +!!! error TS2322: Type 'T & { value: T; }' is not assignable to type 'Linked'. + } + + type ObjectValues = T extends unknown ? T["value"] : never; + type IndexedValues = K extends unknown ? T[K] : never; + + function indexedAccess(value: T["value"], indexed: T[K]) { + const fromObject: ObjectValues = value; + const fromIndex: IndexedValues = indexed; + } + + function correlated(pair: { x: T; y: T }, tuple: [T, T], callback: (value: T) => T) { + const p: Pair = pair; // Error + ~ +!!! error TS2322: Type '{ x: T; y: T; }' is not assignable to type 'Pair'. + const t: Tuple = tuple; // Error + ~ +!!! error TS2322: Type '[T, T]' is not assignable to type 'Tuple'. + const c: Callback = callback; // Error + ~ +!!! error TS2322: Type '(value: T) => T' is not assignable to type 'Callback'. + } + + function possiblyNever(value: { a: number; b: number }) { + const unconstrained: Constant = value; + const constrained: Constant = value; + const conditional: Constant<[T] extends [never] ? object : never> = value; + } + + function requiredWitness(value: { a: number; b: number; value: T }) { + const result: Constant = value; + } + + requiredWitness({ a: 1, b: 2, get value(): never { throw new Error(); } }); + + function optionalWitness(value: { a: number; b: number; value?: T }) { + const result: Constant = value; + } + + function arrayWitness(value: { a: number; b: number; values: T[] }) { + const result: Constant = value; + } + + function functionWitness(value: { a: number; b: number; get: () => T }) { + const result: Constant = value; + } + + function definitelyNotNever(value: { a: number; b: number }) { + const object: Constant<{ value: T }> = value; + const array: Constant = value; + const tuple: Constant<[T]> = value; + const union: Constant = value; + const conditional: Constant<[T] extends [string] ? { value: T } : { other: T }> = value; + } + + type Homomorphic = { [K in keyof T]: T[K] }; + type Spread = [...T]; + + function emptyInstantiations(value: { a: number; b: number }) { + const mapped: Constant> = value; + const variadic: Constant> = value; + } + + type Inferred = T extends (value: infer P) => unknown ? T | P : never; + type InferredNonDistributive = [T] extends [(value: infer P) => unknown] ? T | P : never; + type InferredValue = T extends { value: infer P } ? P : never; + type InferredCallback = T extends { value: infer P } ? (value: P) => P : never; + type ConstrainedInfer = T extends { value: infer P extends string } ? T : never; + + function inference unknown>(value: T) { + const distributive: Inferred = value; // Error: additional overloads can change the inferred parameter. + ~~~~~~~~~~~~ +!!! error TS2322: Type 'T' is not assignable to type 'Inferred'. +!!! error TS2322: Type '(value: any) => unknown' is not assignable to type 'Inferred'. + const nonDistributive: InferredNonDistributive = value; // Error + ~~~~~~~~~~~~~~~ +!!! error TS2322: Type 'T' is not assignable to type 'InferredNonDistributive'. +!!! error TS2322: Type '(value: any) => unknown' is not assignable to type 'InferredNonDistributive'. + } + + function independentInference(value: T) { + const result: T extends infer P ? T | P : never = value; + } + + function propertyInference(value: T) { + const result: T extends { value: infer P } ? T | P : never = value; + } + + function tupleInference(value: T) { + const result: [T] extends [infer P] ? T | P : never = value; + } + + function independentParameters unknown>(value: T) { + const result: T extends (value: infer P) => infer R ? T | P | R : never = value; // Error + ~~~~~~ +!!! error TS2322: Type 'T' is not assignable to type 'T extends (value: infer P) => infer R ? P | R | T : never'. +!!! error TS2322: Type '(value: any) => unknown' is not assignable to type 'T extends (value: infer P) => infer R ? P | R | T : never'. + } + + type InferredReturn = T extends () => infer R ? T | R : never; + + function returnInference unknown>(value: T) { + const result: InferredReturn = value; // Error: inference can use an overload with incompatible arity. + ~~~~~~ +!!! error TS2322: Type 'T' is not assignable to type 'InferredReturn'. +!!! error TS2322: Type '() => unknown' is not assignable to type 'InferredReturn'. + } + + function universalSignature never>(value: T) { + const parameter: Inferred = value; + const both: T extends (value: infer P) => infer R ? T | P | R : never = value; + } + + function ignoredParameter never>(value: T) { + const result: Inferred = value; + const returned: InferredReturn = value; + } + + type InferredMethod = T extends { method: (value: infer P) => unknown } ? T | P : never; + type InferredMethodReturn = T extends { method: (value: any) => infer R } ? T | R : never; + + function bivariantInference(value: T) { + const result: InferredMethod = value; // Error: the constraint permits narrower method parameters. + ~~~~~~ +!!! error TS2322: Type 'T' is not assignable to type 'InferredMethod'. +!!! error TS2322: Type '{ method(value: unknown): never; }' is not assignable to type 'InferredMethod'. + const returned: InferredMethodReturn = value; // Error: a never parameter fails the strict function pattern. + ~~~~~~~~ +!!! error TS2322: Type 'T' is not assignable to type 'InferredMethodReturn'. +!!! error TS2322: Type '{ method(value: unknown): never; }' is not assignable to type 'InferredMethodReturn'. + } + + function dependentInference(value: string, callback: (value: string) => string) { + const inferred: InferredValue = value; // Error: T can contain a narrower value. + ~~~~~~~~ +!!! error TS2322: Type 'string' is not assignable to type 'InferredValue'. + const inferredCallback: InferredCallback = callback; // Error + ~~~~~~~~~~~~~~~~ +!!! error TS2322: Type '(value: string) => string' is not assignable to type 'InferredCallback'. + } + + function constrainedInference(value: T) { + const result: ConstrainedInfer = value; // Error: any in the constraint does not imply string. + ~~~~~~ +!!! error TS2322: Type 'T' is not assignable to type 'ConstrainedInfer'. +!!! error TS2322: Type '{ value: any; }' is not assignable to type 'ConstrainedInfer'. + } + + type SpecificParameter = T extends (value: string) => infer R ? T | R : never; + type SpecificMethod = T extends { method(value: string): infer R } ? T | R : never; + type OptionalProperty = T extends { optional?: string; value: infer R } ? T | R : never; + type MixedInference = T extends { input: (value: infer R) => void; output: infer R } ? T | R : never; + type SingleParameter = T extends (value: infer R) => unknown ? T | R : never; + type OptionalParameter = T extends (value?: infer R) => unknown ? T | R : never; + type IntersectionPattern = T extends { value: infer R } & { method(value: string): unknown } ? T | R : never; + type PrimitiveBoundary = T extends object | (infer R extends number) ? T | R : never; + + function fixedParameter unknown>(value: T) { + const result: SpecificParameter = value; // Error: a number parameter also satisfies the constraint. + ~~~~~~ +!!! error TS2322: Type 'T' is not assignable to type 'SpecificParameter'. +!!! error TS2322: Type '(value: any) => unknown' is not assignable to type 'SpecificParameter'. + const withoutInference: T extends (value: string) => unknown ? T : never = value; // Error + ~~~~~~~~~~~~~~~~ +!!! error TS2322: Type 'T' is not assignable to type 'T extends (value: string) => unknown ? T : never'. +!!! error TS2322: Type '(value: any) => unknown' is not assignable to type 'T extends (value: string) => unknown ? T : never'. + } + + function bivariantParameter(value: T) { + const result: SpecificMethod = value; // Error + ~~~~~~ +!!! error TS2322: Type 'T' is not assignable to type 'SpecificMethod'. +!!! error TS2322: Type '{ method(value: unknown): unknown; }' is not assignable to type 'SpecificMethod'. + } + + function optionalProperty(value: T) { + const result: OptionalProperty = value; // Error: subtypes can add an incompatible optional property. + ~~~~~~ +!!! error TS2322: Type 'T' is not assignable to type 'OptionalProperty'. +!!! error TS2322: Type '{ value: unknown; }' is not assignable to type 'OptionalProperty'. + } + + function mixedInference void; output: any }>(value: T) { + const result: MixedInference = value; // Error: input and output can require incompatible inferences. + ~~~~~~ +!!! error TS2322: Type 'T' is not assignable to type 'MixedInference'. +!!! error TS2322: Type '{ input: (value: any) => void; output: any; }' is not assignable to type 'MixedInference'. + } + + interface InvariantInferencePattern { + input: (value: T) => void; + output: T; + } + + type InvariantInference = T extends InvariantInferencePattern ? T | R : never; + + function invariantInference>(value: T) { + const result: InvariantInference = value; // Error: invariant type-argument inference must not hide mixed positions. + ~~~~~~ +!!! error TS2322: Type 'T' is not assignable to type 'InvariantInference'. +!!! error TS2322: Type 'InvariantInferencePattern' is not assignable to type 'InvariantInference'. + } + + enum NumericInferencePattern { + value + } + + type NumericInference = T extends (value: infer P) => NumericInferencePattern ? T | P : never; + + function numericInference number>(value: T) { + const result: NumericInference = value; // Error: number-to-enum compatibility does not cover every numeric literal. + ~~~~~~ +!!! error TS2322: Type 'T' is not assignable to type 'NumericInference'. +!!! error TS2322: Type '(value: unknown) => number' is not assignable to type 'NumericInference'. + } + + function unboundedArity unknown>(value: T) { + const result: SingleParameter = value; // Error: subtypes can require multiple arguments. + ~~~~~~ +!!! error TS2322: Type 'T' is not assignable to type 'SingleParameter'. +!!! error TS2322: Type '(...args: any[]) => unknown' is not assignable to type 'SingleParameter'. + } + + function optionalParameter unknown>(value: T) { + const result: OptionalParameter = value; // Error: subtypes can require an argument. + ~~~~~~ +!!! error TS2322: Type 'T' is not assignable to type 'OptionalParameter'. +!!! error TS2322: Type '(value?: any) => unknown' is not assignable to type 'OptionalParameter'. + } + + function intersectionPattern(value: T) { + const result: IntersectionPattern = value; // Error + ~~~~~~ +!!! error TS2322: Type 'T' is not assignable to type 'IntersectionPattern'. +!!! error TS2322: Type '{ value: any; method(value: any): unknown; }' is not assignable to type 'IntersectionPattern'. + } + + function primitiveBoundary(value: T) { + const result: PrimitiveBoundary = value; // Error: structural object constraints admit primitives. + ~~~~~~ +!!! error TS2322: Type 'T' is not assignable to type 'PrimitiveBoundary'. +!!! error TS2322: Type '{ toString(): string; }' is not assignable to type 'PrimitiveBoundary'. + } + + class Nominal { + private brand!: void; + value!: T; + } + + function nominalInference(value: T) { + const result: T extends Nominal ? T | P : never = value; // Error + ~~~~~~ +!!! error TS2322: Type 'T' is not assignable to type 'T extends Nominal ? P | T : never'. +!!! error TS2322: Type '{ value: unknown; }' is not assignable to type 'T extends Nominal ? P | T : never'. + } + + type FilteredKeys = T extends U ? never : T; + type DecoratedKeys = T extends U ? never : `key-${T}`; + + function filtering(value: FilteredKeys) { + const excluded: Exclude = value; + const remapped: DecoratedKeys, never> = `key-${value}`; + } + + function filteredRemapping(value: `key-${Exclude}`) { + const result: DecoratedKeys = value; + } + + type FilteredMap = { [K in T as K extends U ? never : K]: unknown }; + type DecoratedMap = { [K in T as K extends U ? never : `key-${K}`]: unknown }; + + function mappedKeys(key: Exclude, decorated: `key-${Exclude}`) { + const filtered: keyof FilteredMap = key; + const remapped: keyof DecoratedMap = decorated; + } + + type Recursive = T extends object ? T | { next: Recursive } : T; + + function recursion(value: T) { + const result: Recursive = value; + } + + function subtypeIsNotConstituent(value: T & string) { + const result: Extract = value; // Error: Extract is never. + ~~~~~~ +!!! error TS2322: Type 'T & string' is not assignable to type 'Extract'. + } + + // Concrete instantiations witnessing the rejected assignments. + const mixedPair: Pair<"a" | "b"> = { x: "a", y: "b" }; // Error + ~~~~~~~~~ +!!! error TS2322: Type '{ x: "a"; y: "b"; }' is not assignable to type '{ x: "a"; y: "a"; } | { x: "b"; y: "b"; }'. +!!! error TS2322: Types of property 'y' are incompatible. +!!! error TS2322: Type '"b"' is not assignable to type '"a"'. + const mixedTuple: Tuple<"a" | "b"> = ["a", "b"]; // Error + ~~~~~~~~~~ +!!! error TS2322: Type '["a", "b"]' is not assignable to type '["a", "a"] | ["b", "b"]'. +!!! error TS2322: Type '["a", "b"]' is not assignable to type '["a", "a"]'. +!!! error TS2322: Type at position 1 in source is not compatible with type at position 1 in target. +!!! error TS2322: Type '"b"' is not assignable to type '"a"'. + const mixedString: Repeated<"a" | "b"> = "a-b"; // Error + ~~~~~~~~~~~ +!!! error TS2322: Type '"a-b"' is not assignable to type '"a-a" | "b-b"'. + const emptyConstant: Constant = { a: 1, b: 2 }; // Error + ~~~~~~~~~~~~~ +!!! error TS2322: Type '{ a: number; b: number; }' is not assignable to type 'never'. + const narrowInference: InferredValue<{ value: "a" }> = "b"; // Error + ~~~~~~~~~~~~~~~ +!!! error TS2322: Type '"b"' is not assignable to type '"a"'. + const rejectedConstrainedInfer: ConstrainedInfer<{ value: number }> = { value: 1 }; // Error + ~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2322: Type '{ value: number; }' is not assignable to type 'never'. + const rejectedFixedParameter: SpecificParameter<(value: number) => boolean> = (value: number) => true; // Error + ~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2322: Type '(value: number) => boolean' is not assignable to type 'never'. + const rejectedMethod: SpecificMethod<{ method(value: number): boolean }> = { method: (value: number) => true }; // Error + ~~~~~~~~~~~~~~ +!!! error TS2322: Type '{ method: (value: number) => boolean; }' is not assignable to type 'never'. + const rejectedOptionalProperty: OptionalProperty<{ optional: number; value: boolean }> = { optional: 1, value: true }; // Error + ~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2322: Type '{ optional: number; value: boolean; }' is not assignable to type 'never'. + const rejectedMixedInference: MixedInference<{ input(value: number): void; output: string }> = { input: (value: number) => {}, output: "" }; // Error + ~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2322: Type '{ input: (value: number) => void; output: string; }' is not assignable to type 'never'. + const rejectedInvariantInference: InvariantInference<{ input: (value: number) => void; output: string }> = { input: (value: number) => {}, output: "" }; // Error + ~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2322: Type '{ input: (value: number) => void; output: string; }' is not assignable to type 'never'. + const rejectedNumericInference: NumericInference<(value: unknown) => 123> = (value: unknown) => 123; // Error + ~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2322: Type '(value: unknown) => number' is not assignable to type 'never'. + const rejectedArity: SingleParameter<(left: number, right: number) => void> = (left: number, right: number) => {}; // Error + ~~~~~~~~~~~~~ +!!! error TS2322: Type '(left: number, right: number) => void' is not assignable to type 'never'. + const rejectedOptionalParameter: OptionalParameter<(value: string) => void> = (value: string) => {}; // Error + ~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2322: Type '(value: string) => void' is not assignable to type 'never'. + const rejectedIntersection: IntersectionPattern<{ value: boolean; method(value: number): unknown }> = { value: true, method: (value: number) => {} }; // Error + ~~~~~~~~~~~~~~~~~~~~ +!!! error TS2322: Type '{ value: boolean; method: (value: number) => void; }' is not assignable to type 'never'. + const rejectedPrimitive: PrimitiveBoundary = ""; // Error + ~~~~~~~~~~~~~~~~~ +!!! error TS2322: Type '""' is not assignable to type 'never'. + + declare const overloadedParameter: { + (value: string): void; + (left: number, right: number): void; + }; + const parameterConstraint: (value: any) => unknown = overloadedParameter; + const rejectedOverloadedParameter: Inferred = overloadedParameter; // Error + ~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2322: Type '{ (value: string): void; (left: number, right: number): void; }' is not assignable to type 'never'. + const rejectedOverloadedNonDistributive: InferredNonDistributive = overloadedParameter; // Error + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2322: Type '{ (value: string): void; (left: number, right: number): void; }' is not assignable to type 'never'. + + declare const overloadedReturn: { + (): string; + (value: number): number; + }; + const returnConstraint: () => unknown = overloadedReturn; + const rejectedOverloadedReturn: InferredReturn = overloadedReturn; // Error + ~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2322: Type '{ (): string; (value: number): number; }' is not assignable to type 'never'. + + declare const overloadedMethod: { + method(value: string): never; + method(left: number, right: number): void; + }; + const methodConstraint: { method(value: unknown): never } = overloadedMethod; + const rejectedOverloadedMethod: InferredMethod = overloadedMethod; // Error + ~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2322: Type '{ method(value: string): never; method(left: number, right: number): void; }' is not assignable to type 'never'. + + declare const neverParameterMethod: { method(value: never): never }; + const neverMethodConstraint: { method(value: unknown): never } = neverParameterMethod; + const rejectedMethodReturn: InferredMethodReturn = neverParameterMethod; // Error + ~~~~~~~~~~~~~~~~~~~~ +!!! error TS2322: Type '{ method(value: never): never; }' is not assignable to type 'never'. + \ No newline at end of file diff --git a/tsc/testdata/baselines/reference/compiler/distributiveConditionalTypeRelations.symbols b/tsc/testdata/baselines/reference/compiler/distributiveConditionalTypeRelations.symbols new file mode 100644 index 0000000000000..0181915fadcf6 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/distributiveConditionalTypeRelations.symbols @@ -0,0 +1,1436 @@ +//// [tests/cases/compiler/distributiveConditionalTypeRelations.ts] //// + +=== distributiveConditionalTypeRelations.ts === +// The original #27932 cases are in conditionalTypeAssignabilityWhenDeferred.ts. + +type Either = T extends U ? T | { matched: true } : T | { matched: false }; +>Either : Symbol(Either, Decl(distributiveConditionalTypeRelations.ts, 0, 0)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 2, 12)) +>U : Symbol(U, Decl(distributiveConditionalTypeRelations.ts, 2, 14)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 2, 12)) +>U : Symbol(U, Decl(distributiveConditionalTypeRelations.ts, 2, 14)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 2, 12)) +>matched : Symbol(matched, Decl(distributiveConditionalTypeRelations.ts, 2, 39)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 2, 12)) +>matched : Symbol(matched, Decl(distributiveConditionalTypeRelations.ts, 2, 63)) + +type Both = T extends U ? T & U : T; +>Both : Symbol(Both, Decl(distributiveConditionalTypeRelations.ts, 2, 81)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 3, 10)) +>U : Symbol(U, Decl(distributiveConditionalTypeRelations.ts, 3, 12)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 3, 10)) +>U : Symbol(U, Decl(distributiveConditionalTypeRelations.ts, 3, 12)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 3, 10)) +>U : Symbol(U, Decl(distributiveConditionalTypeRelations.ts, 3, 12)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 3, 10)) + +type Constant = T extends string ? { a: number } : { b: number }; +>Constant : Symbol(Constant, Decl(distributiveConditionalTypeRelations.ts, 3, 42)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 4, 14)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 4, 14)) +>a : Symbol(a, Decl(distributiveConditionalTypeRelations.ts, 4, 39)) +>b : Symbol(b, Decl(distributiveConditionalTypeRelations.ts, 4, 55)) + +type Pair = T extends unknown ? { x: T; y: T } : never; +>Pair : Symbol(Pair, Decl(distributiveConditionalTypeRelations.ts, 4, 68)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 5, 10)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 5, 10)) +>x : Symbol(x, Decl(distributiveConditionalTypeRelations.ts, 5, 36)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 5, 10)) +>y : Symbol(y, Decl(distributiveConditionalTypeRelations.ts, 5, 42)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 5, 10)) + +type Tuple = T extends unknown ? [T, T] : never; +>Tuple : Symbol(Tuple, Decl(distributiveConditionalTypeRelations.ts, 5, 58)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 6, 11)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 6, 11)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 6, 11)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 6, 11)) + +type Callback = T extends unknown ? (value: T) => T : never; +>Callback : Symbol(Callback, Decl(distributiveConditionalTypeRelations.ts, 6, 51)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 7, 14)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 7, 14)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 7, 40)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 7, 14)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 7, 14)) + +type Repeated = T extends unknown ? `${T}-${T}` : never; +>Repeated : Symbol(Repeated, Decl(distributiveConditionalTypeRelations.ts, 7, 63)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 8, 14)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 8, 14)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 8, 14)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 8, 14)) + +type Remapped = T extends unknown ? `key-${T}` : never; +>Remapped : Symbol(Remapped, Decl(distributiveConditionalTypeRelations.ts, 8, 74)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 9, 14)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 9, 14)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 9, 14)) + +type Upper = T extends unknown ? Uppercase : never; +>Upper : Symbol(Upper, Decl(distributiveConditionalTypeRelations.ts, 9, 73)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 10, 11)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 10, 11)) +>Uppercase : Symbol(Uppercase, Decl(lib.es5.d.ts, --, --)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 10, 11)) + +function identity(value: T, intersection: T & U) { +>identity : Symbol(identity, Decl(distributiveConditionalTypeRelations.ts, 10, 72)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 12, 18)) +>U : Symbol(U, Decl(distributiveConditionalTypeRelations.ts, 12, 20)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 12, 24)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 12, 18)) +>intersection : Symbol(intersection, Decl(distributiveConditionalTypeRelations.ts, 12, 33)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 12, 18)) +>U : Symbol(U, Decl(distributiveConditionalTypeRelations.ts, 12, 20)) + + const either: Either = value; +>either : Symbol(either, Decl(distributiveConditionalTypeRelations.ts, 13, 9)) +>Either : Symbol(Either, Decl(distributiveConditionalTypeRelations.ts, 0, 0)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 12, 18)) +>U : Symbol(U, Decl(distributiveConditionalTypeRelations.ts, 12, 20)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 12, 24)) + + const both: Both = value; +>both : Symbol(both, Decl(distributiveConditionalTypeRelations.ts, 14, 9)) +>Both : Symbol(Both, Decl(distributiveConditionalTypeRelations.ts, 2, 81)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 12, 18)) +>U : Symbol(U, Decl(distributiveConditionalTypeRelations.ts, 12, 20)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 12, 24)) + + const intersected: Either = intersection; +>intersected : Symbol(intersected, Decl(distributiveConditionalTypeRelations.ts, 15, 9)) +>Either : Symbol(Either, Decl(distributiveConditionalTypeRelations.ts, 0, 0)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 12, 18)) +>U : Symbol(U, Decl(distributiveConditionalTypeRelations.ts, 12, 20)) +>intersection : Symbol(intersection, Decl(distributiveConditionalTypeRelations.ts, 12, 33)) + + const extracted: Extract = value; +>extracted : Symbol(extracted, Decl(distributiveConditionalTypeRelations.ts, 16, 9)) +>Extract : Symbol(Extract, Decl(lib.es5.d.ts, --, --)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 12, 18)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 12, 24)) + + const excluded: Exclude = value; +>excluded : Symbol(excluded, Decl(distributiveConditionalTypeRelations.ts, 17, 9)) +>Exclude : Symbol(Exclude, Decl(lib.es5.d.ts, --, --)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 12, 18)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 12, 24)) +} + +function wrappers(value: `key-${T}`, upper: Uppercase, repeated: `${T}-${T}`) { +>wrappers : Symbol(wrappers, Decl(distributiveConditionalTypeRelations.ts, 18, 1)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 20, 18)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 20, 36)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 20, 18)) +>upper : Symbol(upper, Decl(distributiveConditionalTypeRelations.ts, 20, 54)) +>Uppercase : Symbol(Uppercase, Decl(lib.es5.d.ts, --, --)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 20, 18)) +>repeated : Symbol(repeated, Decl(distributiveConditionalTypeRelations.ts, 20, 75)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 20, 18)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 20, 18)) + + const remapped: Remapped = value; +>remapped : Symbol(remapped, Decl(distributiveConditionalTypeRelations.ts, 21, 9)) +>Remapped : Symbol(Remapped, Decl(distributiveConditionalTypeRelations.ts, 8, 74)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 20, 18)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 20, 36)) + + const capitalized: Upper = upper; +>capitalized : Symbol(capitalized, Decl(distributiveConditionalTypeRelations.ts, 22, 9)) +>Upper : Symbol(Upper, Decl(distributiveConditionalTypeRelations.ts, 9, 73)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 20, 18)) +>upper : Symbol(upper, Decl(distributiveConditionalTypeRelations.ts, 20, 54)) + + const correlated: Repeated = repeated; // Error: cross products do not distribute. +>correlated : Symbol(correlated, Decl(distributiveConditionalTypeRelations.ts, 23, 9)) +>Repeated : Symbol(Repeated, Decl(distributiveConditionalTypeRelations.ts, 7, 63)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 20, 18)) +>repeated : Symbol(repeated, Decl(distributiveConditionalTypeRelations.ts, 20, 75)) +} + +type WithOuter = T extends unknown ? `${T}-${U}` : never; +>WithOuter : Symbol(WithOuter, Decl(distributiveConditionalTypeRelations.ts, 24, 1)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 26, 15)) +>U : Symbol(U, Decl(distributiveConditionalTypeRelations.ts, 26, 32)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 26, 15)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 26, 15)) +>U : Symbol(U, Decl(distributiveConditionalTypeRelations.ts, 26, 32)) + +function retainOuterArgument(value: `${T}-${T}`) { +>retainOuterArgument : Symbol(retainOuterArgument, Decl(distributiveConditionalTypeRelations.ts, 26, 93)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 28, 29)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 28, 47)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 28, 29)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 28, 29)) + + const result: WithOuter = value; // Error: the uniform-instantiation proof conservatively rejects repeated occurrences. +>result : Symbol(result, Decl(distributiveConditionalTypeRelations.ts, 29, 9)) +>WithOuter : Symbol(WithOuter, Decl(distributiveConditionalTypeRelations.ts, 24, 1)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 28, 29)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 28, 29)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 28, 47)) +} + +type OuterSource = T extends unknown ? `${T}-${U}` : never; +>OuterSource : Symbol(OuterSource, Decl(distributiveConditionalTypeRelations.ts, 30, 1)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 32, 17)) +>U : Symbol(U, Decl(distributiveConditionalTypeRelations.ts, 32, 34)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 32, 17)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 32, 17)) +>U : Symbol(U, Decl(distributiveConditionalTypeRelations.ts, 32, 34)) + +type OuterRepeated = T extends unknown ? `outer-${T}-${T}` : never; +>OuterRepeated : Symbol(OuterRepeated, Decl(distributiveConditionalTypeRelations.ts, 32, 95)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 33, 19)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 33, 19)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 33, 19)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 33, 19)) + +type UpperRepeated = T extends unknown ? `${Uppercase}-${Uppercase}` : never; +>UpperRepeated : Symbol(UpperRepeated, Decl(distributiveConditionalTypeRelations.ts, 33, 85)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 34, 19)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 34, 19)) +>Uppercase : Symbol(Uppercase, Decl(lib.es5.d.ts, --, --)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 34, 19)) +>Uppercase : Symbol(Uppercase, Decl(lib.es5.d.ts, --, --)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 34, 19)) + +type InferredRepeated = [T] extends [infer U extends string] ? `${U}-${U}` : never; +>InferredRepeated : Symbol(InferredRepeated, Decl(distributiveConditionalTypeRelations.ts, 34, 101)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 35, 22)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 35, 22)) +>U : Symbol(U, Decl(distributiveConditionalTypeRelations.ts, 35, 60)) +>U : Symbol(U, Decl(distributiveConditionalTypeRelations.ts, 35, 60)) +>U : Symbol(U, Decl(distributiveConditionalTypeRelations.ts, 35, 60)) + +function retainConditionalArgument(value: `outer-${OuterSource}`) { +>retainConditionalArgument : Symbol(retainConditionalArgument, Decl(distributiveConditionalTypeRelations.ts, 35, 101)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 37, 35)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 37, 53)) +>OuterSource : Symbol(OuterSource, Decl(distributiveConditionalTypeRelations.ts, 30, 1)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 37, 35)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 37, 35)) + + const correlated: OuterRepeated = value; // Error: the second argument is not distributed. +>correlated : Symbol(correlated, Decl(distributiveConditionalTypeRelations.ts, 38, 9)) +>OuterRepeated : Symbol(OuterRepeated, Decl(distributiveConditionalTypeRelations.ts, 32, 95)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 37, 35)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 37, 53)) +} + +function cachedOccurrences(value: `${Uppercase}-${Uppercase}`) { +>cachedOccurrences : Symbol(cachedOccurrences, Decl(distributiveConditionalTypeRelations.ts, 39, 1)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 41, 27)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 41, 45)) +>Uppercase : Symbol(Uppercase, Decl(lib.es5.d.ts, --, --)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 41, 27)) +>Uppercase : Symbol(Uppercase, Decl(lib.es5.d.ts, --, --)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 41, 27)) + + const correlated: UpperRepeated = value; // Error: a cached inner type still occurs twice. +>correlated : Symbol(correlated, Decl(distributiveConditionalTypeRelations.ts, 42, 9)) +>UpperRepeated : Symbol(UpperRepeated, Decl(distributiveConditionalTypeRelations.ts, 33, 85)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 41, 27)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 41, 45)) +} + +function deferredOccurrences(value: `outer-${InferredRepeated}`) { +>deferredOccurrences : Symbol(deferredOccurrences, Decl(distributiveConditionalTypeRelations.ts, 43, 1)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 45, 29)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 45, 47)) +>InferredRepeated : Symbol(InferredRepeated, Decl(distributiveConditionalTypeRelations.ts, 34, 101)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 45, 29)) + + const correlated: OuterRepeated = value; // Error: deferred inference hides a non-distributive use. +>correlated : Symbol(correlated, Decl(distributiveConditionalTypeRelations.ts, 46, 9)) +>OuterRepeated : Symbol(OuterRepeated, Decl(distributiveConditionalTypeRelations.ts, 32, 95)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 45, 29)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 45, 47)) +} + +type Linked = T extends unknown ? T & { value: T } : never; +>Linked : Symbol(Linked, Decl(distributiveConditionalTypeRelations.ts, 47, 1)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 49, 12)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 49, 12)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 49, 12)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 49, 42)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 49, 12)) + +function retainIntersectionComponent(value: T & { value: T }) { +>retainIntersectionComponent : Symbol(retainIntersectionComponent, Decl(distributiveConditionalTypeRelations.ts, 49, 62)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 51, 37)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 51, 40)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 51, 37)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 51, 52)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 51, 37)) + + const correlated: Linked = value; // Error: the property can come from a different constituent. +>correlated : Symbol(correlated, Decl(distributiveConditionalTypeRelations.ts, 52, 9)) +>Linked : Symbol(Linked, Decl(distributiveConditionalTypeRelations.ts, 47, 1)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 51, 37)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 51, 40)) +} + +type ObjectValues = T extends unknown ? T["value"] : never; +>ObjectValues : Symbol(ObjectValues, Decl(distributiveConditionalTypeRelations.ts, 53, 1)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 55, 18)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 55, 29)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 55, 18)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 55, 18)) + +type IndexedValues = K extends unknown ? T[K] : never; +>IndexedValues : Symbol(IndexedValues, Decl(distributiveConditionalTypeRelations.ts, 55, 89)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 56, 19)) +>K : Symbol(K, Decl(distributiveConditionalTypeRelations.ts, 56, 21)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 56, 19)) +>K : Symbol(K, Decl(distributiveConditionalTypeRelations.ts, 56, 21)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 56, 19)) +>K : Symbol(K, Decl(distributiveConditionalTypeRelations.ts, 56, 21)) + +function indexedAccess(value: T["value"], indexed: T[K]) { +>indexedAccess : Symbol(indexedAccess, Decl(distributiveConditionalTypeRelations.ts, 56, 76)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 58, 23)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 58, 34)) +>K : Symbol(K, Decl(distributiveConditionalTypeRelations.ts, 58, 52)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 58, 23)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 58, 72)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 58, 23)) +>indexed : Symbol(indexed, Decl(distributiveConditionalTypeRelations.ts, 58, 90)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 58, 23)) +>K : Symbol(K, Decl(distributiveConditionalTypeRelations.ts, 58, 52)) + + const fromObject: ObjectValues = value; +>fromObject : Symbol(fromObject, Decl(distributiveConditionalTypeRelations.ts, 59, 9)) +>ObjectValues : Symbol(ObjectValues, Decl(distributiveConditionalTypeRelations.ts, 53, 1)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 58, 23)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 58, 72)) + + const fromIndex: IndexedValues = indexed; +>fromIndex : Symbol(fromIndex, Decl(distributiveConditionalTypeRelations.ts, 60, 9)) +>IndexedValues : Symbol(IndexedValues, Decl(distributiveConditionalTypeRelations.ts, 55, 89)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 58, 23)) +>K : Symbol(K, Decl(distributiveConditionalTypeRelations.ts, 58, 52)) +>indexed : Symbol(indexed, Decl(distributiveConditionalTypeRelations.ts, 58, 90)) +} + +function correlated(pair: { x: T; y: T }, tuple: [T, T], callback: (value: T) => T) { +>correlated : Symbol(correlated, Decl(distributiveConditionalTypeRelations.ts, 61, 1)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 63, 20)) +>pair : Symbol(pair, Decl(distributiveConditionalTypeRelations.ts, 63, 23)) +>x : Symbol(x, Decl(distributiveConditionalTypeRelations.ts, 63, 30)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 63, 20)) +>y : Symbol(y, Decl(distributiveConditionalTypeRelations.ts, 63, 36)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 63, 20)) +>tuple : Symbol(tuple, Decl(distributiveConditionalTypeRelations.ts, 63, 44)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 63, 20)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 63, 20)) +>callback : Symbol(callback, Decl(distributiveConditionalTypeRelations.ts, 63, 59)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 63, 71)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 63, 20)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 63, 20)) + + const p: Pair = pair; // Error +>p : Symbol(p, Decl(distributiveConditionalTypeRelations.ts, 64, 9)) +>Pair : Symbol(Pair, Decl(distributiveConditionalTypeRelations.ts, 4, 68)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 63, 20)) +>pair : Symbol(pair, Decl(distributiveConditionalTypeRelations.ts, 63, 23)) + + const t: Tuple = tuple; // Error +>t : Symbol(t, Decl(distributiveConditionalTypeRelations.ts, 65, 9)) +>Tuple : Symbol(Tuple, Decl(distributiveConditionalTypeRelations.ts, 5, 58)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 63, 20)) +>tuple : Symbol(tuple, Decl(distributiveConditionalTypeRelations.ts, 63, 44)) + + const c: Callback = callback; // Error +>c : Symbol(c, Decl(distributiveConditionalTypeRelations.ts, 66, 9)) +>Callback : Symbol(Callback, Decl(distributiveConditionalTypeRelations.ts, 6, 51)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 63, 20)) +>callback : Symbol(callback, Decl(distributiveConditionalTypeRelations.ts, 63, 59)) +} + +function possiblyNever(value: { a: number; b: number }) { +>possiblyNever : Symbol(possiblyNever, Decl(distributiveConditionalTypeRelations.ts, 67, 1)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 69, 23)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 69, 26)) +>a : Symbol(a, Decl(distributiveConditionalTypeRelations.ts, 69, 34)) +>b : Symbol(b, Decl(distributiveConditionalTypeRelations.ts, 69, 45)) + + const unconstrained: Constant = value; +>unconstrained : Symbol(unconstrained, Decl(distributiveConditionalTypeRelations.ts, 70, 9)) +>Constant : Symbol(Constant, Decl(distributiveConditionalTypeRelations.ts, 3, 42)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 69, 23)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 69, 26)) + + const constrained: Constant = value; +>constrained : Symbol(constrained, Decl(distributiveConditionalTypeRelations.ts, 71, 9)) +>Constant : Symbol(Constant, Decl(distributiveConditionalTypeRelations.ts, 3, 42)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 69, 23)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 69, 26)) + + const conditional: Constant<[T] extends [never] ? object : never> = value; +>conditional : Symbol(conditional, Decl(distributiveConditionalTypeRelations.ts, 72, 9)) +>Constant : Symbol(Constant, Decl(distributiveConditionalTypeRelations.ts, 3, 42)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 69, 23)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 69, 26)) +} + +function requiredWitness(value: { a: number; b: number; value: T }) { +>requiredWitness : Symbol(requiredWitness, Decl(distributiveConditionalTypeRelations.ts, 73, 1)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 75, 25)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 75, 28)) +>a : Symbol(a, Decl(distributiveConditionalTypeRelations.ts, 75, 36)) +>b : Symbol(b, Decl(distributiveConditionalTypeRelations.ts, 75, 47)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 75, 58)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 75, 25)) + + const result: Constant = value; +>result : Symbol(result, Decl(distributiveConditionalTypeRelations.ts, 76, 9)) +>Constant : Symbol(Constant, Decl(distributiveConditionalTypeRelations.ts, 3, 42)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 75, 25)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 75, 28)) +} + +requiredWitness({ a: 1, b: 2, get value(): never { throw new Error(); } }); +>requiredWitness : Symbol(requiredWitness, Decl(distributiveConditionalTypeRelations.ts, 73, 1)) +>a : Symbol(a, Decl(distributiveConditionalTypeRelations.ts, 79, 24)) +>b : Symbol(b, Decl(distributiveConditionalTypeRelations.ts, 79, 30)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 79, 36)) +>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2022.error.d.ts, --, --)) + +function optionalWitness(value: { a: number; b: number; value?: T }) { +>optionalWitness : Symbol(optionalWitness, Decl(distributiveConditionalTypeRelations.ts, 79, 82)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 81, 25)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 81, 28)) +>a : Symbol(a, Decl(distributiveConditionalTypeRelations.ts, 81, 36)) +>b : Symbol(b, Decl(distributiveConditionalTypeRelations.ts, 81, 47)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 81, 58)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 81, 25)) + + const result: Constant = value; +>result : Symbol(result, Decl(distributiveConditionalTypeRelations.ts, 82, 9)) +>Constant : Symbol(Constant, Decl(distributiveConditionalTypeRelations.ts, 3, 42)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 81, 25)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 81, 28)) +} + +function arrayWitness(value: { a: number; b: number; values: T[] }) { +>arrayWitness : Symbol(arrayWitness, Decl(distributiveConditionalTypeRelations.ts, 83, 1)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 85, 22)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 85, 25)) +>a : Symbol(a, Decl(distributiveConditionalTypeRelations.ts, 85, 33)) +>b : Symbol(b, Decl(distributiveConditionalTypeRelations.ts, 85, 44)) +>values : Symbol(values, Decl(distributiveConditionalTypeRelations.ts, 85, 55)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 85, 22)) + + const result: Constant = value; +>result : Symbol(result, Decl(distributiveConditionalTypeRelations.ts, 86, 9)) +>Constant : Symbol(Constant, Decl(distributiveConditionalTypeRelations.ts, 3, 42)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 85, 22)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 85, 25)) +} + +function functionWitness(value: { a: number; b: number; get: () => T }) { +>functionWitness : Symbol(functionWitness, Decl(distributiveConditionalTypeRelations.ts, 87, 1)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 89, 25)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 89, 28)) +>a : Symbol(a, Decl(distributiveConditionalTypeRelations.ts, 89, 36)) +>b : Symbol(b, Decl(distributiveConditionalTypeRelations.ts, 89, 47)) +>get : Symbol(get, Decl(distributiveConditionalTypeRelations.ts, 89, 58)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 89, 25)) + + const result: Constant = value; +>result : Symbol(result, Decl(distributiveConditionalTypeRelations.ts, 90, 9)) +>Constant : Symbol(Constant, Decl(distributiveConditionalTypeRelations.ts, 3, 42)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 89, 25)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 89, 28)) +} + +function definitelyNotNever(value: { a: number; b: number }) { +>definitelyNotNever : Symbol(definitelyNotNever, Decl(distributiveConditionalTypeRelations.ts, 91, 1)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 93, 28)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 93, 31)) +>a : Symbol(a, Decl(distributiveConditionalTypeRelations.ts, 93, 39)) +>b : Symbol(b, Decl(distributiveConditionalTypeRelations.ts, 93, 50)) + + const object: Constant<{ value: T }> = value; +>object : Symbol(object, Decl(distributiveConditionalTypeRelations.ts, 94, 9)) +>Constant : Symbol(Constant, Decl(distributiveConditionalTypeRelations.ts, 3, 42)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 94, 28)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 93, 28)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 93, 31)) + + const array: Constant = value; +>array : Symbol(array, Decl(distributiveConditionalTypeRelations.ts, 95, 9)) +>Constant : Symbol(Constant, Decl(distributiveConditionalTypeRelations.ts, 3, 42)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 93, 28)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 93, 31)) + + const tuple: Constant<[T]> = value; +>tuple : Symbol(tuple, Decl(distributiveConditionalTypeRelations.ts, 96, 9)) +>Constant : Symbol(Constant, Decl(distributiveConditionalTypeRelations.ts, 3, 42)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 93, 28)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 93, 31)) + + const union: Constant = value; +>union : Symbol(union, Decl(distributiveConditionalTypeRelations.ts, 97, 9)) +>Constant : Symbol(Constant, Decl(distributiveConditionalTypeRelations.ts, 3, 42)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 93, 28)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 93, 31)) + + const conditional: Constant<[T] extends [string] ? { value: T } : { other: T }> = value; +>conditional : Symbol(conditional, Decl(distributiveConditionalTypeRelations.ts, 98, 9)) +>Constant : Symbol(Constant, Decl(distributiveConditionalTypeRelations.ts, 3, 42)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 93, 28)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 98, 56)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 93, 28)) +>other : Symbol(other, Decl(distributiveConditionalTypeRelations.ts, 98, 71)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 93, 28)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 93, 31)) +} + +type Homomorphic = { [K in keyof T]: T[K] }; +>Homomorphic : Symbol(Homomorphic, Decl(distributiveConditionalTypeRelations.ts, 99, 1)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 101, 17)) +>K : Symbol(K, Decl(distributiveConditionalTypeRelations.ts, 101, 25)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 101, 17)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 101, 17)) +>K : Symbol(K, Decl(distributiveConditionalTypeRelations.ts, 101, 25)) + +type Spread = [...T]; +>Spread : Symbol(Spread, Decl(distributiveConditionalTypeRelations.ts, 101, 47)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 102, 12)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 102, 12)) + +function emptyInstantiations(value: { a: number; b: number }) { +>emptyInstantiations : Symbol(emptyInstantiations, Decl(distributiveConditionalTypeRelations.ts, 102, 42)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 104, 29)) +>U : Symbol(U, Decl(distributiveConditionalTypeRelations.ts, 104, 31)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 104, 53)) +>a : Symbol(a, Decl(distributiveConditionalTypeRelations.ts, 104, 61)) +>b : Symbol(b, Decl(distributiveConditionalTypeRelations.ts, 104, 72)) + + const mapped: Constant> = value; +>mapped : Symbol(mapped, Decl(distributiveConditionalTypeRelations.ts, 105, 9)) +>Constant : Symbol(Constant, Decl(distributiveConditionalTypeRelations.ts, 3, 42)) +>Homomorphic : Symbol(Homomorphic, Decl(distributiveConditionalTypeRelations.ts, 99, 1)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 104, 29)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 104, 53)) + + const variadic: Constant> = value; +>variadic : Symbol(variadic, Decl(distributiveConditionalTypeRelations.ts, 106, 9)) +>Constant : Symbol(Constant, Decl(distributiveConditionalTypeRelations.ts, 3, 42)) +>Spread : Symbol(Spread, Decl(distributiveConditionalTypeRelations.ts, 101, 47)) +>U : Symbol(U, Decl(distributiveConditionalTypeRelations.ts, 104, 31)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 104, 53)) +} + +type Inferred = T extends (value: infer P) => unknown ? T | P : never; +>Inferred : Symbol(Inferred, Decl(distributiveConditionalTypeRelations.ts, 107, 1)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 109, 14)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 109, 14)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 109, 30)) +>P : Symbol(P, Decl(distributiveConditionalTypeRelations.ts, 109, 42)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 109, 14)) +>P : Symbol(P, Decl(distributiveConditionalTypeRelations.ts, 109, 42)) + +type InferredNonDistributive = [T] extends [(value: infer P) => unknown] ? T | P : never; +>InferredNonDistributive : Symbol(InferredNonDistributive, Decl(distributiveConditionalTypeRelations.ts, 109, 73)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 110, 29)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 110, 29)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 110, 48)) +>P : Symbol(P, Decl(distributiveConditionalTypeRelations.ts, 110, 60)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 110, 29)) +>P : Symbol(P, Decl(distributiveConditionalTypeRelations.ts, 110, 60)) + +type InferredValue = T extends { value: infer P } ? P : never; +>InferredValue : Symbol(InferredValue, Decl(distributiveConditionalTypeRelations.ts, 110, 92)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 111, 19)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 111, 19)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 111, 35)) +>P : Symbol(P, Decl(distributiveConditionalTypeRelations.ts, 111, 48)) +>P : Symbol(P, Decl(distributiveConditionalTypeRelations.ts, 111, 48)) + +type InferredCallback = T extends { value: infer P } ? (value: P) => P : never; +>InferredCallback : Symbol(InferredCallback, Decl(distributiveConditionalTypeRelations.ts, 111, 65)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 112, 22)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 112, 22)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 112, 38)) +>P : Symbol(P, Decl(distributiveConditionalTypeRelations.ts, 112, 51)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 112, 59)) +>P : Symbol(P, Decl(distributiveConditionalTypeRelations.ts, 112, 51)) +>P : Symbol(P, Decl(distributiveConditionalTypeRelations.ts, 112, 51)) + +type ConstrainedInfer = T extends { value: infer P extends string } ? T : never; +>ConstrainedInfer : Symbol(ConstrainedInfer, Decl(distributiveConditionalTypeRelations.ts, 112, 82)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 113, 22)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 113, 22)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 113, 38)) +>P : Symbol(P, Decl(distributiveConditionalTypeRelations.ts, 113, 51)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 113, 22)) + +function inference unknown>(value: T) { +>inference : Symbol(inference, Decl(distributiveConditionalTypeRelations.ts, 113, 83)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 115, 19)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 115, 30)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 115, 54)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 115, 19)) + + const distributive: Inferred = value; // Error: additional overloads can change the inferred parameter. +>distributive : Symbol(distributive, Decl(distributiveConditionalTypeRelations.ts, 116, 9)) +>Inferred : Symbol(Inferred, Decl(distributiveConditionalTypeRelations.ts, 107, 1)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 115, 19)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 115, 54)) + + const nonDistributive: InferredNonDistributive = value; // Error +>nonDistributive : Symbol(nonDistributive, Decl(distributiveConditionalTypeRelations.ts, 117, 9)) +>InferredNonDistributive : Symbol(InferredNonDistributive, Decl(distributiveConditionalTypeRelations.ts, 109, 73)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 115, 19)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 115, 54)) +} + +function independentInference(value: T) { +>independentInference : Symbol(independentInference, Decl(distributiveConditionalTypeRelations.ts, 118, 1)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 120, 30)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 120, 33)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 120, 30)) + + const result: T extends infer P ? T | P : never = value; +>result : Symbol(result, Decl(distributiveConditionalTypeRelations.ts, 121, 9)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 120, 30)) +>P : Symbol(P, Decl(distributiveConditionalTypeRelations.ts, 121, 33)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 120, 30)) +>P : Symbol(P, Decl(distributiveConditionalTypeRelations.ts, 121, 33)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 120, 33)) +} + +function propertyInference(value: T) { +>propertyInference : Symbol(propertyInference, Decl(distributiveConditionalTypeRelations.ts, 122, 1)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 124, 27)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 124, 38)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 124, 57)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 124, 27)) + + const result: T extends { value: infer P } ? T | P : never = value; +>result : Symbol(result, Decl(distributiveConditionalTypeRelations.ts, 125, 9)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 124, 27)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 125, 29)) +>P : Symbol(P, Decl(distributiveConditionalTypeRelations.ts, 125, 42)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 124, 27)) +>P : Symbol(P, Decl(distributiveConditionalTypeRelations.ts, 125, 42)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 124, 57)) +} + +function tupleInference(value: T) { +>tupleInference : Symbol(tupleInference, Decl(distributiveConditionalTypeRelations.ts, 126, 1)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 128, 24)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 128, 27)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 128, 24)) + + const result: [T] extends [infer P] ? T | P : never = value; +>result : Symbol(result, Decl(distributiveConditionalTypeRelations.ts, 129, 9)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 128, 24)) +>P : Symbol(P, Decl(distributiveConditionalTypeRelations.ts, 129, 36)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 128, 24)) +>P : Symbol(P, Decl(distributiveConditionalTypeRelations.ts, 129, 36)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 128, 27)) +} + +function independentParameters unknown>(value: T) { +>independentParameters : Symbol(independentParameters, Decl(distributiveConditionalTypeRelations.ts, 130, 1)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 132, 31)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 132, 42)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 132, 66)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 132, 31)) + + const result: T extends (value: infer P) => infer R ? T | P | R : never = value; // Error +>result : Symbol(result, Decl(distributiveConditionalTypeRelations.ts, 133, 9)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 132, 31)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 133, 29)) +>P : Symbol(P, Decl(distributiveConditionalTypeRelations.ts, 133, 41)) +>R : Symbol(R, Decl(distributiveConditionalTypeRelations.ts, 133, 53)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 132, 31)) +>P : Symbol(P, Decl(distributiveConditionalTypeRelations.ts, 133, 41)) +>R : Symbol(R, Decl(distributiveConditionalTypeRelations.ts, 133, 53)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 132, 66)) +} + +type InferredReturn = T extends () => infer R ? T | R : never; +>InferredReturn : Symbol(InferredReturn, Decl(distributiveConditionalTypeRelations.ts, 134, 1)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 136, 20)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 136, 20)) +>R : Symbol(R, Decl(distributiveConditionalTypeRelations.ts, 136, 46)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 136, 20)) +>R : Symbol(R, Decl(distributiveConditionalTypeRelations.ts, 136, 46)) + +function returnInference unknown>(value: T) { +>returnInference : Symbol(returnInference, Decl(distributiveConditionalTypeRelations.ts, 136, 65)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 138, 25)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 138, 50)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 138, 25)) + + const result: InferredReturn = value; // Error: inference can use an overload with incompatible arity. +>result : Symbol(result, Decl(distributiveConditionalTypeRelations.ts, 139, 9)) +>InferredReturn : Symbol(InferredReturn, Decl(distributiveConditionalTypeRelations.ts, 134, 1)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 138, 25)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 138, 50)) +} + +function universalSignature never>(value: T) { +>universalSignature : Symbol(universalSignature, Decl(distributiveConditionalTypeRelations.ts, 140, 1)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 142, 28)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 142, 39)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 142, 65)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 142, 28)) + + const parameter: Inferred = value; +>parameter : Symbol(parameter, Decl(distributiveConditionalTypeRelations.ts, 143, 9)) +>Inferred : Symbol(Inferred, Decl(distributiveConditionalTypeRelations.ts, 107, 1)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 142, 28)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 142, 65)) + + const both: T extends (value: infer P) => infer R ? T | P | R : never = value; +>both : Symbol(both, Decl(distributiveConditionalTypeRelations.ts, 144, 9)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 142, 28)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 144, 27)) +>P : Symbol(P, Decl(distributiveConditionalTypeRelations.ts, 144, 39)) +>R : Symbol(R, Decl(distributiveConditionalTypeRelations.ts, 144, 51)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 142, 28)) +>P : Symbol(P, Decl(distributiveConditionalTypeRelations.ts, 144, 39)) +>R : Symbol(R, Decl(distributiveConditionalTypeRelations.ts, 144, 51)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 142, 65)) +} + +function ignoredParameter never>(value: T) { +>ignoredParameter : Symbol(ignoredParameter, Decl(distributiveConditionalTypeRelations.ts, 145, 1)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 147, 26)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 147, 49)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 147, 26)) + + const result: Inferred = value; +>result : Symbol(result, Decl(distributiveConditionalTypeRelations.ts, 148, 9)) +>Inferred : Symbol(Inferred, Decl(distributiveConditionalTypeRelations.ts, 107, 1)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 147, 26)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 147, 49)) + + const returned: InferredReturn = value; +>returned : Symbol(returned, Decl(distributiveConditionalTypeRelations.ts, 149, 9)) +>InferredReturn : Symbol(InferredReturn, Decl(distributiveConditionalTypeRelations.ts, 134, 1)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 147, 26)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 147, 49)) +} + +type InferredMethod = T extends { method: (value: infer P) => unknown } ? T | P : never; +>InferredMethod : Symbol(InferredMethod, Decl(distributiveConditionalTypeRelations.ts, 150, 1)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 152, 20)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 152, 20)) +>method : Symbol(method, Decl(distributiveConditionalTypeRelations.ts, 152, 36)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 152, 46)) +>P : Symbol(P, Decl(distributiveConditionalTypeRelations.ts, 152, 58)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 152, 20)) +>P : Symbol(P, Decl(distributiveConditionalTypeRelations.ts, 152, 58)) + +type InferredMethodReturn = T extends { method: (value: any) => infer R } ? T | R : never; +>InferredMethodReturn : Symbol(InferredMethodReturn, Decl(distributiveConditionalTypeRelations.ts, 152, 91)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 153, 26)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 153, 26)) +>method : Symbol(method, Decl(distributiveConditionalTypeRelations.ts, 153, 42)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 153, 52)) +>R : Symbol(R, Decl(distributiveConditionalTypeRelations.ts, 153, 72)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 153, 26)) +>R : Symbol(R, Decl(distributiveConditionalTypeRelations.ts, 153, 72)) + +function bivariantInference(value: T) { +>bivariantInference : Symbol(bivariantInference, Decl(distributiveConditionalTypeRelations.ts, 153, 93)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 155, 28)) +>method : Symbol(method, Decl(distributiveConditionalTypeRelations.ts, 155, 39)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 155, 47)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 155, 73)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 155, 28)) + + const result: InferredMethod = value; // Error: the constraint permits narrower method parameters. +>result : Symbol(result, Decl(distributiveConditionalTypeRelations.ts, 156, 9)) +>InferredMethod : Symbol(InferredMethod, Decl(distributiveConditionalTypeRelations.ts, 150, 1)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 155, 28)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 155, 73)) + + const returned: InferredMethodReturn = value; // Error: a never parameter fails the strict function pattern. +>returned : Symbol(returned, Decl(distributiveConditionalTypeRelations.ts, 157, 9)) +>InferredMethodReturn : Symbol(InferredMethodReturn, Decl(distributiveConditionalTypeRelations.ts, 152, 91)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 155, 28)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 155, 73)) +} + +function dependentInference(value: string, callback: (value: string) => string) { +>dependentInference : Symbol(dependentInference, Decl(distributiveConditionalTypeRelations.ts, 158, 1)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 160, 28)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 160, 39)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 160, 57)) +>callback : Symbol(callback, Decl(distributiveConditionalTypeRelations.ts, 160, 71)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 160, 83)) + + const inferred: InferredValue = value; // Error: T can contain a narrower value. +>inferred : Symbol(inferred, Decl(distributiveConditionalTypeRelations.ts, 161, 9)) +>InferredValue : Symbol(InferredValue, Decl(distributiveConditionalTypeRelations.ts, 110, 92)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 160, 28)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 160, 57)) + + const inferredCallback: InferredCallback = callback; // Error +>inferredCallback : Symbol(inferredCallback, Decl(distributiveConditionalTypeRelations.ts, 162, 9)) +>InferredCallback : Symbol(InferredCallback, Decl(distributiveConditionalTypeRelations.ts, 111, 65)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 160, 28)) +>callback : Symbol(callback, Decl(distributiveConditionalTypeRelations.ts, 160, 71)) +} + +function constrainedInference(value: T) { +>constrainedInference : Symbol(constrainedInference, Decl(distributiveConditionalTypeRelations.ts, 163, 1)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 165, 30)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 165, 41)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 165, 56)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 165, 30)) + + const result: ConstrainedInfer = value; // Error: any in the constraint does not imply string. +>result : Symbol(result, Decl(distributiveConditionalTypeRelations.ts, 166, 9)) +>ConstrainedInfer : Symbol(ConstrainedInfer, Decl(distributiveConditionalTypeRelations.ts, 112, 82)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 165, 30)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 165, 56)) +} + +type SpecificParameter = T extends (value: string) => infer R ? T | R : never; +>SpecificParameter : Symbol(SpecificParameter, Decl(distributiveConditionalTypeRelations.ts, 167, 1)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 169, 23)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 169, 23)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 169, 39)) +>R : Symbol(R, Decl(distributiveConditionalTypeRelations.ts, 169, 62)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 169, 23)) +>R : Symbol(R, Decl(distributiveConditionalTypeRelations.ts, 169, 62)) + +type SpecificMethod = T extends { method(value: string): infer R } ? T | R : never; +>SpecificMethod : Symbol(SpecificMethod, Decl(distributiveConditionalTypeRelations.ts, 169, 81)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 170, 20)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 170, 20)) +>method : Symbol(method, Decl(distributiveConditionalTypeRelations.ts, 170, 36)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 170, 44)) +>R : Symbol(R, Decl(distributiveConditionalTypeRelations.ts, 170, 65)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 170, 20)) +>R : Symbol(R, Decl(distributiveConditionalTypeRelations.ts, 170, 65)) + +type OptionalProperty = T extends { optional?: string; value: infer R } ? T | R : never; +>OptionalProperty : Symbol(OptionalProperty, Decl(distributiveConditionalTypeRelations.ts, 170, 86)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 171, 22)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 171, 22)) +>optional : Symbol(optional, Decl(distributiveConditionalTypeRelations.ts, 171, 38)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 171, 57)) +>R : Symbol(R, Decl(distributiveConditionalTypeRelations.ts, 171, 70)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 171, 22)) +>R : Symbol(R, Decl(distributiveConditionalTypeRelations.ts, 171, 70)) + +type MixedInference = T extends { input: (value: infer R) => void; output: infer R } ? T | R : never; +>MixedInference : Symbol(MixedInference, Decl(distributiveConditionalTypeRelations.ts, 171, 91)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 172, 20)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 172, 20)) +>input : Symbol(input, Decl(distributiveConditionalTypeRelations.ts, 172, 36)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 172, 45)) +>R : Symbol(R, Decl(distributiveConditionalTypeRelations.ts, 172, 57), Decl(distributiveConditionalTypeRelations.ts, 172, 83)) +>output : Symbol(output, Decl(distributiveConditionalTypeRelations.ts, 172, 69)) +>R : Symbol(R, Decl(distributiveConditionalTypeRelations.ts, 172, 57), Decl(distributiveConditionalTypeRelations.ts, 172, 83)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 172, 20)) +>R : Symbol(R, Decl(distributiveConditionalTypeRelations.ts, 172, 57), Decl(distributiveConditionalTypeRelations.ts, 172, 83)) + +type SingleParameter = T extends (value: infer R) => unknown ? T | R : never; +>SingleParameter : Symbol(SingleParameter, Decl(distributiveConditionalTypeRelations.ts, 172, 104)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 173, 21)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 173, 21)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 173, 37)) +>R : Symbol(R, Decl(distributiveConditionalTypeRelations.ts, 173, 49)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 173, 21)) +>R : Symbol(R, Decl(distributiveConditionalTypeRelations.ts, 173, 49)) + +type OptionalParameter = T extends (value?: infer R) => unknown ? T | R : never; +>OptionalParameter : Symbol(OptionalParameter, Decl(distributiveConditionalTypeRelations.ts, 173, 80)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 174, 23)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 174, 23)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 174, 39)) +>R : Symbol(R, Decl(distributiveConditionalTypeRelations.ts, 174, 52)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 174, 23)) +>R : Symbol(R, Decl(distributiveConditionalTypeRelations.ts, 174, 52)) + +type IntersectionPattern = T extends { value: infer R } & { method(value: string): unknown } ? T | R : never; +>IntersectionPattern : Symbol(IntersectionPattern, Decl(distributiveConditionalTypeRelations.ts, 174, 83)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 175, 25)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 175, 25)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 175, 41)) +>R : Symbol(R, Decl(distributiveConditionalTypeRelations.ts, 175, 54)) +>method : Symbol(method, Decl(distributiveConditionalTypeRelations.ts, 175, 62)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 175, 70)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 175, 25)) +>R : Symbol(R, Decl(distributiveConditionalTypeRelations.ts, 175, 54)) + +type PrimitiveBoundary = T extends object | (infer R extends number) ? T | R : never; +>PrimitiveBoundary : Symbol(PrimitiveBoundary, Decl(distributiveConditionalTypeRelations.ts, 175, 112)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 176, 23)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 176, 23)) +>R : Symbol(R, Decl(distributiveConditionalTypeRelations.ts, 176, 53)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 176, 23)) +>R : Symbol(R, Decl(distributiveConditionalTypeRelations.ts, 176, 53)) + +function fixedParameter unknown>(value: T) { +>fixedParameter : Symbol(fixedParameter, Decl(distributiveConditionalTypeRelations.ts, 176, 88)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 178, 24)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 178, 35)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 178, 59)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 178, 24)) + + const result: SpecificParameter = value; // Error: a number parameter also satisfies the constraint. +>result : Symbol(result, Decl(distributiveConditionalTypeRelations.ts, 179, 9)) +>SpecificParameter : Symbol(SpecificParameter, Decl(distributiveConditionalTypeRelations.ts, 167, 1)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 178, 24)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 178, 59)) + + const withoutInference: T extends (value: string) => unknown ? T : never = value; // Error +>withoutInference : Symbol(withoutInference, Decl(distributiveConditionalTypeRelations.ts, 180, 9)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 178, 24)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 180, 39)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 178, 24)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 178, 59)) +} + +function bivariantParameter(value: T) { +>bivariantParameter : Symbol(bivariantParameter, Decl(distributiveConditionalTypeRelations.ts, 181, 1)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 183, 28)) +>method : Symbol(method, Decl(distributiveConditionalTypeRelations.ts, 183, 39)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 183, 47)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 183, 75)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 183, 28)) + + const result: SpecificMethod = value; // Error +>result : Symbol(result, Decl(distributiveConditionalTypeRelations.ts, 184, 9)) +>SpecificMethod : Symbol(SpecificMethod, Decl(distributiveConditionalTypeRelations.ts, 169, 81)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 183, 28)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 183, 75)) +} + +function optionalProperty(value: T) { +>optionalProperty : Symbol(optionalProperty, Decl(distributiveConditionalTypeRelations.ts, 185, 1)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 187, 26)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 187, 37)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 187, 56)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 187, 26)) + + const result: OptionalProperty = value; // Error: subtypes can add an incompatible optional property. +>result : Symbol(result, Decl(distributiveConditionalTypeRelations.ts, 188, 9)) +>OptionalProperty : Symbol(OptionalProperty, Decl(distributiveConditionalTypeRelations.ts, 170, 86)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 187, 26)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 187, 56)) +} + +function mixedInference void; output: any }>(value: T) { +>mixedInference : Symbol(mixedInference, Decl(distributiveConditionalTypeRelations.ts, 189, 1)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 191, 24)) +>input : Symbol(input, Decl(distributiveConditionalTypeRelations.ts, 191, 35)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 191, 44)) +>output : Symbol(output, Decl(distributiveConditionalTypeRelations.ts, 191, 64)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 191, 80)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 191, 24)) + + const result: MixedInference = value; // Error: input and output can require incompatible inferences. +>result : Symbol(result, Decl(distributiveConditionalTypeRelations.ts, 192, 9)) +>MixedInference : Symbol(MixedInference, Decl(distributiveConditionalTypeRelations.ts, 171, 91)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 191, 24)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 191, 80)) +} + +interface InvariantInferencePattern { +>InvariantInferencePattern : Symbol(InvariantInferencePattern, Decl(distributiveConditionalTypeRelations.ts, 193, 1)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 195, 36)) + + input: (value: T) => void; +>input : Symbol(InvariantInferencePattern.input, Decl(distributiveConditionalTypeRelations.ts, 195, 40)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 196, 12)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 195, 36)) + + output: T; +>output : Symbol(InvariantInferencePattern.output, Decl(distributiveConditionalTypeRelations.ts, 196, 30)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 195, 36)) +} + +type InvariantInference = T extends InvariantInferencePattern ? T | R : never; +>InvariantInference : Symbol(InvariantInference, Decl(distributiveConditionalTypeRelations.ts, 198, 1)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 200, 24)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 200, 24)) +>InvariantInferencePattern : Symbol(InvariantInferencePattern, Decl(distributiveConditionalTypeRelations.ts, 193, 1)) +>R : Symbol(R, Decl(distributiveConditionalTypeRelations.ts, 200, 70)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 200, 24)) +>R : Symbol(R, Decl(distributiveConditionalTypeRelations.ts, 200, 70)) + +function invariantInference>(value: T) { +>invariantInference : Symbol(invariantInference, Decl(distributiveConditionalTypeRelations.ts, 200, 90)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 202, 28)) +>InvariantInferencePattern : Symbol(InvariantInferencePattern, Decl(distributiveConditionalTypeRelations.ts, 193, 1)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 202, 70)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 202, 28)) + + const result: InvariantInference = value; // Error: invariant type-argument inference must not hide mixed positions. +>result : Symbol(result, Decl(distributiveConditionalTypeRelations.ts, 203, 9)) +>InvariantInference : Symbol(InvariantInference, Decl(distributiveConditionalTypeRelations.ts, 198, 1)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 202, 28)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 202, 70)) +} + +enum NumericInferencePattern { +>NumericInferencePattern : Symbol(NumericInferencePattern, Decl(distributiveConditionalTypeRelations.ts, 204, 1)) + + value +>value : Symbol(NumericInferencePattern.value, Decl(distributiveConditionalTypeRelations.ts, 206, 30)) +} + +type NumericInference = T extends (value: infer P) => NumericInferencePattern ? T | P : never; +>NumericInference : Symbol(NumericInference, Decl(distributiveConditionalTypeRelations.ts, 208, 1)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 210, 22)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 210, 22)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 210, 38)) +>P : Symbol(P, Decl(distributiveConditionalTypeRelations.ts, 210, 50)) +>NumericInferencePattern : Symbol(NumericInferencePattern, Decl(distributiveConditionalTypeRelations.ts, 204, 1)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 210, 22)) +>P : Symbol(P, Decl(distributiveConditionalTypeRelations.ts, 210, 50)) + +function numericInference number>(value: T) { +>numericInference : Symbol(numericInference, Decl(distributiveConditionalTypeRelations.ts, 210, 97)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 212, 26)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 212, 37)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 212, 64)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 212, 26)) + + const result: NumericInference = value; // Error: number-to-enum compatibility does not cover every numeric literal. +>result : Symbol(result, Decl(distributiveConditionalTypeRelations.ts, 213, 9)) +>NumericInference : Symbol(NumericInference, Decl(distributiveConditionalTypeRelations.ts, 208, 1)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 212, 26)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 212, 64)) +} + +function unboundedArity unknown>(value: T) { +>unboundedArity : Symbol(unboundedArity, Decl(distributiveConditionalTypeRelations.ts, 214, 1)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 216, 24)) +>args : Symbol(args, Decl(distributiveConditionalTypeRelations.ts, 216, 35)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 216, 63)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 216, 24)) + + const result: SingleParameter = value; // Error: subtypes can require multiple arguments. +>result : Symbol(result, Decl(distributiveConditionalTypeRelations.ts, 217, 9)) +>SingleParameter : Symbol(SingleParameter, Decl(distributiveConditionalTypeRelations.ts, 172, 104)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 216, 24)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 216, 63)) +} + +function optionalParameter unknown>(value: T) { +>optionalParameter : Symbol(optionalParameter, Decl(distributiveConditionalTypeRelations.ts, 218, 1)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 220, 27)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 220, 38)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 220, 63)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 220, 27)) + + const result: OptionalParameter = value; // Error: subtypes can require an argument. +>result : Symbol(result, Decl(distributiveConditionalTypeRelations.ts, 221, 9)) +>OptionalParameter : Symbol(OptionalParameter, Decl(distributiveConditionalTypeRelations.ts, 173, 80)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 220, 27)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 220, 63)) +} + +function intersectionPattern(value: T) { +>intersectionPattern : Symbol(intersectionPattern, Decl(distributiveConditionalTypeRelations.ts, 222, 1)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 224, 29)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 224, 40)) +>method : Symbol(method, Decl(distributiveConditionalTypeRelations.ts, 224, 52)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 224, 60)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 224, 84)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 224, 29)) + + const result: IntersectionPattern = value; // Error +>result : Symbol(result, Decl(distributiveConditionalTypeRelations.ts, 225, 9)) +>IntersectionPattern : Symbol(IntersectionPattern, Decl(distributiveConditionalTypeRelations.ts, 174, 83)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 224, 29)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 224, 84)) +} + +function primitiveBoundary(value: T) { +>primitiveBoundary : Symbol(primitiveBoundary, Decl(distributiveConditionalTypeRelations.ts, 226, 1)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 228, 27)) +>toString : Symbol(toString, Decl(distributiveConditionalTypeRelations.ts, 228, 38)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 228, 61)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 228, 27)) + + const result: PrimitiveBoundary = value; // Error: structural object constraints admit primitives. +>result : Symbol(result, Decl(distributiveConditionalTypeRelations.ts, 229, 9)) +>PrimitiveBoundary : Symbol(PrimitiveBoundary, Decl(distributiveConditionalTypeRelations.ts, 175, 112)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 228, 27)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 228, 61)) +} + +class Nominal { +>Nominal : Symbol(Nominal, Decl(distributiveConditionalTypeRelations.ts, 230, 1)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 232, 14)) + + private brand!: void; +>brand : Symbol(Nominal.brand, Decl(distributiveConditionalTypeRelations.ts, 232, 18)) + + value!: T; +>value : Symbol(Nominal.value, Decl(distributiveConditionalTypeRelations.ts, 233, 25)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 232, 14)) +} + +function nominalInference(value: T) { +>nominalInference : Symbol(nominalInference, Decl(distributiveConditionalTypeRelations.ts, 235, 1)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 237, 26)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 237, 37)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 237, 56)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 237, 26)) + + const result: T extends Nominal ? T | P : never = value; // Error +>result : Symbol(result, Decl(distributiveConditionalTypeRelations.ts, 238, 9)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 237, 26)) +>Nominal : Symbol(Nominal, Decl(distributiveConditionalTypeRelations.ts, 230, 1)) +>P : Symbol(P, Decl(distributiveConditionalTypeRelations.ts, 238, 41)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 237, 26)) +>P : Symbol(P, Decl(distributiveConditionalTypeRelations.ts, 238, 41)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 237, 56)) +} + +type FilteredKeys = T extends U ? never : T; +>FilteredKeys : Symbol(FilteredKeys, Decl(distributiveConditionalTypeRelations.ts, 239, 1)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 241, 18)) +>U : Symbol(U, Decl(distributiveConditionalTypeRelations.ts, 241, 20)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 241, 18)) +>U : Symbol(U, Decl(distributiveConditionalTypeRelations.ts, 241, 20)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 241, 18)) + +type DecoratedKeys = T extends U ? never : `key-${T}`; +>DecoratedKeys : Symbol(DecoratedKeys, Decl(distributiveConditionalTypeRelations.ts, 241, 50)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 242, 19)) +>U : Symbol(U, Decl(distributiveConditionalTypeRelations.ts, 242, 36)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 242, 19)) +>U : Symbol(U, Decl(distributiveConditionalTypeRelations.ts, 242, 36)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 242, 19)) + +function filtering(value: FilteredKeys) { +>filtering : Symbol(filtering, Decl(distributiveConditionalTypeRelations.ts, 242, 75)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 244, 19)) +>U : Symbol(U, Decl(distributiveConditionalTypeRelations.ts, 244, 36)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 244, 40)) +>FilteredKeys : Symbol(FilteredKeys, Decl(distributiveConditionalTypeRelations.ts, 239, 1)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 244, 19)) +>U : Symbol(U, Decl(distributiveConditionalTypeRelations.ts, 244, 36)) + + const excluded: Exclude = value; +>excluded : Symbol(excluded, Decl(distributiveConditionalTypeRelations.ts, 245, 9)) +>Exclude : Symbol(Exclude, Decl(lib.es5.d.ts, --, --)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 244, 19)) +>U : Symbol(U, Decl(distributiveConditionalTypeRelations.ts, 244, 36)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 244, 40)) + + const remapped: DecoratedKeys, never> = `key-${value}`; +>remapped : Symbol(remapped, Decl(distributiveConditionalTypeRelations.ts, 246, 9)) +>DecoratedKeys : Symbol(DecoratedKeys, Decl(distributiveConditionalTypeRelations.ts, 241, 50)) +>FilteredKeys : Symbol(FilteredKeys, Decl(distributiveConditionalTypeRelations.ts, 239, 1)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 244, 19)) +>U : Symbol(U, Decl(distributiveConditionalTypeRelations.ts, 244, 36)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 244, 40)) +} + +function filteredRemapping(value: `key-${Exclude}`) { +>filteredRemapping : Symbol(filteredRemapping, Decl(distributiveConditionalTypeRelations.ts, 247, 1)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 249, 27)) +>U : Symbol(U, Decl(distributiveConditionalTypeRelations.ts, 249, 44)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 249, 48)) +>Exclude : Symbol(Exclude, Decl(lib.es5.d.ts, --, --)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 249, 27)) +>U : Symbol(U, Decl(distributiveConditionalTypeRelations.ts, 249, 44)) + + const result: DecoratedKeys = value; +>result : Symbol(result, Decl(distributiveConditionalTypeRelations.ts, 250, 9)) +>DecoratedKeys : Symbol(DecoratedKeys, Decl(distributiveConditionalTypeRelations.ts, 241, 50)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 249, 27)) +>U : Symbol(U, Decl(distributiveConditionalTypeRelations.ts, 249, 44)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 249, 48)) +} + +type FilteredMap = { [K in T as K extends U ? never : K]: unknown }; +>FilteredMap : Symbol(FilteredMap, Decl(distributiveConditionalTypeRelations.ts, 251, 1)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 253, 17)) +>U : Symbol(U, Decl(distributiveConditionalTypeRelations.ts, 253, 34)) +>K : Symbol(K, Decl(distributiveConditionalTypeRelations.ts, 253, 43)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 253, 17)) +>K : Symbol(K, Decl(distributiveConditionalTypeRelations.ts, 253, 43)) +>U : Symbol(U, Decl(distributiveConditionalTypeRelations.ts, 253, 34)) +>K : Symbol(K, Decl(distributiveConditionalTypeRelations.ts, 253, 43)) + +type DecoratedMap = { [K in T as K extends U ? never : `key-${K}`]: unknown }; +>DecoratedMap : Symbol(DecoratedMap, Decl(distributiveConditionalTypeRelations.ts, 253, 89)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 254, 18)) +>U : Symbol(U, Decl(distributiveConditionalTypeRelations.ts, 254, 35)) +>K : Symbol(K, Decl(distributiveConditionalTypeRelations.ts, 254, 44)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 254, 18)) +>K : Symbol(K, Decl(distributiveConditionalTypeRelations.ts, 254, 44)) +>U : Symbol(U, Decl(distributiveConditionalTypeRelations.ts, 254, 35)) +>K : Symbol(K, Decl(distributiveConditionalTypeRelations.ts, 254, 44)) + +function mappedKeys(key: Exclude, decorated: `key-${Exclude}`) { +>mappedKeys : Symbol(mappedKeys, Decl(distributiveConditionalTypeRelations.ts, 254, 99)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 256, 20)) +>U : Symbol(U, Decl(distributiveConditionalTypeRelations.ts, 256, 37)) +>key : Symbol(key, Decl(distributiveConditionalTypeRelations.ts, 256, 41)) +>Exclude : Symbol(Exclude, Decl(lib.es5.d.ts, --, --)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 256, 20)) +>U : Symbol(U, Decl(distributiveConditionalTypeRelations.ts, 256, 37)) +>decorated : Symbol(decorated, Decl(distributiveConditionalTypeRelations.ts, 256, 60)) +>Exclude : Symbol(Exclude, Decl(lib.es5.d.ts, --, --)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 256, 20)) +>U : Symbol(U, Decl(distributiveConditionalTypeRelations.ts, 256, 37)) + + const filtered: keyof FilteredMap = key; +>filtered : Symbol(filtered, Decl(distributiveConditionalTypeRelations.ts, 257, 9)) +>FilteredMap : Symbol(FilteredMap, Decl(distributiveConditionalTypeRelations.ts, 251, 1)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 256, 20)) +>U : Symbol(U, Decl(distributiveConditionalTypeRelations.ts, 256, 37)) +>key : Symbol(key, Decl(distributiveConditionalTypeRelations.ts, 256, 41)) + + const remapped: keyof DecoratedMap = decorated; +>remapped : Symbol(remapped, Decl(distributiveConditionalTypeRelations.ts, 258, 9)) +>DecoratedMap : Symbol(DecoratedMap, Decl(distributiveConditionalTypeRelations.ts, 253, 89)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 256, 20)) +>U : Symbol(U, Decl(distributiveConditionalTypeRelations.ts, 256, 37)) +>decorated : Symbol(decorated, Decl(distributiveConditionalTypeRelations.ts, 256, 60)) +} + +type Recursive = T extends object ? T | { next: Recursive } : T; +>Recursive : Symbol(Recursive, Decl(distributiveConditionalTypeRelations.ts, 259, 1)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 261, 15)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 261, 15)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 261, 15)) +>next : Symbol(next, Decl(distributiveConditionalTypeRelations.ts, 261, 44)) +>Recursive : Symbol(Recursive, Decl(distributiveConditionalTypeRelations.ts, 259, 1)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 261, 15)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 261, 15)) + +function recursion(value: T) { +>recursion : Symbol(recursion, Decl(distributiveConditionalTypeRelations.ts, 261, 70)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 263, 19)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 263, 22)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 263, 19)) + + const result: Recursive = value; +>result : Symbol(result, Decl(distributiveConditionalTypeRelations.ts, 264, 9)) +>Recursive : Symbol(Recursive, Decl(distributiveConditionalTypeRelations.ts, 259, 1)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 263, 19)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 263, 22)) +} + +function subtypeIsNotConstituent(value: T & string) { +>subtypeIsNotConstituent : Symbol(subtypeIsNotConstituent, Decl(distributiveConditionalTypeRelations.ts, 265, 1)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 267, 33)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 267, 36)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 267, 33)) + + const result: Extract = value; // Error: Extract is never. +>result : Symbol(result, Decl(distributiveConditionalTypeRelations.ts, 268, 9)) +>Extract : Symbol(Extract, Decl(lib.es5.d.ts, --, --)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelations.ts, 267, 33)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 267, 36)) +} + +// Concrete instantiations witnessing the rejected assignments. +const mixedPair: Pair<"a" | "b"> = { x: "a", y: "b" }; // Error +>mixedPair : Symbol(mixedPair, Decl(distributiveConditionalTypeRelations.ts, 272, 5)) +>Pair : Symbol(Pair, Decl(distributiveConditionalTypeRelations.ts, 4, 68)) +>x : Symbol(x, Decl(distributiveConditionalTypeRelations.ts, 272, 36)) +>y : Symbol(y, Decl(distributiveConditionalTypeRelations.ts, 272, 44)) + +const mixedTuple: Tuple<"a" | "b"> = ["a", "b"]; // Error +>mixedTuple : Symbol(mixedTuple, Decl(distributiveConditionalTypeRelations.ts, 273, 5)) +>Tuple : Symbol(Tuple, Decl(distributiveConditionalTypeRelations.ts, 5, 58)) + +const mixedString: Repeated<"a" | "b"> = "a-b"; // Error +>mixedString : Symbol(mixedString, Decl(distributiveConditionalTypeRelations.ts, 274, 5)) +>Repeated : Symbol(Repeated, Decl(distributiveConditionalTypeRelations.ts, 7, 63)) + +const emptyConstant: Constant = { a: 1, b: 2 }; // Error +>emptyConstant : Symbol(emptyConstant, Decl(distributiveConditionalTypeRelations.ts, 275, 5)) +>Constant : Symbol(Constant, Decl(distributiveConditionalTypeRelations.ts, 3, 42)) +>a : Symbol(a, Decl(distributiveConditionalTypeRelations.ts, 275, 40)) +>b : Symbol(b, Decl(distributiveConditionalTypeRelations.ts, 275, 46)) + +const narrowInference: InferredValue<{ value: "a" }> = "b"; // Error +>narrowInference : Symbol(narrowInference, Decl(distributiveConditionalTypeRelations.ts, 276, 5)) +>InferredValue : Symbol(InferredValue, Decl(distributiveConditionalTypeRelations.ts, 110, 92)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 276, 38)) + +const rejectedConstrainedInfer: ConstrainedInfer<{ value: number }> = { value: 1 }; // Error +>rejectedConstrainedInfer : Symbol(rejectedConstrainedInfer, Decl(distributiveConditionalTypeRelations.ts, 277, 5)) +>ConstrainedInfer : Symbol(ConstrainedInfer, Decl(distributiveConditionalTypeRelations.ts, 112, 82)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 277, 50)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 277, 71)) + +const rejectedFixedParameter: SpecificParameter<(value: number) => boolean> = (value: number) => true; // Error +>rejectedFixedParameter : Symbol(rejectedFixedParameter, Decl(distributiveConditionalTypeRelations.ts, 278, 5)) +>SpecificParameter : Symbol(SpecificParameter, Decl(distributiveConditionalTypeRelations.ts, 167, 1)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 278, 49)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 278, 79)) + +const rejectedMethod: SpecificMethod<{ method(value: number): boolean }> = { method: (value: number) => true }; // Error +>rejectedMethod : Symbol(rejectedMethod, Decl(distributiveConditionalTypeRelations.ts, 279, 5)) +>SpecificMethod : Symbol(SpecificMethod, Decl(distributiveConditionalTypeRelations.ts, 169, 81)) +>method : Symbol(method, Decl(distributiveConditionalTypeRelations.ts, 279, 38)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 279, 46)) +>method : Symbol(method, Decl(distributiveConditionalTypeRelations.ts, 279, 76)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 279, 86)) + +const rejectedOptionalProperty: OptionalProperty<{ optional: number; value: boolean }> = { optional: 1, value: true }; // Error +>rejectedOptionalProperty : Symbol(rejectedOptionalProperty, Decl(distributiveConditionalTypeRelations.ts, 280, 5)) +>OptionalProperty : Symbol(OptionalProperty, Decl(distributiveConditionalTypeRelations.ts, 170, 86)) +>optional : Symbol(optional, Decl(distributiveConditionalTypeRelations.ts, 280, 50)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 280, 68)) +>optional : Symbol(optional, Decl(distributiveConditionalTypeRelations.ts, 280, 90)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 280, 103)) + +const rejectedMixedInference: MixedInference<{ input(value: number): void; output: string }> = { input: (value: number) => {}, output: "" }; // Error +>rejectedMixedInference : Symbol(rejectedMixedInference, Decl(distributiveConditionalTypeRelations.ts, 281, 5)) +>MixedInference : Symbol(MixedInference, Decl(distributiveConditionalTypeRelations.ts, 171, 91)) +>input : Symbol(input, Decl(distributiveConditionalTypeRelations.ts, 281, 46)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 281, 53)) +>output : Symbol(output, Decl(distributiveConditionalTypeRelations.ts, 281, 74)) +>input : Symbol(input, Decl(distributiveConditionalTypeRelations.ts, 281, 96)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 281, 105)) +>output : Symbol(output, Decl(distributiveConditionalTypeRelations.ts, 281, 126)) + +const rejectedInvariantInference: InvariantInference<{ input: (value: number) => void; output: string }> = { input: (value: number) => {}, output: "" }; // Error +>rejectedInvariantInference : Symbol(rejectedInvariantInference, Decl(distributiveConditionalTypeRelations.ts, 282, 5)) +>InvariantInference : Symbol(InvariantInference, Decl(distributiveConditionalTypeRelations.ts, 198, 1)) +>input : Symbol(input, Decl(distributiveConditionalTypeRelations.ts, 282, 54)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 282, 63)) +>output : Symbol(output, Decl(distributiveConditionalTypeRelations.ts, 282, 86)) +>input : Symbol(input, Decl(distributiveConditionalTypeRelations.ts, 282, 108)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 282, 117)) +>output : Symbol(output, Decl(distributiveConditionalTypeRelations.ts, 282, 138)) + +const rejectedNumericInference: NumericInference<(value: unknown) => 123> = (value: unknown) => 123; // Error +>rejectedNumericInference : Symbol(rejectedNumericInference, Decl(distributiveConditionalTypeRelations.ts, 283, 5)) +>NumericInference : Symbol(NumericInference, Decl(distributiveConditionalTypeRelations.ts, 208, 1)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 283, 50)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 283, 77)) + +const rejectedArity: SingleParameter<(left: number, right: number) => void> = (left: number, right: number) => {}; // Error +>rejectedArity : Symbol(rejectedArity, Decl(distributiveConditionalTypeRelations.ts, 284, 5)) +>SingleParameter : Symbol(SingleParameter, Decl(distributiveConditionalTypeRelations.ts, 172, 104)) +>left : Symbol(left, Decl(distributiveConditionalTypeRelations.ts, 284, 38)) +>right : Symbol(right, Decl(distributiveConditionalTypeRelations.ts, 284, 51)) +>left : Symbol(left, Decl(distributiveConditionalTypeRelations.ts, 284, 79)) +>right : Symbol(right, Decl(distributiveConditionalTypeRelations.ts, 284, 92)) + +const rejectedOptionalParameter: OptionalParameter<(value: string) => void> = (value: string) => {}; // Error +>rejectedOptionalParameter : Symbol(rejectedOptionalParameter, Decl(distributiveConditionalTypeRelations.ts, 285, 5)) +>OptionalParameter : Symbol(OptionalParameter, Decl(distributiveConditionalTypeRelations.ts, 173, 80)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 285, 52)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 285, 79)) + +const rejectedIntersection: IntersectionPattern<{ value: boolean; method(value: number): unknown }> = { value: true, method: (value: number) => {} }; // Error +>rejectedIntersection : Symbol(rejectedIntersection, Decl(distributiveConditionalTypeRelations.ts, 286, 5)) +>IntersectionPattern : Symbol(IntersectionPattern, Decl(distributiveConditionalTypeRelations.ts, 174, 83)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 286, 49)) +>method : Symbol(method, Decl(distributiveConditionalTypeRelations.ts, 286, 65)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 286, 73)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 286, 103)) +>method : Symbol(method, Decl(distributiveConditionalTypeRelations.ts, 286, 116)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 286, 126)) + +const rejectedPrimitive: PrimitiveBoundary = ""; // Error +>rejectedPrimitive : Symbol(rejectedPrimitive, Decl(distributiveConditionalTypeRelations.ts, 287, 5)) +>PrimitiveBoundary : Symbol(PrimitiveBoundary, Decl(distributiveConditionalTypeRelations.ts, 175, 112)) + +declare const overloadedParameter: { +>overloadedParameter : Symbol(overloadedParameter, Decl(distributiveConditionalTypeRelations.ts, 289, 13)) + + (value: string): void; +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 290, 5)) + + (left: number, right: number): void; +>left : Symbol(left, Decl(distributiveConditionalTypeRelations.ts, 291, 5)) +>right : Symbol(right, Decl(distributiveConditionalTypeRelations.ts, 291, 18)) + +}; +const parameterConstraint: (value: any) => unknown = overloadedParameter; +>parameterConstraint : Symbol(parameterConstraint, Decl(distributiveConditionalTypeRelations.ts, 293, 5)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 293, 28)) +>overloadedParameter : Symbol(overloadedParameter, Decl(distributiveConditionalTypeRelations.ts, 289, 13)) + +const rejectedOverloadedParameter: Inferred = overloadedParameter; // Error +>rejectedOverloadedParameter : Symbol(rejectedOverloadedParameter, Decl(distributiveConditionalTypeRelations.ts, 294, 5)) +>Inferred : Symbol(Inferred, Decl(distributiveConditionalTypeRelations.ts, 107, 1)) +>overloadedParameter : Symbol(overloadedParameter, Decl(distributiveConditionalTypeRelations.ts, 289, 13)) +>overloadedParameter : Symbol(overloadedParameter, Decl(distributiveConditionalTypeRelations.ts, 289, 13)) + +const rejectedOverloadedNonDistributive: InferredNonDistributive = overloadedParameter; // Error +>rejectedOverloadedNonDistributive : Symbol(rejectedOverloadedNonDistributive, Decl(distributiveConditionalTypeRelations.ts, 295, 5)) +>InferredNonDistributive : Symbol(InferredNonDistributive, Decl(distributiveConditionalTypeRelations.ts, 109, 73)) +>overloadedParameter : Symbol(overloadedParameter, Decl(distributiveConditionalTypeRelations.ts, 289, 13)) +>overloadedParameter : Symbol(overloadedParameter, Decl(distributiveConditionalTypeRelations.ts, 289, 13)) + +declare const overloadedReturn: { +>overloadedReturn : Symbol(overloadedReturn, Decl(distributiveConditionalTypeRelations.ts, 297, 13)) + + (): string; + (value: number): number; +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 299, 5)) + +}; +const returnConstraint: () => unknown = overloadedReturn; +>returnConstraint : Symbol(returnConstraint, Decl(distributiveConditionalTypeRelations.ts, 301, 5)) +>overloadedReturn : Symbol(overloadedReturn, Decl(distributiveConditionalTypeRelations.ts, 297, 13)) + +const rejectedOverloadedReturn: InferredReturn = overloadedReturn; // Error +>rejectedOverloadedReturn : Symbol(rejectedOverloadedReturn, Decl(distributiveConditionalTypeRelations.ts, 302, 5)) +>InferredReturn : Symbol(InferredReturn, Decl(distributiveConditionalTypeRelations.ts, 134, 1)) +>overloadedReturn : Symbol(overloadedReturn, Decl(distributiveConditionalTypeRelations.ts, 297, 13)) +>overloadedReturn : Symbol(overloadedReturn, Decl(distributiveConditionalTypeRelations.ts, 297, 13)) + +declare const overloadedMethod: { +>overloadedMethod : Symbol(overloadedMethod, Decl(distributiveConditionalTypeRelations.ts, 304, 13)) + + method(value: string): never; +>method : Symbol(method, Decl(distributiveConditionalTypeRelations.ts, 304, 33), Decl(distributiveConditionalTypeRelations.ts, 305, 33)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 305, 11)) + + method(left: number, right: number): void; +>method : Symbol(method, Decl(distributiveConditionalTypeRelations.ts, 304, 33), Decl(distributiveConditionalTypeRelations.ts, 305, 33)) +>left : Symbol(left, Decl(distributiveConditionalTypeRelations.ts, 306, 11)) +>right : Symbol(right, Decl(distributiveConditionalTypeRelations.ts, 306, 24)) + +}; +const methodConstraint: { method(value: unknown): never } = overloadedMethod; +>methodConstraint : Symbol(methodConstraint, Decl(distributiveConditionalTypeRelations.ts, 308, 5)) +>method : Symbol(method, Decl(distributiveConditionalTypeRelations.ts, 308, 25)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 308, 33)) +>overloadedMethod : Symbol(overloadedMethod, Decl(distributiveConditionalTypeRelations.ts, 304, 13)) + +const rejectedOverloadedMethod: InferredMethod = overloadedMethod; // Error +>rejectedOverloadedMethod : Symbol(rejectedOverloadedMethod, Decl(distributiveConditionalTypeRelations.ts, 309, 5)) +>InferredMethod : Symbol(InferredMethod, Decl(distributiveConditionalTypeRelations.ts, 150, 1)) +>overloadedMethod : Symbol(overloadedMethod, Decl(distributiveConditionalTypeRelations.ts, 304, 13)) +>overloadedMethod : Symbol(overloadedMethod, Decl(distributiveConditionalTypeRelations.ts, 304, 13)) + +declare const neverParameterMethod: { method(value: never): never }; +>neverParameterMethod : Symbol(neverParameterMethod, Decl(distributiveConditionalTypeRelations.ts, 311, 13)) +>method : Symbol(method, Decl(distributiveConditionalTypeRelations.ts, 311, 37)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 311, 45)) + +const neverMethodConstraint: { method(value: unknown): never } = neverParameterMethod; +>neverMethodConstraint : Symbol(neverMethodConstraint, Decl(distributiveConditionalTypeRelations.ts, 312, 5)) +>method : Symbol(method, Decl(distributiveConditionalTypeRelations.ts, 312, 30)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelations.ts, 312, 38)) +>neverParameterMethod : Symbol(neverParameterMethod, Decl(distributiveConditionalTypeRelations.ts, 311, 13)) + +const rejectedMethodReturn: InferredMethodReturn = neverParameterMethod; // Error +>rejectedMethodReturn : Symbol(rejectedMethodReturn, Decl(distributiveConditionalTypeRelations.ts, 313, 5)) +>InferredMethodReturn : Symbol(InferredMethodReturn, Decl(distributiveConditionalTypeRelations.ts, 152, 91)) +>neverParameterMethod : Symbol(neverParameterMethod, Decl(distributiveConditionalTypeRelations.ts, 311, 13)) +>neverParameterMethod : Symbol(neverParameterMethod, Decl(distributiveConditionalTypeRelations.ts, 311, 13)) + diff --git a/tsc/testdata/baselines/reference/compiler/distributiveConditionalTypeRelations.types b/tsc/testdata/baselines/reference/compiler/distributiveConditionalTypeRelations.types new file mode 100644 index 0000000000000..d725cf8474a06 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/distributiveConditionalTypeRelations.types @@ -0,0 +1,961 @@ +//// [tests/cases/compiler/distributiveConditionalTypeRelations.ts] //// + +=== distributiveConditionalTypeRelations.ts === +// The original #27932 cases are in conditionalTypeAssignabilityWhenDeferred.ts. + +type Either = T extends U ? T | { matched: true } : T | { matched: false }; +>Either : Either +>matched : true +>true : true +>matched : false +>false : false + +type Both = T extends U ? T & U : T; +>Both : Both + +type Constant = T extends string ? { a: number } : { b: number }; +>Constant : Constant +>a : number +>b : number + +type Pair = T extends unknown ? { x: T; y: T } : never; +>Pair : Pair +>x : T +>y : T + +type Tuple = T extends unknown ? [T, T] : never; +>Tuple : Tuple + +type Callback = T extends unknown ? (value: T) => T : never; +>Callback : Callback +>value : T + +type Repeated = T extends unknown ? `${T}-${T}` : never; +>Repeated : Repeated + +type Remapped = T extends unknown ? `key-${T}` : never; +>Remapped : Remapped + +type Upper = T extends unknown ? Uppercase : never; +>Upper : Upper + +function identity(value: T, intersection: T & U) { +>identity : (value: T, intersection: T & U) => void +>value : T +>intersection : T & U + + const either: Either = value; +>either : Either +>value : T + + const both: Both = value; +>both : Both +>value : T + + const intersected: Either = intersection; +>intersected : Either +>intersection : T & U + + const extracted: Extract = value; +>extracted : Extract +>value : T + + const excluded: Exclude = value; +>excluded : Exclude +>value : T +} + +function wrappers(value: `key-${T}`, upper: Uppercase, repeated: `${T}-${T}`) { +>wrappers : (value: `key-${T}`, upper: Uppercase, repeated: `${T}-${T}`) => void +>value : `key-${T}` +>upper : Uppercase +>repeated : `${T}-${T}` + + const remapped: Remapped = value; +>remapped : Remapped +>value : `key-${T}` + + const capitalized: Upper = upper; +>capitalized : Upper +>upper : Uppercase + + const correlated: Repeated = repeated; // Error: cross products do not distribute. +>correlated : Repeated +>repeated : `${T}-${T}` +} + +type WithOuter = T extends unknown ? `${T}-${U}` : never; +>WithOuter : WithOuter + +function retainOuterArgument(value: `${T}-${T}`) { +>retainOuterArgument : (value: `${T}-${T}`) => void +>value : `${T}-${T}` + + const result: WithOuter = value; // Error: the uniform-instantiation proof conservatively rejects repeated occurrences. +>result : WithOuter +>value : `${T}-${T}` +} + +type OuterSource = T extends unknown ? `${T}-${U}` : never; +>OuterSource : OuterSource + +type OuterRepeated = T extends unknown ? `outer-${T}-${T}` : never; +>OuterRepeated : OuterRepeated + +type UpperRepeated = T extends unknown ? `${Uppercase}-${Uppercase}` : never; +>UpperRepeated : UpperRepeated + +type InferredRepeated = [T] extends [infer U extends string] ? `${U}-${U}` : never; +>InferredRepeated : InferredRepeated + +function retainConditionalArgument(value: `outer-${OuterSource}`) { +>retainConditionalArgument : (value: `outer-${OuterSource}`) => void +>value : `outer-${OuterSource}` + + const correlated: OuterRepeated = value; // Error: the second argument is not distributed. +>correlated : OuterRepeated +>value : `outer-${OuterSource}` +} + +function cachedOccurrences(value: `${Uppercase}-${Uppercase}`) { +>cachedOccurrences : (value: `${Uppercase}-${Uppercase}`) => void +>value : `${Uppercase}-${Uppercase}` + + const correlated: UpperRepeated = value; // Error: a cached inner type still occurs twice. +>correlated : UpperRepeated +>value : `${Uppercase}-${Uppercase}` +} + +function deferredOccurrences(value: `outer-${InferredRepeated}`) { +>deferredOccurrences : (value: `outer-${InferredRepeated}`) => void +>value : `outer-${InferredRepeated}` + + const correlated: OuterRepeated = value; // Error: deferred inference hides a non-distributive use. +>correlated : OuterRepeated +>value : `outer-${InferredRepeated}` +} + +type Linked = T extends unknown ? T & { value: T } : never; +>Linked : Linked +>value : T + +function retainIntersectionComponent(value: T & { value: T }) { +>retainIntersectionComponent : (value: T & { value: T; }) => void +>value : T & { value: T; } +>value : T + + const correlated: Linked = value; // Error: the property can come from a different constituent. +>correlated : Linked +>value : T & { value: T; } +} + +type ObjectValues = T extends unknown ? T["value"] : never; +>ObjectValues : ObjectValues +>value : unknown + +type IndexedValues = K extends unknown ? T[K] : never; +>IndexedValues : IndexedValues + +function indexedAccess(value: T["value"], indexed: T[K]) { +>indexedAccess : (value: T["value"], indexed: T[K]) => void +>value : unknown +>value : T["value"] +>indexed : T[K] + + const fromObject: ObjectValues = value; +>fromObject : ObjectValues +>value : T["value"] + + const fromIndex: IndexedValues = indexed; +>fromIndex : IndexedValues +>indexed : T[K] +} + +function correlated(pair: { x: T; y: T }, tuple: [T, T], callback: (value: T) => T) { +>correlated : (pair: { x: T; y: T; }, tuple: [T, T], callback: (value: T) => T) => void +>pair : { x: T; y: T; } +>x : T +>y : T +>tuple : [T, T] +>callback : (value: T) => T +>value : T + + const p: Pair = pair; // Error +>p : Pair +>pair : { x: T; y: T; } + + const t: Tuple = tuple; // Error +>t : Tuple +>tuple : [T, T] + + const c: Callback = callback; // Error +>c : Callback +>callback : (value: T) => T +} + +function possiblyNever(value: { a: number; b: number }) { +>possiblyNever : (value: { a: number; b: number; }) => void +>value : { a: number; b: number; } +>a : number +>b : number + + const unconstrained: Constant = value; +>unconstrained : Constant +>value : { a: number; b: number; } + + const constrained: Constant = value; +>constrained : Constant +>value : { a: number; b: number; } + + const conditional: Constant<[T] extends [never] ? object : never> = value; +>conditional : Constant<[T] extends [never] ? object : never> +>value : { a: number; b: number; } +} + +function requiredWitness(value: { a: number; b: number; value: T }) { +>requiredWitness : (value: { a: number; b: number; value: T; }) => void +>value : { a: number; b: number; value: T; } +>a : number +>b : number +>value : T + + const result: Constant = value; +>result : Constant +>value : { a: number; b: number; value: T; } +} + +requiredWitness({ a: 1, b: 2, get value(): never { throw new Error(); } }); +>requiredWitness({ a: 1, b: 2, get value(): never { throw new Error(); } }) : void +>requiredWitness : (value: { a: number; b: number; value: T; }) => void +>{ a: 1, b: 2, get value(): never { throw new Error(); } } : { a: number; b: number; readonly value: never; } +>a : number +>1 : 1 +>b : number +>2 : 2 +>value : never +>new Error() : Error +>Error : ErrorConstructor + +function optionalWitness(value: { a: number; b: number; value?: T }) { +>optionalWitness : (value: { a: number; b: number; value?: T; }) => void +>value : { a: number; b: number; value?: T; } +>a : number +>b : number +>value : T | undefined + + const result: Constant = value; +>result : Constant +>value : { a: number; b: number; value?: T; } +} + +function arrayWitness(value: { a: number; b: number; values: T[] }) { +>arrayWitness : (value: { a: number; b: number; values: T[]; }) => void +>value : { a: number; b: number; values: T[]; } +>a : number +>b : number +>values : T[] + + const result: Constant = value; +>result : Constant +>value : { a: number; b: number; values: T[]; } +} + +function functionWitness(value: { a: number; b: number; get: () => T }) { +>functionWitness : (value: { a: number; b: number; get: () => T; }) => void +>value : { a: number; b: number; get: () => T; } +>a : number +>b : number +>get : () => T + + const result: Constant = value; +>result : Constant +>value : { a: number; b: number; get: () => T; } +} + +function definitelyNotNever(value: { a: number; b: number }) { +>definitelyNotNever : (value: { a: number; b: number; }) => void +>value : { a: number; b: number; } +>a : number +>b : number + + const object: Constant<{ value: T }> = value; +>object : { b: number; } +>value : T +>value : { a: number; b: number; } + + const array: Constant = value; +>array : { b: number; } +>value : { a: number; b: number; } + + const tuple: Constant<[T]> = value; +>tuple : { b: number; } +>value : { a: number; b: number; } + + const union: Constant = value; +>union : { b: number; } | Constant +>value : { a: number; b: number; } + + const conditional: Constant<[T] extends [string] ? { value: T } : { other: T }> = value; +>conditional : Constant<[T] extends [string] ? { value: T; } : { other: T; }> +>value : T +>other : T +>value : { a: number; b: number; } +} + +type Homomorphic = { [K in keyof T]: T[K] }; +>Homomorphic : Homomorphic + +type Spread = [...T]; +>Spread : [...T] + +function emptyInstantiations(value: { a: number; b: number }) { +>emptyInstantiations : (value: { a: number; b: number; }) => void +>value : { a: number; b: number; } +>a : number +>b : number + + const mapped: Constant> = value; +>mapped : Constant> +>value : { a: number; b: number; } + + const variadic: Constant> = value; +>variadic : Constant<[...U]> +>value : { a: number; b: number; } +} + +type Inferred = T extends (value: infer P) => unknown ? T | P : never; +>Inferred : Inferred +>value : P + +type InferredNonDistributive = [T] extends [(value: infer P) => unknown] ? T | P : never; +>InferredNonDistributive : InferredNonDistributive +>value : P + +type InferredValue = T extends { value: infer P } ? P : never; +>InferredValue : InferredValue +>value : P + +type InferredCallback = T extends { value: infer P } ? (value: P) => P : never; +>InferredCallback : InferredCallback +>value : P +>value : P + +type ConstrainedInfer = T extends { value: infer P extends string } ? T : never; +>ConstrainedInfer : ConstrainedInfer +>value : P + +function inference unknown>(value: T) { +>inference : unknown>(value: T) => void +>value : any +>value : T + + const distributive: Inferred = value; // Error: additional overloads can change the inferred parameter. +>distributive : Inferred +>value : T + + const nonDistributive: InferredNonDistributive = value; // Error +>nonDistributive : InferredNonDistributive +>value : T +} + +function independentInference(value: T) { +>independentInference : (value: T) => void +>value : T + + const result: T extends infer P ? T | P : never = value; +>result : T extends infer P ? P | T : never +>value : T +} + +function propertyInference(value: T) { +>propertyInference : (value: T) => void +>value : unknown +>value : T + + const result: T extends { value: infer P } ? T | P : never = value; +>result : T extends { value: infer P; } ? P | T : never +>value : P +>value : T +} + +function tupleInference(value: T) { +>tupleInference : (value: T) => void +>value : T + + const result: [T] extends [infer P] ? T | P : never = value; +>result : [T] extends [infer P] ? P | T : never +>value : T +} + +function independentParameters unknown>(value: T) { +>independentParameters : unknown>(value: T) => void +>value : any +>value : T + + const result: T extends (value: infer P) => infer R ? T | P | R : never = value; // Error +>result : T extends (value: infer P) => infer R ? P | R | T : never +>value : P +>value : T +} + +type InferredReturn = T extends () => infer R ? T | R : never; +>InferredReturn : InferredReturn + +function returnInference unknown>(value: T) { +>returnInference : unknown>(value: T) => void +>value : T + + const result: InferredReturn = value; // Error: inference can use an overload with incompatible arity. +>result : InferredReturn +>value : T +} + +function universalSignature never>(value: T) { +>universalSignature : never>(value: T) => void +>value : unknown +>value : T + + const parameter: Inferred = value; +>parameter : Inferred +>value : T + + const both: T extends (value: infer P) => infer R ? T | P | R : never = value; +>both : T extends (value: infer P) => infer R ? P | R | T : never +>value : P +>value : T +} + +function ignoredParameter never>(value: T) { +>ignoredParameter : never>(value: T) => void +>value : T + + const result: Inferred = value; +>result : Inferred +>value : T + + const returned: InferredReturn = value; +>returned : InferredReturn +>value : T +} + +type InferredMethod = T extends { method: (value: infer P) => unknown } ? T | P : never; +>InferredMethod : InferredMethod +>method : (value: infer P) => unknown +>value : P + +type InferredMethodReturn = T extends { method: (value: any) => infer R } ? T | R : never; +>InferredMethodReturn : InferredMethodReturn +>method : (value: any) => infer R +>value : any + +function bivariantInference(value: T) { +>bivariantInference : (value: T) => void +>method : (value: unknown) => never +>value : unknown +>value : T + + const result: InferredMethod = value; // Error: the constraint permits narrower method parameters. +>result : InferredMethod +>value : T + + const returned: InferredMethodReturn = value; // Error: a never parameter fails the strict function pattern. +>returned : InferredMethodReturn +>value : T +} + +function dependentInference(value: string, callback: (value: string) => string) { +>dependentInference : (value: string, callback: (value: string) => string) => void +>value : string +>value : string +>callback : (value: string) => string +>value : string + + const inferred: InferredValue = value; // Error: T can contain a narrower value. +>inferred : InferredValue +>value : string + + const inferredCallback: InferredCallback = callback; // Error +>inferredCallback : InferredCallback +>callback : (value: string) => string +} + +function constrainedInference(value: T) { +>constrainedInference : (value: T) => void +>value : any +>value : T + + const result: ConstrainedInfer = value; // Error: any in the constraint does not imply string. +>result : ConstrainedInfer +>value : T +} + +type SpecificParameter = T extends (value: string) => infer R ? T | R : never; +>SpecificParameter : SpecificParameter +>value : string + +type SpecificMethod = T extends { method(value: string): infer R } ? T | R : never; +>SpecificMethod : SpecificMethod +>method : (value: string) => infer R +>value : string + +type OptionalProperty = T extends { optional?: string; value: infer R } ? T | R : never; +>OptionalProperty : OptionalProperty +>optional : string | undefined +>value : R + +type MixedInference = T extends { input: (value: infer R) => void; output: infer R } ? T | R : never; +>MixedInference : MixedInference +>input : (value: infer R) => void +>value : R +>output : R + +type SingleParameter = T extends (value: infer R) => unknown ? T | R : never; +>SingleParameter : SingleParameter +>value : R + +type OptionalParameter = T extends (value?: infer R) => unknown ? T | R : never; +>OptionalParameter : OptionalParameter +>value : R | undefined + +type IntersectionPattern = T extends { value: infer R } & { method(value: string): unknown } ? T | R : never; +>IntersectionPattern : IntersectionPattern +>value : R +>method : (value: string) => unknown +>value : string + +type PrimitiveBoundary = T extends object | (infer R extends number) ? T | R : never; +>PrimitiveBoundary : PrimitiveBoundary + +function fixedParameter unknown>(value: T) { +>fixedParameter : unknown>(value: T) => void +>value : any +>value : T + + const result: SpecificParameter = value; // Error: a number parameter also satisfies the constraint. +>result : SpecificParameter +>value : T + + const withoutInference: T extends (value: string) => unknown ? T : never = value; // Error +>withoutInference : T extends (value: string) => unknown ? T : never +>value : string +>value : T +} + +function bivariantParameter(value: T) { +>bivariantParameter : (value: T) => void +>method : (value: unknown) => unknown +>value : unknown +>value : T + + const result: SpecificMethod = value; // Error +>result : SpecificMethod +>value : T +} + +function optionalProperty(value: T) { +>optionalProperty : (value: T) => void +>value : unknown +>value : T + + const result: OptionalProperty = value; // Error: subtypes can add an incompatible optional property. +>result : OptionalProperty +>value : T +} + +function mixedInference void; output: any }>(value: T) { +>mixedInference : void; output: any; }>(value: T) => void +>input : (value: any) => void +>value : any +>output : any +>value : T + + const result: MixedInference = value; // Error: input and output can require incompatible inferences. +>result : MixedInference +>value : T +} + +interface InvariantInferencePattern { + input: (value: T) => void; +>input : (value: T) => void +>value : T + + output: T; +>output : T +} + +type InvariantInference = T extends InvariantInferencePattern ? T | R : never; +>InvariantInference : InvariantInference + +function invariantInference>(value: T) { +>invariantInference : >(value: T) => void +>value : T + + const result: InvariantInference = value; // Error: invariant type-argument inference must not hide mixed positions. +>result : InvariantInference +>value : T +} + +enum NumericInferencePattern { +>NumericInferencePattern : NumericInferencePattern + + value +>value : NumericInferencePattern.value +} + +type NumericInference = T extends (value: infer P) => NumericInferencePattern ? T | P : never; +>NumericInference : NumericInference +>value : P + +function numericInference number>(value: T) { +>numericInference : number>(value: T) => void +>value : unknown +>value : T + + const result: NumericInference = value; // Error: number-to-enum compatibility does not cover every numeric literal. +>result : NumericInference +>value : T +} + +function unboundedArity unknown>(value: T) { +>unboundedArity : unknown>(value: T) => void +>args : any[] +>value : T + + const result: SingleParameter = value; // Error: subtypes can require multiple arguments. +>result : SingleParameter +>value : T +} + +function optionalParameter unknown>(value: T) { +>optionalParameter : unknown>(value: T) => void +>value : any +>value : T + + const result: OptionalParameter = value; // Error: subtypes can require an argument. +>result : OptionalParameter +>value : T +} + +function intersectionPattern(value: T) { +>intersectionPattern : (value: T) => void +>value : any +>method : (value: any) => unknown +>value : any +>value : T + + const result: IntersectionPattern = value; // Error +>result : IntersectionPattern +>value : T +} + +function primitiveBoundary(value: T) { +>primitiveBoundary : (value: T) => void +>toString : () => string +>value : T + + const result: PrimitiveBoundary = value; // Error: structural object constraints admit primitives. +>result : PrimitiveBoundary +>value : T +} + +class Nominal { +>Nominal : Nominal + + private brand!: void; +>brand : void + + value!: T; +>value : T +} + +function nominalInference(value: T) { +>nominalInference : (value: T) => void +>value : unknown +>value : T + + const result: T extends Nominal ? T | P : never = value; // Error +>result : T extends Nominal ? P | T : never +>value : T +} + +type FilteredKeys = T extends U ? never : T; +>FilteredKeys : FilteredKeys + +type DecoratedKeys = T extends U ? never : `key-${T}`; +>DecoratedKeys : DecoratedKeys + +function filtering(value: FilteredKeys) { +>filtering : (value: FilteredKeys) => void +>value : FilteredKeys + + const excluded: Exclude = value; +>excluded : Exclude +>value : FilteredKeys + + const remapped: DecoratedKeys, never> = `key-${value}`; +>remapped : DecoratedKeys, never> +>`key-${value}` : `key-${FilteredKeys}` +>value : FilteredKeys +} + +function filteredRemapping(value: `key-${Exclude}`) { +>filteredRemapping : (value: `key-${Exclude}`) => void +>value : `key-${Exclude}` + + const result: DecoratedKeys = value; +>result : DecoratedKeys +>value : `key-${Exclude}` +} + +type FilteredMap = { [K in T as K extends U ? never : K]: unknown }; +>FilteredMap : FilteredMap + +type DecoratedMap = { [K in T as K extends U ? never : `key-${K}`]: unknown }; +>DecoratedMap : DecoratedMap + +function mappedKeys(key: Exclude, decorated: `key-${Exclude}`) { +>mappedKeys : (key: Exclude, decorated: `key-${Exclude}`) => void +>key : Exclude +>decorated : `key-${Exclude}` + + const filtered: keyof FilteredMap = key; +>filtered : keyof FilteredMap +>key : Exclude + + const remapped: keyof DecoratedMap = decorated; +>remapped : keyof DecoratedMap +>decorated : `key-${Exclude}` +} + +type Recursive = T extends object ? T | { next: Recursive } : T; +>Recursive : Recursive +>next : Recursive + +function recursion(value: T) { +>recursion : (value: T) => void +>value : T + + const result: Recursive = value; +>result : Recursive +>value : T +} + +function subtypeIsNotConstituent(value: T & string) { +>subtypeIsNotConstituent : (value: T & string) => void +>value : T & string + + const result: Extract = value; // Error: Extract is never. +>result : Extract +>value : T & string +} + +// Concrete instantiations witnessing the rejected assignments. +const mixedPair: Pair<"a" | "b"> = { x: "a", y: "b" }; // Error +>mixedPair : { x: "a"; y: "a"; } | { x: "b"; y: "b"; } +>{ x: "a", y: "b" } : { x: "a"; y: "b"; } +>x : "a" +>"a" : "a" +>y : "b" +>"b" : "b" + +const mixedTuple: Tuple<"a" | "b"> = ["a", "b"]; // Error +>mixedTuple : ["a", "a"] | ["b", "b"] +>["a", "b"] : ["a", "b"] +>"a" : "a" +>"b" : "b" + +const mixedString: Repeated<"a" | "b"> = "a-b"; // Error +>mixedString : "a-a" | "b-b" +>"a-b" : "a-b" + +const emptyConstant: Constant = { a: 1, b: 2 }; // Error +>emptyConstant : never +>{ a: 1, b: 2 } : { a: number; b: number; } +>a : number +>1 : 1 +>b : number +>2 : 2 + +const narrowInference: InferredValue<{ value: "a" }> = "b"; // Error +>narrowInference : "a" +>value : "a" +>"b" : "b" + +const rejectedConstrainedInfer: ConstrainedInfer<{ value: number }> = { value: 1 }; // Error +>rejectedConstrainedInfer : never +>value : number +>{ value: 1 } : { value: number; } +>value : number +>1 : 1 + +const rejectedFixedParameter: SpecificParameter<(value: number) => boolean> = (value: number) => true; // Error +>rejectedFixedParameter : never +>value : number +>(value: number) => true : (value: number) => boolean +>value : number +>true : true + +const rejectedMethod: SpecificMethod<{ method(value: number): boolean }> = { method: (value: number) => true }; // Error +>rejectedMethod : never +>method : (value: number) => boolean +>value : number +>{ method: (value: number) => true } : { method: (value: number) => boolean; } +>method : (value: number) => boolean +>(value: number) => true : (value: number) => boolean +>value : number +>true : true + +const rejectedOptionalProperty: OptionalProperty<{ optional: number; value: boolean }> = { optional: 1, value: true }; // Error +>rejectedOptionalProperty : never +>optional : number +>value : boolean +>{ optional: 1, value: true } : { optional: number; value: boolean; } +>optional : number +>1 : 1 +>value : boolean +>true : true + +const rejectedMixedInference: MixedInference<{ input(value: number): void; output: string }> = { input: (value: number) => {}, output: "" }; // Error +>rejectedMixedInference : never +>input : (value: number) => void +>value : number +>output : string +>{ input: (value: number) => {}, output: "" } : { input: (value: number) => void; output: string; } +>input : (value: number) => void +>(value: number) => {} : (value: number) => void +>value : number +>output : string +>"" : "" + +const rejectedInvariantInference: InvariantInference<{ input: (value: number) => void; output: string }> = { input: (value: number) => {}, output: "" }; // Error +>rejectedInvariantInference : never +>input : (value: number) => void +>value : number +>output : string +>{ input: (value: number) => {}, output: "" } : { input: (value: number) => void; output: string; } +>input : (value: number) => void +>(value: number) => {} : (value: number) => void +>value : number +>output : string +>"" : "" + +const rejectedNumericInference: NumericInference<(value: unknown) => 123> = (value: unknown) => 123; // Error +>rejectedNumericInference : never +>value : unknown +>(value: unknown) => 123 : (value: unknown) => number +>value : unknown +>123 : 123 + +const rejectedArity: SingleParameter<(left: number, right: number) => void> = (left: number, right: number) => {}; // Error +>rejectedArity : never +>left : number +>right : number +>(left: number, right: number) => {} : (left: number, right: number) => void +>left : number +>right : number + +const rejectedOptionalParameter: OptionalParameter<(value: string) => void> = (value: string) => {}; // Error +>rejectedOptionalParameter : never +>value : string +>(value: string) => {} : (value: string) => void +>value : string + +const rejectedIntersection: IntersectionPattern<{ value: boolean; method(value: number): unknown }> = { value: true, method: (value: number) => {} }; // Error +>rejectedIntersection : never +>value : boolean +>method : (value: number) => unknown +>value : number +>{ value: true, method: (value: number) => {} } : { value: boolean; method: (value: number) => void; } +>value : boolean +>true : true +>method : (value: number) => void +>(value: number) => {} : (value: number) => void +>value : number + +const rejectedPrimitive: PrimitiveBoundary = ""; // Error +>rejectedPrimitive : never +>"" : "" + +declare const overloadedParameter: { +>overloadedParameter : { (value: string): void; (left: number, right: number): void; } + + (value: string): void; +>value : string + + (left: number, right: number): void; +>left : number +>right : number + +}; +const parameterConstraint: (value: any) => unknown = overloadedParameter; +>parameterConstraint : (value: any) => unknown +>value : any +>overloadedParameter : { (value: string): void; (left: number, right: number): void; } + +const rejectedOverloadedParameter: Inferred = overloadedParameter; // Error +>rejectedOverloadedParameter : never +>overloadedParameter : { (value: string): void; (left: number, right: number): void; } +>overloadedParameter : { (value: string): void; (left: number, right: number): void; } + +const rejectedOverloadedNonDistributive: InferredNonDistributive = overloadedParameter; // Error +>rejectedOverloadedNonDistributive : never +>overloadedParameter : { (value: string): void; (left: number, right: number): void; } +>overloadedParameter : { (value: string): void; (left: number, right: number): void; } + +declare const overloadedReturn: { +>overloadedReturn : { (): string; (value: number): number; } + + (): string; + (value: number): number; +>value : number + +}; +const returnConstraint: () => unknown = overloadedReturn; +>returnConstraint : () => unknown +>overloadedReturn : { (): string; (value: number): number; } + +const rejectedOverloadedReturn: InferredReturn = overloadedReturn; // Error +>rejectedOverloadedReturn : never +>overloadedReturn : { (): string; (value: number): number; } +>overloadedReturn : { (): string; (value: number): number; } + +declare const overloadedMethod: { +>overloadedMethod : { method(value: string): never; method(left: number, right: number): void; } + + method(value: string): never; +>method : { (value: string): never; (left: number, right: number): void; } +>value : string + + method(left: number, right: number): void; +>method : { (value: string): never; (left: number, right: number): void; } +>left : number +>right : number + +}; +const methodConstraint: { method(value: unknown): never } = overloadedMethod; +>methodConstraint : { method(value: unknown): never; } +>method : (value: unknown) => never +>value : unknown +>overloadedMethod : { method(value: string): never; method(left: number, right: number): void; } + +const rejectedOverloadedMethod: InferredMethod = overloadedMethod; // Error +>rejectedOverloadedMethod : never +>overloadedMethod : { method(value: string): never; method(left: number, right: number): void; } +>overloadedMethod : { method(value: string): never; method(left: number, right: number): void; } + +declare const neverParameterMethod: { method(value: never): never }; +>neverParameterMethod : { method(value: never): never; } +>method : (value: never) => never +>value : never + +const neverMethodConstraint: { method(value: unknown): never } = neverParameterMethod; +>neverMethodConstraint : { method(value: unknown): never; } +>method : (value: unknown) => never +>value : unknown +>neverParameterMethod : { method(value: never): never; } + +const rejectedMethodReturn: InferredMethodReturn = neverParameterMethod; // Error +>rejectedMethodReturn : never +>neverParameterMethod : { method(value: never): never; } +>neverParameterMethod : { method(value: never): never; } + diff --git a/tsc/testdata/baselines/reference/compiler/distributiveConditionalTypeRelationsUserTests.symbols b/tsc/testdata/baselines/reference/compiler/distributiveConditionalTypeRelationsUserTests.symbols new file mode 100644 index 0000000000000..6480c87322d0e --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/distributiveConditionalTypeRelationsUserTests.symbols @@ -0,0 +1,333 @@ +//// [tests/cases/compiler/distributiveConditionalTypeRelationsUserTests.ts] //// + +=== distributiveConditionalTypeRelationsUserTests.ts === +namespace Inquirer { +>Inquirer : Symbol(Inquirer, Decl(distributiveConditionalTypeRelationsUserTests.ts, 0, 0)) + + type Answer = R extends true ? number : number | undefined; +>Answer : Symbol(Answer, Decl(distributiveConditionalTypeRelationsUserTests.ts, 0, 20)) +>R : Symbol(R, Decl(distributiveConditionalTypeRelationsUserTests.ts, 1, 16)) +>R : Symbol(R, Decl(distributiveConditionalTypeRelationsUserTests.ts, 1, 16)) + + export function answer(validate: (value: Answer) => boolean, value: number) { +>answer : Symbol(answer, Decl(distributiveConditionalTypeRelationsUserTests.ts, 1, 82)) +>R : Symbol(R, Decl(distributiveConditionalTypeRelationsUserTests.ts, 3, 27)) +>validate : Symbol(validate, Decl(distributiveConditionalTypeRelationsUserTests.ts, 3, 46)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelationsUserTests.ts, 3, 57)) +>Answer : Symbol(Answer, Decl(distributiveConditionalTypeRelationsUserTests.ts, 0, 20)) +>R : Symbol(R, Decl(distributiveConditionalTypeRelationsUserTests.ts, 3, 27)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelationsUserTests.ts, 3, 86)) + + return validate(value); +>validate : Symbol(validate, Decl(distributiveConditionalTypeRelationsUserTests.ts, 3, 46)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelationsUserTests.ts, 3, 86)) + } +} + +namespace VSCode { +>VSCode : Symbol(VSCode, Decl(distributiveConditionalTypeRelationsUserTests.ts, 6, 1)) + + interface Separator { +>Separator : Symbol(Separator, Decl(distributiveConditionalTypeRelationsUserTests.ts, 8, 18)) + + separator: true; +>separator : Symbol(Separator.separator, Decl(distributiveConditionalTypeRelationsUserTests.ts, 9, 25)) + } + + export class QuickPick { +>QuickPick : Symbol(QuickPick, Decl(distributiveConditionalTypeRelationsUserTests.ts, 11, 5)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelationsUserTests.ts, 13, 27)) +>O : Symbol(O, Decl(distributiveConditionalTypeRelationsUserTests.ts, 13, 29)) +>useSeparators : Symbol(useSeparators, Decl(distributiveConditionalTypeRelationsUserTests.ts, 13, 41)) + + private items: O extends { useSeparators: true } ? (T | Separator)[] : T[] = []; +>items : Symbol(QuickPick.items, Decl(distributiveConditionalTypeRelationsUserTests.ts, 13, 69)) +>O : Symbol(O, Decl(distributiveConditionalTypeRelationsUserTests.ts, 13, 29)) +>useSeparators : Symbol(useSeparators, Decl(distributiveConditionalTypeRelationsUserTests.ts, 14, 34)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelationsUserTests.ts, 13, 27)) +>Separator : Symbol(Separator, Decl(distributiveConditionalTypeRelationsUserTests.ts, 8, 18)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelationsUserTests.ts, 13, 27)) + } +} + +namespace Fabric { +>Fabric : Symbol(Fabric, Decl(distributiveConditionalTypeRelationsUserTests.ts, 16, 1)) + + type Easing = T extends number[] +>Easing : Symbol(Easing, Decl(distributiveConditionalTypeRelationsUserTests.ts, 18, 18)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelationsUserTests.ts, 19, 16)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelationsUserTests.ts, 19, 16)) + + ? (time: number, start: number, delta: number, duration: number, index: number) => number +>time : Symbol(time, Decl(distributiveConditionalTypeRelationsUserTests.ts, 20, 11)) +>start : Symbol(start, Decl(distributiveConditionalTypeRelationsUserTests.ts, 20, 24)) +>delta : Symbol(delta, Decl(distributiveConditionalTypeRelationsUserTests.ts, 20, 39)) +>duration : Symbol(duration, Decl(distributiveConditionalTypeRelationsUserTests.ts, 20, 54)) +>index : Symbol(index, Decl(distributiveConditionalTypeRelationsUserTests.ts, 20, 72)) + + : (time: number, start: number, delta: number, duration: number) => number; +>time : Symbol(time, Decl(distributiveConditionalTypeRelationsUserTests.ts, 21, 11)) +>start : Symbol(start, Decl(distributiveConditionalTypeRelationsUserTests.ts, 21, 24)) +>delta : Symbol(delta, Decl(distributiveConditionalTypeRelationsUserTests.ts, 21, 39)) +>duration : Symbol(duration, Decl(distributiveConditionalTypeRelationsUserTests.ts, 21, 54)) + + const defaultEasing = (time: number, start: number, delta: number, duration: number) => start; +>defaultEasing : Symbol(defaultEasing, Decl(distributiveConditionalTypeRelationsUserTests.ts, 23, 9)) +>time : Symbol(time, Decl(distributiveConditionalTypeRelationsUserTests.ts, 23, 27)) +>start : Symbol(start, Decl(distributiveConditionalTypeRelationsUserTests.ts, 23, 40)) +>delta : Symbol(delta, Decl(distributiveConditionalTypeRelationsUserTests.ts, 23, 55)) +>duration : Symbol(duration, Decl(distributiveConditionalTypeRelationsUserTests.ts, 23, 70)) +>start : Symbol(start, Decl(distributiveConditionalTypeRelationsUserTests.ts, 23, 40)) + + export function easing(): Easing { +>easing : Symbol(easing, Decl(distributiveConditionalTypeRelationsUserTests.ts, 23, 98)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelationsUserTests.ts, 25, 27)) +>Easing : Symbol(Easing, Decl(distributiveConditionalTypeRelationsUserTests.ts, 18, 18)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelationsUserTests.ts, 25, 27)) + + return defaultEasing; +>defaultEasing : Symbol(defaultEasing, Decl(distributiveConditionalTypeRelationsUserTests.ts, 23, 9)) + } +} + +namespace Supabase { +>Supabase : Symbol(Supabase, Decl(distributiveConditionalTypeRelationsUserTests.ts, 28, 1)) + + interface Row { +>Row : Symbol(Row, Decl(distributiveConditionalTypeRelationsUserTests.ts, 30, 20)) + + id: number; +>id : Symbol(Row.id, Decl(distributiveConditionalTypeRelationsUserTests.ts, 31, 19)) + + columns?: string[]; +>columns : Symbol(Row.columns, Decl(distributiveConditionalTypeRelationsUserTests.ts, 32, 19)) + } + + type Selected = T extends true ? Row : Omit; +>Selected : Symbol(Selected, Decl(distributiveConditionalTypeRelationsUserTests.ts, 34, 5)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelationsUserTests.ts, 36, 18)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelationsUserTests.ts, 36, 18)) +>Row : Symbol(Row, Decl(distributiveConditionalTypeRelationsUserTests.ts, 30, 20)) +>Omit : Symbol(Omit, Decl(lib.es5.d.ts, --, --)) +>Row : Symbol(Row, Decl(distributiveConditionalTypeRelationsUserTests.ts, 30, 20)) + + export function rows(value: Row[]): Selected[] { +>rows : Symbol(rows, Decl(distributiveConditionalTypeRelationsUserTests.ts, 36, 95)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelationsUserTests.ts, 38, 25)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelationsUserTests.ts, 38, 56)) +>Row : Symbol(Row, Decl(distributiveConditionalTypeRelationsUserTests.ts, 30, 20)) +>Selected : Symbol(Selected, Decl(distributiveConditionalTypeRelationsUserTests.ts, 34, 5)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelationsUserTests.ts, 38, 25)) + + return value; +>value : Symbol(value, Decl(distributiveConditionalTypeRelationsUserTests.ts, 38, 56)) + } +} + +namespace Lexical { +>Lexical : Symbol(Lexical, Decl(distributiveConditionalTypeRelationsUserTests.ts, 41, 1)) + + class Slot { +>Slot : Symbol(Slot, Decl(distributiveConditionalTypeRelationsUserTests.ts, 43, 19)) + + value = 0; +>value : Symbol(Slot.value, Decl(distributiveConditionalTypeRelationsUserTests.ts, 44, 16)) + } + + class ElementSlot extends Slot { +>ElementSlot : Symbol(ElementSlot, Decl(distributiveConditionalTypeRelationsUserTests.ts, 46, 5)) +>Slot : Symbol(Slot, Decl(distributiveConditionalTypeRelationsUserTests.ts, 43, 19)) + + element = true; +>element : Symbol(ElementSlot.element, Decl(distributiveConditionalTypeRelationsUserTests.ts, 48, 36)) + + withAfter(): ElementSlot { +>withAfter : Symbol(ElementSlot.withAfter, Decl(distributiveConditionalTypeRelationsUserTests.ts, 49, 23)) +>ElementSlot : Symbol(ElementSlot, Decl(distributiveConditionalTypeRelationsUserTests.ts, 46, 5)) + + return this; +>this : Symbol(ElementSlot, Decl(distributiveConditionalTypeRelationsUserTests.ts, 46, 5)) + } + } + + class BaseNode { +>BaseNode : Symbol(BaseNode, Decl(distributiveConditionalTypeRelationsUserTests.ts, 53, 5)) + + node = true; +>node : Symbol(BaseNode.node, Decl(distributiveConditionalTypeRelationsUserTests.ts, 55, 20)) + } + + type SlotForNode = N extends ElementNode ? ElementSlot : Slot; +>SlotForNode : Symbol(SlotForNode, Decl(distributiveConditionalTypeRelationsUserTests.ts, 57, 5)) +>N : Symbol(N, Decl(distributiveConditionalTypeRelationsUserTests.ts, 59, 21)) +>BaseNode : Symbol(BaseNode, Decl(distributiveConditionalTypeRelationsUserTests.ts, 53, 5)) +>N : Symbol(N, Decl(distributiveConditionalTypeRelationsUserTests.ts, 59, 21)) +>ElementNode : Symbol(ElementNode, Decl(distributiveConditionalTypeRelationsUserTests.ts, 60, 74)) +>ElementSlot : Symbol(ElementSlot, Decl(distributiveConditionalTypeRelationsUserTests.ts, 46, 5)) +>Slot : Symbol(Slot, Decl(distributiveConditionalTypeRelationsUserTests.ts, 43, 19)) + + declare function getSlot(node: N): SlotForNode; +>getSlot : Symbol(getSlot, Decl(distributiveConditionalTypeRelationsUserTests.ts, 59, 86)) +>N : Symbol(N, Decl(distributiveConditionalTypeRelationsUserTests.ts, 60, 29)) +>BaseNode : Symbol(BaseNode, Decl(distributiveConditionalTypeRelationsUserTests.ts, 53, 5)) +>node : Symbol(node, Decl(distributiveConditionalTypeRelationsUserTests.ts, 60, 49)) +>N : Symbol(N, Decl(distributiveConditionalTypeRelationsUserTests.ts, 60, 29)) +>SlotForNode : Symbol(SlotForNode, Decl(distributiveConditionalTypeRelationsUserTests.ts, 57, 5)) +>N : Symbol(N, Decl(distributiveConditionalTypeRelationsUserTests.ts, 60, 29)) + + export class ElementNode extends BaseNode { +>ElementNode : Symbol(ElementNode, Decl(distributiveConditionalTypeRelationsUserTests.ts, 60, 74)) +>BaseNode : Symbol(BaseNode, Decl(distributiveConditionalTypeRelationsUserTests.ts, 53, 5)) + + element = true; +>element : Symbol(ElementNode.element, Decl(distributiveConditionalTypeRelationsUserTests.ts, 62, 47)) + + refresh() { +>refresh : Symbol(ElementNode.refresh, Decl(distributiveConditionalTypeRelationsUserTests.ts, 63, 23)) + + let slot = getSlot(this); +>slot : Symbol(slot, Decl(distributiveConditionalTypeRelationsUserTests.ts, 65, 15)) +>getSlot : Symbol(getSlot, Decl(distributiveConditionalTypeRelationsUserTests.ts, 59, 86)) +>this : Symbol(ElementNode, Decl(distributiveConditionalTypeRelationsUserTests.ts, 60, 74)) + + slot = slot.withAfter(); +>slot : Symbol(slot, Decl(distributiveConditionalTypeRelationsUserTests.ts, 65, 15)) +>slot.withAfter : Symbol(ElementSlot.withAfter, Decl(distributiveConditionalTypeRelationsUserTests.ts, 49, 23)) +>slot : Symbol(slot, Decl(distributiveConditionalTypeRelationsUserTests.ts, 65, 15)) +>withAfter : Symbol(ElementSlot.withAfter, Decl(distributiveConditionalTypeRelationsUserTests.ts, 49, 23)) + } + } +} + +namespace LobeHub { +>LobeHub : Symbol(LobeHub, Decl(distributiveConditionalTypeRelationsUserTests.ts, 69, 1)) + + type RequestResult = Throw extends true ? Promise : Promise; +>RequestResult : Symbol(RequestResult, Decl(distributiveConditionalTypeRelationsUserTests.ts, 71, 19)) +>Data : Symbol(Data, Decl(distributiveConditionalTypeRelationsUserTests.ts, 72, 23)) +>Throw : Symbol(Throw, Decl(distributiveConditionalTypeRelationsUserTests.ts, 72, 28)) +>Throw : Symbol(Throw, Decl(distributiveConditionalTypeRelationsUserTests.ts, 72, 28)) +>Promise : Symbol(Promise, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2018.promise.d.ts, --, --)) +>Data : Symbol(Data, Decl(distributiveConditionalTypeRelationsUserTests.ts, 72, 23)) +>Promise : Symbol(Promise, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2018.promise.d.ts, --, --)) +>Data : Symbol(Data, Decl(distributiveConditionalTypeRelationsUserTests.ts, 72, 23)) + + export const request: ( +>request : Symbol(request, Decl(distributiveConditionalTypeRelationsUserTests.ts, 74, 16)) +>Data : Symbol(Data, Decl(distributiveConditionalTypeRelationsUserTests.ts, 74, 27)) +>Throw : Symbol(Throw, Decl(distributiveConditionalTypeRelationsUserTests.ts, 74, 32)) + + data: Data, +>data : Symbol(data, Decl(distributiveConditionalTypeRelationsUserTests.ts, 74, 56)) +>Data : Symbol(Data, Decl(distributiveConditionalTypeRelationsUserTests.ts, 74, 27)) + + flag: Throw, +>flag : Symbol(flag, Decl(distributiveConditionalTypeRelationsUserTests.ts, 75, 19)) +>Throw : Symbol(Throw, Decl(distributiveConditionalTypeRelationsUserTests.ts, 74, 32)) + + ) => RequestResult = async (data, flag) => data; +>RequestResult : Symbol(RequestResult, Decl(distributiveConditionalTypeRelationsUserTests.ts, 71, 19)) +>Data : Symbol(Data, Decl(distributiveConditionalTypeRelationsUserTests.ts, 74, 27)) +>Throw : Symbol(Throw, Decl(distributiveConditionalTypeRelationsUserTests.ts, 74, 32)) +>data : Symbol(data, Decl(distributiveConditionalTypeRelationsUserTests.ts, 77, 45)) +>flag : Symbol(flag, Decl(distributiveConditionalTypeRelationsUserTests.ts, 77, 50)) +>data : Symbol(data, Decl(distributiveConditionalTypeRelationsUserTests.ts, 77, 45)) +} + +namespace Effect { +>Effect : Symbol(Effect, Decl(distributiveConditionalTypeRelationsUserTests.ts, 78, 1)) + + interface Box { +>Box : Symbol(Box, Decl(distributiveConditionalTypeRelationsUserTests.ts, 80, 18)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelationsUserTests.ts, 81, 18)) + + value: T; +>value : Symbol(Box.value, Decl(distributiveConditionalTypeRelationsUserTests.ts, 81, 22)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelationsUserTests.ts, 81, 18)) + } + + type With, T> = S extends readonly [any, ...any[]] ? [T, ...T[]] : T[]; +>With : Symbol(With, Decl(distributiveConditionalTypeRelationsUserTests.ts, 83, 5)) +>S : Symbol(S, Decl(distributiveConditionalTypeRelationsUserTests.ts, 85, 14)) +>Iterable : Symbol(Iterable, Decl(lib.es2015.iterable.d.ts, --, --)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelationsUserTests.ts, 85, 38)) +>S : Symbol(S, Decl(distributiveConditionalTypeRelationsUserTests.ts, 85, 14)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelationsUserTests.ts, 85, 38)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelationsUserTests.ts, 85, 38)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelationsUserTests.ts, 85, 38)) + + export function join>>( +>join : Symbol(join, Decl(distributiveConditionalTypeRelationsUserTests.ts, 85, 99)) +>A : Symbol(A, Decl(distributiveConditionalTypeRelationsUserTests.ts, 87, 25)) +>Iterable : Symbol(Iterable, Decl(lib.es2015.iterable.d.ts, --, --)) +>Box : Symbol(Box, Decl(distributiveConditionalTypeRelationsUserTests.ts, 80, 18)) + + value: [any, ...any[]], +>value : Symbol(value, Decl(distributiveConditionalTypeRelationsUserTests.ts, 87, 55)) + + ): With> ? T : never> { +>With : Symbol(With, Decl(distributiveConditionalTypeRelationsUserTests.ts, 83, 5)) +>A : Symbol(A, Decl(distributiveConditionalTypeRelationsUserTests.ts, 87, 25)) +>A : Symbol(A, Decl(distributiveConditionalTypeRelationsUserTests.ts, 87, 25)) +>Iterable : Symbol(Iterable, Decl(lib.es2015.iterable.d.ts, --, --)) +>Box : Symbol(Box, Decl(distributiveConditionalTypeRelationsUserTests.ts, 80, 18)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelationsUserTests.ts, 89, 43)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelationsUserTests.ts, 89, 43)) + + return value; +>value : Symbol(value, Decl(distributiveConditionalTypeRelationsUserTests.ts, 87, 55)) + } + + type Result = [T] extends [Box] ? Box : never; +>Result : Symbol(Result, Decl(distributiveConditionalTypeRelationsUserTests.ts, 91, 5)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelationsUserTests.ts, 93, 16)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelationsUserTests.ts, 93, 16)) +>Box : Symbol(Box, Decl(distributiveConditionalTypeRelationsUserTests.ts, 80, 18)) +>U : Symbol(U, Decl(distributiveConditionalTypeRelationsUserTests.ts, 93, 43)) +>Box : Symbol(Box, Decl(distributiveConditionalTypeRelationsUserTests.ts, 80, 18)) +>U : Symbol(U, Decl(distributiveConditionalTypeRelationsUserTests.ts, 93, 43)) + + export function result>(value: Box): Result { +>result : Symbol(result, Decl(distributiveConditionalTypeRelationsUserTests.ts, 93, 65)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelationsUserTests.ts, 95, 27)) +>Box : Symbol(Box, Decl(distributiveConditionalTypeRelationsUserTests.ts, 80, 18)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelationsUserTests.ts, 95, 47)) +>Box : Symbol(Box, Decl(distributiveConditionalTypeRelationsUserTests.ts, 80, 18)) +>Result : Symbol(Result, Decl(distributiveConditionalTypeRelationsUserTests.ts, 91, 5)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelationsUserTests.ts, 95, 27)) + + return value; +>value : Symbol(value, Decl(distributiveConditionalTypeRelationsUserTests.ts, 95, 47)) + } +} + +namespace NonDistributive { +>NonDistributive : Symbol(NonDistributive, Decl(distributiveConditionalTypeRelationsUserTests.ts, 98, 1)) + + export function answer(value: number): [R] extends [true] ? number : number | undefined { +>answer : Symbol(answer, Decl(distributiveConditionalTypeRelationsUserTests.ts, 100, 27)) +>R : Symbol(R, Decl(distributiveConditionalTypeRelationsUserTests.ts, 101, 27)) +>value : Symbol(value, Decl(distributiveConditionalTypeRelationsUserTests.ts, 101, 46)) +>R : Symbol(R, Decl(distributiveConditionalTypeRelationsUserTests.ts, 101, 27)) + + return value; +>value : Symbol(value, Decl(distributiveConditionalTypeRelationsUserTests.ts, 101, 46)) + } + + export function items(): +>items : Symbol(items, Decl(distributiveConditionalTypeRelationsUserTests.ts, 103, 5)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelationsUserTests.ts, 105, 26)) +>O : Symbol(O, Decl(distributiveConditionalTypeRelationsUserTests.ts, 105, 28)) +>useSeparators : Symbol(useSeparators, Decl(distributiveConditionalTypeRelationsUserTests.ts, 105, 40)) + + [O] extends [{ useSeparators: true }] ? (T | { separator: true })[] : T[] { +>O : Symbol(O, Decl(distributiveConditionalTypeRelationsUserTests.ts, 105, 28)) +>useSeparators : Symbol(useSeparators, Decl(distributiveConditionalTypeRelationsUserTests.ts, 106, 22)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelationsUserTests.ts, 105, 26)) +>separator : Symbol(separator, Decl(distributiveConditionalTypeRelationsUserTests.ts, 106, 54)) +>T : Symbol(T, Decl(distributiveConditionalTypeRelationsUserTests.ts, 105, 26)) + + return []; + } +} + diff --git a/tsc/testdata/baselines/reference/compiler/distributiveConditionalTypeRelationsUserTests.types b/tsc/testdata/baselines/reference/compiler/distributiveConditionalTypeRelationsUserTests.types new file mode 100644 index 0000000000000..3d8d6a129ff5d --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/distributiveConditionalTypeRelationsUserTests.types @@ -0,0 +1,258 @@ +//// [tests/cases/compiler/distributiveConditionalTypeRelationsUserTests.ts] //// + +=== distributiveConditionalTypeRelationsUserTests.ts === +namespace Inquirer { +>Inquirer : typeof Inquirer + + type Answer = R extends true ? number : number | undefined; +>Answer : Answer +>true : true + + export function answer(validate: (value: Answer) => boolean, value: number) { +>answer : (validate: (value: Answer) => boolean, value: number) => boolean +>validate : (value: Answer) => boolean +>value : Answer +>value : number + + return validate(value); +>validate(value) : boolean +>validate : (value: Answer) => boolean +>value : number + } +} + +namespace VSCode { +>VSCode : typeof VSCode + + interface Separator { + separator: true; +>separator : true +>true : true + } + + export class QuickPick { +>QuickPick : QuickPick +>useSeparators : boolean + + private items: O extends { useSeparators: true } ? (T | Separator)[] : T[] = []; +>items : O extends { useSeparators: true; } ? (T | Separator)[] : T[] +>useSeparators : true +>true : true +>[] : never[] + } +} + +namespace Fabric { +>Fabric : typeof Fabric + + type Easing = T extends number[] +>Easing : Easing + + ? (time: number, start: number, delta: number, duration: number, index: number) => number +>time : number +>start : number +>delta : number +>duration : number +>index : number + + : (time: number, start: number, delta: number, duration: number) => number; +>time : number +>start : number +>delta : number +>duration : number + + const defaultEasing = (time: number, start: number, delta: number, duration: number) => start; +>defaultEasing : (time: number, start: number, delta: number, duration: number) => number +>(time: number, start: number, delta: number, duration: number) => start : (time: number, start: number, delta: number, duration: number) => number +>time : number +>start : number +>delta : number +>duration : number +>start : number + + export function easing(): Easing { +>easing : () => Easing + + return defaultEasing; +>defaultEasing : (time: number, start: number, delta: number, duration: number) => number + } +} + +namespace Supabase { +>Supabase : typeof Supabase + + interface Row { + id: number; +>id : number + + columns?: string[]; +>columns : string[] | undefined + } + + type Selected = T extends true ? Row : Omit; +>Selected : Selected +>true : true + + export function rows(value: Row[]): Selected[] { +>rows : (value: Row[]) => Selected[] +>value : Row[] + + return value; +>value : Row[] + } +} + +namespace Lexical { +>Lexical : typeof Lexical + + class Slot { +>Slot : Slot + + value = 0; +>value : number +>0 : 0 + } + + class ElementSlot extends Slot { +>ElementSlot : ElementSlot +>Slot : Slot + + element = true; +>element : boolean +>true : true + + withAfter(): ElementSlot { +>withAfter : () => ElementSlot + + return this; +>this : this + } + } + + class BaseNode { +>BaseNode : BaseNode + + node = true; +>node : boolean +>true : true + } + + type SlotForNode = N extends ElementNode ? ElementSlot : Slot; +>SlotForNode : SlotForNode + + declare function getSlot(node: N): SlotForNode; +>getSlot : (node: N) => SlotForNode +>node : N + + export class ElementNode extends BaseNode { +>ElementNode : ElementNode +>BaseNode : BaseNode + + element = true; +>element : boolean +>true : true + + refresh() { +>refresh : () => void + + let slot = getSlot(this); +>slot : SlotForNode +>getSlot(this) : SlotForNode +>getSlot : (node: N) => SlotForNode +>this : this + + slot = slot.withAfter(); +>slot = slot.withAfter() : ElementSlot +>slot : SlotForNode +>slot.withAfter() : ElementSlot +>slot.withAfter : () => ElementSlot +>slot : SlotForNode +>withAfter : () => ElementSlot + } + } +} + +namespace LobeHub { +>LobeHub : typeof LobeHub + + type RequestResult = Throw extends true ? Promise : Promise; +>RequestResult : RequestResult +>true : true + + export const request: ( +>request : (data: Data, flag: Throw) => RequestResult + + data: Data, +>data : Data + + flag: Throw, +>flag : Throw + + ) => RequestResult = async (data, flag) => data; +>async (data, flag) => data : (data: Data, flag: Throw) => Promise +>data : Data +>flag : Throw +>data : Data +} + +namespace Effect { +>Effect : typeof Effect + + interface Box { + value: T; +>value : T + } + + type With, T> = S extends readonly [any, ...any[]] ? [T, ...T[]] : T[]; +>With : With + + export function join>>( +>join : >>(value: [any, ...any[]]) => With> ? T : never> + + value: [any, ...any[]], +>value : [any, ...any[]] + + ): With> ? T : never> { + return value; +>value : [any, ...any[]] + } + + type Result = [T] extends [Box] ? Box : never; +>Result : Result + + export function result>(value: Box): Result { +>result : >(value: Box) => Result +>value : Box + + return value; +>value : Box + } +} + +namespace NonDistributive { +>NonDistributive : typeof NonDistributive + + export function answer(value: number): [R] extends [true] ? number : number | undefined { +>answer : (value: number) => [R] extends [true] ? number : number | undefined +>value : number +>true : true + + return value; +>value : number + } + + export function items(): +>items : () => [O] extends [{ useSeparators: true; }] ? (T | { separator: true; })[] : T[] +>useSeparators : boolean + + [O] extends [{ useSeparators: true }] ? (T | { separator: true })[] : T[] { +>useSeparators : true +>true : true +>separator : true +>true : true + + return []; +>[] : never[] + } +} + diff --git a/tsc/testdata/baselines/reference/compiler/excessPropertyCheckingIntersectionWithConditional.js b/tsc/testdata/baselines/reference/compiler/excessPropertyCheckingIntersectionWithConditional.js index ffe10119ef9b1..e0c9ed12200b3 100644 --- a/tsc/testdata/baselines/reference/compiler/excessPropertyCheckingIntersectionWithConditional.js +++ b/tsc/testdata/baselines/reference/compiler/excessPropertyCheckingIntersectionWithConditional.js @@ -3,11 +3,11 @@ //// [excessPropertyCheckingIntersectionWithConditional.ts] type Foo = K extends unknown ? { a: number } : unknown const createDefaultExample = (x: K): Foo & { x: K; } => { - return { a: 1, x: x }; // okay in TS 4.7.4, error in TS 4.8.2 + return { a: 1, x: x }; } //// [excessPropertyCheckingIntersectionWithConditional.js] "use strict"; const createDefaultExample = (x) => { - return { a: 1, x: x }; // okay in TS 4.7.4, error in TS 4.8.2 + return { a: 1, x: x }; }; diff --git a/tsc/testdata/baselines/reference/compiler/excessPropertyCheckingIntersectionWithConditional.symbols b/tsc/testdata/baselines/reference/compiler/excessPropertyCheckingIntersectionWithConditional.symbols index c0086f74dc11c..b442616616aaa 100644 --- a/tsc/testdata/baselines/reference/compiler/excessPropertyCheckingIntersectionWithConditional.symbols +++ b/tsc/testdata/baselines/reference/compiler/excessPropertyCheckingIntersectionWithConditional.symbols @@ -17,7 +17,7 @@ const createDefaultExample = (x: K): Foo & { x: K; } => { >x : Symbol(x, Decl(excessPropertyCheckingIntersectionWithConditional.ts, 1, 51)) >K : Symbol(K, Decl(excessPropertyCheckingIntersectionWithConditional.ts, 1, 30)) - return { a: 1, x: x }; // okay in TS 4.7.4, error in TS 4.8.2 + return { a: 1, x: x }; >a : Symbol(a, Decl(excessPropertyCheckingIntersectionWithConditional.ts, 2, 10)) >x : Symbol(x, Decl(excessPropertyCheckingIntersectionWithConditional.ts, 2, 16)) >x : Symbol(x, Decl(excessPropertyCheckingIntersectionWithConditional.ts, 1, 34)) diff --git a/tsc/testdata/baselines/reference/compiler/excessPropertyCheckingIntersectionWithConditional.types b/tsc/testdata/baselines/reference/compiler/excessPropertyCheckingIntersectionWithConditional.types index 5e2a24c01df20..3527571189b14 100644 --- a/tsc/testdata/baselines/reference/compiler/excessPropertyCheckingIntersectionWithConditional.types +++ b/tsc/testdata/baselines/reference/compiler/excessPropertyCheckingIntersectionWithConditional.types @@ -7,11 +7,11 @@ type Foo = K extends unknown ? { a: number } : unknown const createDefaultExample = (x: K): Foo & { x: K; } => { >createDefaultExample : (x: K) => Foo & { x: K; } ->(x: K): Foo & { x: K; } => { return { a: 1, x: x }; // okay in TS 4.7.4, error in TS 4.8.2} : (x: K) => Foo & { x: K; } +>(x: K): Foo & { x: K; } => { return { a: 1, x: x };} : (x: K) => Foo & { x: K; } >x : K >x : K - return { a: 1, x: x }; // okay in TS 4.7.4, error in TS 4.8.2 + return { a: 1, x: x }; >{ a: 1, x: x } : { a: number; x: K; } >a : number >1 : 1 diff --git a/tsc/testdata/tests/cases/compiler/conditionalTypeAssignabilityWhenDeferred.ts b/tsc/testdata/tests/cases/compiler/conditionalTypeAssignabilityWhenDeferred.ts index 4d0b56da0a663..d33b4da0f8d7d 100644 --- a/tsc/testdata/tests/cases/compiler/conditionalTypeAssignabilityWhenDeferred.ts +++ b/tsc/testdata/tests/cases/compiler/conditionalTypeAssignabilityWhenDeferred.ts @@ -44,7 +44,7 @@ function f2(t1: { x: T; y: T }, t2: T extends T ? { x: T; y: T } : never) { type Foo = T extends true ? string : "a"; function test(x: Foo, s: string) { - x = "a"; // Currently an error, should be ok + x = "a"; // OK: both branches accept "a". x = s; // Error } @@ -64,20 +64,15 @@ function testAssignabilityToConditionalType() { // Infer type parameters: no good const o2: [T] extends [[infer U]] ? U : { b: number } = o; - // The next 4 are arguable - if you choose to ignore the `never` distribution case, - // then they're all good. The `never` case _is_ a bit of an outlier - we say distributive types - // look approximately like the sum of their branches, but the `never` case bucks that. - // There's an argument for the result of dumping `never` into a distributive conditional - // being not `never`, but instead the intersection of the branches - a much more precise bound - // on that "impossible" input. + // Independent branches retain branch-wise checking without a distributive source. - // Distributive where T might instantiate to never: no good + // Distributive with independent branches: OK const o3: Distributive = o; - // Distributive where T & string might instantiate to never: also no good + // Intersection check type with independent branches: OK const o4: Distributive = o; // Distributive where {a: T} cannot instantiate to never: OK const o5: Distributive<{ a: T }> = o; - // Distributive where check type is a conditional which returns a non-never type upon instantiation with `never` but can still return never otherwise: no good + // Conditional check type with independent branches: OK const o6: Distributive<[T] extends [never] ? { a: number } : never> = o; } @@ -105,7 +100,7 @@ type InferBecauseWhyNot = [T] extends [(p: infer P1) => any] : never; function f3 any>(x: Q): InferBecauseWhyNot { - return x; + return x; // Error: Q can add overloads that change the inferred parameter. } type InferBecauseWhyNotDistributive = T extends (p: infer P1) => any @@ -115,5 +110,5 @@ type InferBecauseWhyNotDistributive = T extends (p: infer P1) => any function f4 any>( x: Q ): InferBecauseWhyNotDistributive { - return x; // should fail + return x; // Error: additional overloads can make the extends check fail. } diff --git a/tsc/testdata/tests/cases/compiler/distributiveConditionalTypeInferenceNonStrict.ts b/tsc/testdata/tests/cases/compiler/distributiveConditionalTypeInferenceNonStrict.ts new file mode 100644 index 0000000000000..7b26772da81fa --- /dev/null +++ b/tsc/testdata/tests/cases/compiler/distributiveConditionalTypeInferenceNonStrict.ts @@ -0,0 +1,15 @@ +// @strict: false +// @noEmit: true + +type InferredParameter = T extends (value: infer P) => unknown ? T | P : never; + +function inference unknown>(value: T) { + const result: InferredParameter = value; // Error: bivariant constraints admit narrower parameters. +} + +declare const overloaded: { + (value: string): void; + (left: number, right: number): void; +}; +const constraint: (value: unknown) => unknown = overloaded; +const rejected: InferredParameter = overloaded; // Error diff --git a/tsc/testdata/tests/cases/compiler/distributiveConditionalTypeInferencePatterns.ts b/tsc/testdata/tests/cases/compiler/distributiveConditionalTypeInferencePatterns.ts new file mode 100644 index 0000000000000..c1422211d9159 --- /dev/null +++ b/tsc/testdata/tests/cases/compiler/distributiveConditionalTypeInferencePatterns.ts @@ -0,0 +1,113 @@ +// @strict: true +// @noEmit: true + +namespace IntersectionInference { + type Result = T extends { a: infer U & string; b: infer U } ? T : never; + type NonDistributive = [T] extends [{ a: infer U & string; b: infer U }] ? T : never; + + export function generic(value: T): Result { + return value; // Error: string is removed before inferring U from b. + } + + export function nonDistributive(value: T): NonDistributive { + return value; // Error + } + + export const rejected: Result<{ a: string; b: number }> = { a: "", b: 1 }; // Error + export const accepted: Result<{ a: string; b: string }> = { a: "", b: "" }; + + type Covariant = T extends { a: infer U; b: infer U } ? T : never; + type ObjectIntersection = T extends { a: infer U } & { b: number } ? T : never; + + export function covariant(value: T): Covariant { + return value; + } + + export function objectIntersection(value: T): ObjectIntersection { + return value; + } +} + +namespace DependentConstraints { + interface Dep { + a: A; + b: B; + } + + type Result = T extends Dep ? T : never; + type NonDistributive = [T] extends [Dep] ? T : never; + + export function generic(value: T): Result { + return value; // Error: the inferred B must satisfy the same A. + } + + export function nonDistributive(value: T): NonDistributive { + return value; // Error + } + + export const rejected: Result<{ a: string; b: number }> = { a: "", b: 1 }; // Error + export const accepted: Result<{ a: string; b: string }> = { a: "", b: "" }; + + interface Independent { + a: A; + b: B; + } + + type IndependentResult = T extends Independent ? T : never; + + export function independent(value: T): IndependentResult { + return value; + } +} + +namespace InferredBranchDependence { + type Box = T extends infer U ? { value?: U } : never; + type PropertyBox = T extends { value: infer U } ? { copy?: U } : never; + type Constant = T extends infer U ? { done: true } : never; + + export function box(): Box { + return {}; // Error: Box is never even though the branch only references U. + } + + export function propertyBox(): PropertyBox { + return {}; // Error + } + + export function constant(): Constant { + return { done: true }; // Error: an unused infer binder still requires a nonempty check type. + } + + export const rejectedBox: Box = {}; // Error + export const rejectedProperty: PropertyBox = {}; // Error + export const rejectedConstant: Constant = { done: true }; // Error + + type NonDistributive = [T] extends [infer U] ? { value?: U } : never; + + export function nonDistributive(): NonDistributive { + return {}; + } + + export const emptyNonDistributive: NonDistributive = {}; + + export function nonNeverUnion(): Box { + return {}; + } + + export function nonNeverProperty(): PropertyBox { + return {}; + } + + export function nonNeverObject(): Box<{ value: T }> { + return {}; + } + + export function nonNeverConditional(): Box<[T] extends [never] ? { value: T } : number> { + return {}; + } + + type Identity = T extends infer U ? U : never; + + export function identity(value: T): Identity { + return value; + } +} diff --git a/tsc/testdata/tests/cases/compiler/distributiveConditionalTypeRelations.ts b/tsc/testdata/tests/cases/compiler/distributiveConditionalTypeRelations.ts new file mode 100644 index 0000000000000..d46925cf44dd0 --- /dev/null +++ b/tsc/testdata/tests/cases/compiler/distributiveConditionalTypeRelations.ts @@ -0,0 +1,317 @@ +// @strict: true +// @noEmit: true + +// The original #27932 cases are in conditionalTypeAssignabilityWhenDeferred.ts. + +type Either = T extends U ? T | { matched: true } : T | { matched: false }; +type Both = T extends U ? T & U : T; +type Constant = T extends string ? { a: number } : { b: number }; +type Pair = T extends unknown ? { x: T; y: T } : never; +type Tuple = T extends unknown ? [T, T] : never; +type Callback = T extends unknown ? (value: T) => T : never; +type Repeated = T extends unknown ? `${T}-${T}` : never; +type Remapped = T extends unknown ? `key-${T}` : never; +type Upper = T extends unknown ? Uppercase : never; + +function identity(value: T, intersection: T & U) { + const either: Either = value; + const both: Both = value; + const intersected: Either = intersection; + const extracted: Extract = value; + const excluded: Exclude = value; +} + +function wrappers(value: `key-${T}`, upper: Uppercase, repeated: `${T}-${T}`) { + const remapped: Remapped = value; + const capitalized: Upper = upper; + const correlated: Repeated = repeated; // Error: cross products do not distribute. +} + +type WithOuter = T extends unknown ? `${T}-${U}` : never; + +function retainOuterArgument(value: `${T}-${T}`) { + const result: WithOuter = value; // Error: the uniform-instantiation proof conservatively rejects repeated occurrences. +} + +type OuterSource = T extends unknown ? `${T}-${U}` : never; +type OuterRepeated = T extends unknown ? `outer-${T}-${T}` : never; +type UpperRepeated = T extends unknown ? `${Uppercase}-${Uppercase}` : never; +type InferredRepeated = [T] extends [infer U extends string] ? `${U}-${U}` : never; + +function retainConditionalArgument(value: `outer-${OuterSource}`) { + const correlated: OuterRepeated = value; // Error: the second argument is not distributed. +} + +function cachedOccurrences(value: `${Uppercase}-${Uppercase}`) { + const correlated: UpperRepeated = value; // Error: a cached inner type still occurs twice. +} + +function deferredOccurrences(value: `outer-${InferredRepeated}`) { + const correlated: OuterRepeated = value; // Error: deferred inference hides a non-distributive use. +} + +type Linked = T extends unknown ? T & { value: T } : never; + +function retainIntersectionComponent(value: T & { value: T }) { + const correlated: Linked = value; // Error: the property can come from a different constituent. +} + +type ObjectValues = T extends unknown ? T["value"] : never; +type IndexedValues = K extends unknown ? T[K] : never; + +function indexedAccess(value: T["value"], indexed: T[K]) { + const fromObject: ObjectValues = value; + const fromIndex: IndexedValues = indexed; +} + +function correlated(pair: { x: T; y: T }, tuple: [T, T], callback: (value: T) => T) { + const p: Pair = pair; // Error + const t: Tuple = tuple; // Error + const c: Callback = callback; // Error +} + +function possiblyNever(value: { a: number; b: number }) { + const unconstrained: Constant = value; + const constrained: Constant = value; + const conditional: Constant<[T] extends [never] ? object : never> = value; +} + +function requiredWitness(value: { a: number; b: number; value: T }) { + const result: Constant = value; +} + +requiredWitness({ a: 1, b: 2, get value(): never { throw new Error(); } }); + +function optionalWitness(value: { a: number; b: number; value?: T }) { + const result: Constant = value; +} + +function arrayWitness(value: { a: number; b: number; values: T[] }) { + const result: Constant = value; +} + +function functionWitness(value: { a: number; b: number; get: () => T }) { + const result: Constant = value; +} + +function definitelyNotNever(value: { a: number; b: number }) { + const object: Constant<{ value: T }> = value; + const array: Constant = value; + const tuple: Constant<[T]> = value; + const union: Constant = value; + const conditional: Constant<[T] extends [string] ? { value: T } : { other: T }> = value; +} + +type Homomorphic = { [K in keyof T]: T[K] }; +type Spread = [...T]; + +function emptyInstantiations(value: { a: number; b: number }) { + const mapped: Constant> = value; + const variadic: Constant> = value; +} + +type Inferred = T extends (value: infer P) => unknown ? T | P : never; +type InferredNonDistributive = [T] extends [(value: infer P) => unknown] ? T | P : never; +type InferredValue = T extends { value: infer P } ? P : never; +type InferredCallback = T extends { value: infer P } ? (value: P) => P : never; +type ConstrainedInfer = T extends { value: infer P extends string } ? T : never; + +function inference unknown>(value: T) { + const distributive: Inferred = value; // Error: additional overloads can change the inferred parameter. + const nonDistributive: InferredNonDistributive = value; // Error +} + +function independentInference(value: T) { + const result: T extends infer P ? T | P : never = value; +} + +function propertyInference(value: T) { + const result: T extends { value: infer P } ? T | P : never = value; +} + +function tupleInference(value: T) { + const result: [T] extends [infer P] ? T | P : never = value; +} + +function independentParameters unknown>(value: T) { + const result: T extends (value: infer P) => infer R ? T | P | R : never = value; // Error +} + +type InferredReturn = T extends () => infer R ? T | R : never; + +function returnInference unknown>(value: T) { + const result: InferredReturn = value; // Error: inference can use an overload with incompatible arity. +} + +function universalSignature never>(value: T) { + const parameter: Inferred = value; + const both: T extends (value: infer P) => infer R ? T | P | R : never = value; +} + +function ignoredParameter never>(value: T) { + const result: Inferred = value; + const returned: InferredReturn = value; +} + +type InferredMethod = T extends { method: (value: infer P) => unknown } ? T | P : never; +type InferredMethodReturn = T extends { method: (value: any) => infer R } ? T | R : never; + +function bivariantInference(value: T) { + const result: InferredMethod = value; // Error: the constraint permits narrower method parameters. + const returned: InferredMethodReturn = value; // Error: a never parameter fails the strict function pattern. +} + +function dependentInference(value: string, callback: (value: string) => string) { + const inferred: InferredValue = value; // Error: T can contain a narrower value. + const inferredCallback: InferredCallback = callback; // Error +} + +function constrainedInference(value: T) { + const result: ConstrainedInfer = value; // Error: any in the constraint does not imply string. +} + +type SpecificParameter = T extends (value: string) => infer R ? T | R : never; +type SpecificMethod = T extends { method(value: string): infer R } ? T | R : never; +type OptionalProperty = T extends { optional?: string; value: infer R } ? T | R : never; +type MixedInference = T extends { input: (value: infer R) => void; output: infer R } ? T | R : never; +type SingleParameter = T extends (value: infer R) => unknown ? T | R : never; +type OptionalParameter = T extends (value?: infer R) => unknown ? T | R : never; +type IntersectionPattern = T extends { value: infer R } & { method(value: string): unknown } ? T | R : never; +type PrimitiveBoundary = T extends object | (infer R extends number) ? T | R : never; + +function fixedParameter unknown>(value: T) { + const result: SpecificParameter = value; // Error: a number parameter also satisfies the constraint. + const withoutInference: T extends (value: string) => unknown ? T : never = value; // Error +} + +function bivariantParameter(value: T) { + const result: SpecificMethod = value; // Error +} + +function optionalProperty(value: T) { + const result: OptionalProperty = value; // Error: subtypes can add an incompatible optional property. +} + +function mixedInference void; output: any }>(value: T) { + const result: MixedInference = value; // Error: input and output can require incompatible inferences. +} + +interface InvariantInferencePattern { + input: (value: T) => void; + output: T; +} + +type InvariantInference = T extends InvariantInferencePattern ? T | R : never; + +function invariantInference>(value: T) { + const result: InvariantInference = value; // Error: invariant type-argument inference must not hide mixed positions. +} + +enum NumericInferencePattern { + value +} + +type NumericInference = T extends (value: infer P) => NumericInferencePattern ? T | P : never; + +function numericInference number>(value: T) { + const result: NumericInference = value; // Error: number-to-enum compatibility does not cover every numeric literal. +} + +function unboundedArity unknown>(value: T) { + const result: SingleParameter = value; // Error: subtypes can require multiple arguments. +} + +function optionalParameter unknown>(value: T) { + const result: OptionalParameter = value; // Error: subtypes can require an argument. +} + +function intersectionPattern(value: T) { + const result: IntersectionPattern = value; // Error +} + +function primitiveBoundary(value: T) { + const result: PrimitiveBoundary = value; // Error: structural object constraints admit primitives. +} + +class Nominal { + private brand!: void; + value!: T; +} + +function nominalInference(value: T) { + const result: T extends Nominal ? T | P : never = value; // Error +} + +type FilteredKeys = T extends U ? never : T; +type DecoratedKeys = T extends U ? never : `key-${T}`; + +function filtering(value: FilteredKeys) { + const excluded: Exclude = value; + const remapped: DecoratedKeys, never> = `key-${value}`; +} + +function filteredRemapping(value: `key-${Exclude}`) { + const result: DecoratedKeys = value; +} + +type FilteredMap = { [K in T as K extends U ? never : K]: unknown }; +type DecoratedMap = { [K in T as K extends U ? never : `key-${K}`]: unknown }; + +function mappedKeys(key: Exclude, decorated: `key-${Exclude}`) { + const filtered: keyof FilteredMap = key; + const remapped: keyof DecoratedMap = decorated; +} + +type Recursive = T extends object ? T | { next: Recursive } : T; + +function recursion(value: T) { + const result: Recursive = value; +} + +function subtypeIsNotConstituent(value: T & string) { + const result: Extract = value; // Error: Extract is never. +} + +// Concrete instantiations witnessing the rejected assignments. +const mixedPair: Pair<"a" | "b"> = { x: "a", y: "b" }; // Error +const mixedTuple: Tuple<"a" | "b"> = ["a", "b"]; // Error +const mixedString: Repeated<"a" | "b"> = "a-b"; // Error +const emptyConstant: Constant = { a: 1, b: 2 }; // Error +const narrowInference: InferredValue<{ value: "a" }> = "b"; // Error +const rejectedConstrainedInfer: ConstrainedInfer<{ value: number }> = { value: 1 }; // Error +const rejectedFixedParameter: SpecificParameter<(value: number) => boolean> = (value: number) => true; // Error +const rejectedMethod: SpecificMethod<{ method(value: number): boolean }> = { method: (value: number) => true }; // Error +const rejectedOptionalProperty: OptionalProperty<{ optional: number; value: boolean }> = { optional: 1, value: true }; // Error +const rejectedMixedInference: MixedInference<{ input(value: number): void; output: string }> = { input: (value: number) => {}, output: "" }; // Error +const rejectedInvariantInference: InvariantInference<{ input: (value: number) => void; output: string }> = { input: (value: number) => {}, output: "" }; // Error +const rejectedNumericInference: NumericInference<(value: unknown) => 123> = (value: unknown) => 123; // Error +const rejectedArity: SingleParameter<(left: number, right: number) => void> = (left: number, right: number) => {}; // Error +const rejectedOptionalParameter: OptionalParameter<(value: string) => void> = (value: string) => {}; // Error +const rejectedIntersection: IntersectionPattern<{ value: boolean; method(value: number): unknown }> = { value: true, method: (value: number) => {} }; // Error +const rejectedPrimitive: PrimitiveBoundary = ""; // Error + +declare const overloadedParameter: { + (value: string): void; + (left: number, right: number): void; +}; +const parameterConstraint: (value: any) => unknown = overloadedParameter; +const rejectedOverloadedParameter: Inferred = overloadedParameter; // Error +const rejectedOverloadedNonDistributive: InferredNonDistributive = overloadedParameter; // Error + +declare const overloadedReturn: { + (): string; + (value: number): number; +}; +const returnConstraint: () => unknown = overloadedReturn; +const rejectedOverloadedReturn: InferredReturn = overloadedReturn; // Error + +declare const overloadedMethod: { + method(value: string): never; + method(left: number, right: number): void; +}; +const methodConstraint: { method(value: unknown): never } = overloadedMethod; +const rejectedOverloadedMethod: InferredMethod = overloadedMethod; // Error + +declare const neverParameterMethod: { method(value: never): never }; +const neverMethodConstraint: { method(value: unknown): never } = neverParameterMethod; +const rejectedMethodReturn: InferredMethodReturn = neverParameterMethod; // Error diff --git a/tsc/testdata/tests/cases/compiler/distributiveConditionalTypeRelationsUserTests.ts b/tsc/testdata/tests/cases/compiler/distributiveConditionalTypeRelationsUserTests.ts new file mode 100644 index 0000000000000..010cc537ee6bb --- /dev/null +++ b/tsc/testdata/tests/cases/compiler/distributiveConditionalTypeRelationsUserTests.ts @@ -0,0 +1,115 @@ +// @strict: true +// @exactOptionalPropertyTypes: true +// @target: esnext +// @noEmit: true + +namespace Inquirer { + type Answer = R extends true ? number : number | undefined; + + export function answer(validate: (value: Answer) => boolean, value: number) { + return validate(value); + } +} + +namespace VSCode { + interface Separator { + separator: true; + } + + export class QuickPick { + private items: O extends { useSeparators: true } ? (T | Separator)[] : T[] = []; + } +} + +namespace Fabric { + type Easing = T extends number[] + ? (time: number, start: number, delta: number, duration: number, index: number) => number + : (time: number, start: number, delta: number, duration: number) => number; + + const defaultEasing = (time: number, start: number, delta: number, duration: number) => start; + + export function easing(): Easing { + return defaultEasing; + } +} + +namespace Supabase { + interface Row { + id: number; + columns?: string[]; + } + + type Selected = T extends true ? Row : Omit; + + export function rows(value: Row[]): Selected[] { + return value; + } +} + +namespace Lexical { + class Slot { + value = 0; + } + + class ElementSlot extends Slot { + element = true; + withAfter(): ElementSlot { + return this; + } + } + + class BaseNode { + node = true; + } + + type SlotForNode = N extends ElementNode ? ElementSlot : Slot; + declare function getSlot(node: N): SlotForNode; + + export class ElementNode extends BaseNode { + element = true; + refresh() { + let slot = getSlot(this); + slot = slot.withAfter(); + } + } +} + +namespace LobeHub { + type RequestResult = Throw extends true ? Promise : Promise; + + export const request: ( + data: Data, + flag: Throw, + ) => RequestResult = async (data, flag) => data; +} + +namespace Effect { + interface Box { + value: T; + } + + type With, T> = S extends readonly [any, ...any[]] ? [T, ...T[]] : T[]; + + export function join>>( + value: [any, ...any[]], + ): With> ? T : never> { + return value; + } + + type Result = [T] extends [Box] ? Box : never; + + export function result>(value: Box): Result { + return value; + } +} + +namespace NonDistributive { + export function answer(value: number): [R] extends [true] ? number : number | undefined { + return value; + } + + export function items(): + [O] extends [{ useSeparators: true }] ? (T | { separator: true })[] : T[] { + return []; + } +} diff --git a/tsc/testdata/tests/cases/compiler/excessPropertyCheckingIntersectionWithConditional.ts b/tsc/testdata/tests/cases/compiler/excessPropertyCheckingIntersectionWithConditional.ts index 93fc1c9e6039f..8947de087dba4 100644 --- a/tsc/testdata/tests/cases/compiler/excessPropertyCheckingIntersectionWithConditional.ts +++ b/tsc/testdata/tests/cases/compiler/excessPropertyCheckingIntersectionWithConditional.ts @@ -1,5 +1,5 @@ // @target: es2015 type Foo = K extends unknown ? { a: number } : unknown const createDefaultExample = (x: K): Foo & { x: K; } => { - return { a: 1, x: x }; // okay in TS 4.7.4, error in TS 4.8.2 + return { a: 1, x: x }; } \ No newline at end of file