Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bring back return type narrowing + fixes #61359

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
586 changes: 567 additions & 19 deletions src/compiler/checker.ts

Large diffs are not rendered by default.

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

// Flags that require TypeFlags.Union
/** @internal */
Expand Down Expand Up @@ -6909,12 +6912,16 @@ export interface StringMappingType extends InstantiableType {
}

// Type parameter substitution (TypeFlags.Substitution)
// Substitution types are created for type parameters or indexed access types that occur in the
// - Substitution types are created for type parameters or indexed access types that occur in the
// true branch of a conditional type. For example, in 'T extends string ? Foo<T> : Bar<T>', the
// reference to T in Foo<T> is resolved as a substitution type that substitutes 'string & T' for T.
// Thus, if Foo has a 'string' constraint on its type parameter, T will satisfy it.
// Substitution type are also created for NoInfer<T> types. Those are represented as substitution
// - Substitution types are also created for NoInfer<T> types. Those are represented as substitution
// types where the constraint is type 'unknown' (which is never generated for the case above).
// - Substitution types are also created for return type narrowing:
// if a type parameter `T` is linked to a parameter `x` and `x`'s narrowed type is `S`,
// we represent that with a substitution type with base `T` and constraint `S`.
// The resulting substitution type has `ObjectFlags.IsNarrowedType` set.
export interface SubstitutionType extends InstantiableType {
objectFlags: ObjectFlags;
baseType: Type; // Target type
Expand Down
645 changes: 645 additions & 0 deletions tests/baselines/reference/dependentReturnType1.errors.txt

Large diffs are not rendered by default.

Loading