Skip to content

Commit aa38e34

Browse files
committed
Revert "Revert return type narrowing (#61136)"
This reverts commit c3ae7c4.
1 parent c85e626 commit aa38e34

38 files changed

+12191
-311
lines changed

src/compiler/checker.ts

+358-19
Large diffs are not rendered by default.

src/compiler/types.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -6243,6 +6243,7 @@ export interface NodeLinks {
62436243
decoratorSignature?: Signature; // Signature for decorator as if invoked by the runtime.
62446244
spreadIndices?: { first: number | undefined, last: number | undefined }; // Indices of first and last spread elements in array literal
62456245
parameterInitializerContainsUndefined?: boolean; // True if this is a parameter declaration whose type annotation contains "undefined".
6246+
contextualReturnType?: Type; // If the node is a return statement's expression, then this is the contextual return type.
62466247
fakeScopeForSignatureDeclaration?: "params" | "typeParams"; // If present, this is a fake scope injected into an enclosing declaration chain.
62476248
assertionExpressionType?: Type; // Cached type of the expression of a type assertion
62486249
potentialThisCollisions?: Node[];
@@ -6510,6 +6511,8 @@ export const enum ObjectFlags {
65106511
IsGenericIndexType = 1 << 23, // Union or intersection contains generic index type
65116512
/** @internal */
65126513
IsGenericType = IsGenericObjectType | IsGenericIndexType,
6514+
/** @internal */
6515+
IsNarrowingType = 1 << 24, // Substitution type that comes from type narrowing
65136516

65146517
// Flags that require TypeFlags.Union
65156518
/** @internal */
@@ -6909,12 +6912,16 @@ export interface StringMappingType extends InstantiableType {
69096912
}
69106913

69116914
// Type parameter substitution (TypeFlags.Substitution)
6912-
// Substitution types are created for type parameters or indexed access types that occur in the
6915+
// - Substitution types are created for type parameters or indexed access types that occur in the
69136916
// true branch of a conditional type. For example, in 'T extends string ? Foo<T> : Bar<T>', the
69146917
// reference to T in Foo<T> is resolved as a substitution type that substitutes 'string & T' for T.
69156918
// Thus, if Foo has a 'string' constraint on its type parameter, T will satisfy it.
6916-
// Substitution type are also created for NoInfer<T> types. Those are represented as substitution
6919+
// - Substitution types are also created for NoInfer<T> types. Those are represented as substitution
69176920
// types where the constraint is type 'unknown' (which is never generated for the case above).
6921+
// - Substitution types are also created for return type narrowing:
6922+
// if a type parameter `T` is linked to a parameter `x` and `x`'s narrowed type is `S`,
6923+
// we represent that with a substitution type with base `T` and constraint `S`.
6924+
// The resulting substitution type has `ObjectFlags.IsNarrowedType` set.
69186925
export interface SubstitutionType extends InstantiableType {
69196926
objectFlags: ObjectFlags;
69206927
baseType: Type; // Target type

tests/baselines/reference/conditionalReturnExpression.errors.txt

-36
This file was deleted.

tests/baselines/reference/conditionalReturnExpression.symbols

-63
This file was deleted.

tests/baselines/reference/conditionalReturnExpression.types

-167
This file was deleted.

0 commit comments

Comments
 (0)