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

Revert return type narrowing #61136

Merged
merged 1 commit into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
377 changes: 19 additions & 358 deletions src/compiler/checker.ts

Large diffs are not rendered by default.

11 changes: 2 additions & 9 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6249,7 +6249,6 @@ 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 @@ -6517,8 +6516,6 @@ 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 @@ -6918,16 +6915,12 @@ 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 types are also created for NoInfer<T> types. Those are represented as substitution
// Substitution type 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
36 changes: 36 additions & 0 deletions tests/baselines/reference/conditionalReturnExpression.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
conditionalReturnExpression.ts(2,18): error TS2322: Type '1' is not assignable to type '3'.
conditionalReturnExpression.ts(2,23): error TS2322: Type '2' is not assignable to type '3'.
conditionalReturnExpression.ts(8,43): error TS2322: Type 'number' is not assignable to type 'string'.
conditionalReturnExpression.ts(19,71): error TS2322: Type 'number' is not assignable to type 'string'.


==== conditionalReturnExpression.ts (4 errors) ====
function return1(x: boolean): 3 {
return (x ? (1) : 2);
~
!!! error TS2322: Type '1' is not assignable to type '3'.
~
!!! error TS2322: Type '2' is not assignable to type '3'.
}

declare function getAny(): any;

function return2(x: string): string {
return x.startsWith("a") ? getAny() : 1;
~
!!! error TS2322: Type 'number' is not assignable to type 'string'.
}

function return3(x: string): string {
return x.startsWith("a") ? "a" : x;
}

function return4(x: string): string {
return (x.startsWith("a") ? getAny() : 1) as string;
}

const return5 = (x: string): string => x.startsWith("a") ? getAny() : 1;
~
!!! error TS2322: Type 'number' is not assignable to type 'string'.

const return6 = (x: string): string => (x.startsWith("a") ? getAny() : 1) as string;
63 changes: 63 additions & 0 deletions tests/baselines/reference/conditionalReturnExpression.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//// [tests/cases/compiler/conditionalReturnExpression.ts] ////

=== conditionalReturnExpression.ts ===
function return1(x: boolean): 3 {
>return1 : Symbol(return1, Decl(conditionalReturnExpression.ts, 0, 0))
>x : Symbol(x, Decl(conditionalReturnExpression.ts, 0, 17))

return (x ? (1) : 2);
>x : Symbol(x, Decl(conditionalReturnExpression.ts, 0, 17))
}

declare function getAny(): any;
>getAny : Symbol(getAny, Decl(conditionalReturnExpression.ts, 2, 1))

function return2(x: string): string {
>return2 : Symbol(return2, Decl(conditionalReturnExpression.ts, 4, 31))
>x : Symbol(x, Decl(conditionalReturnExpression.ts, 6, 17))

return x.startsWith("a") ? getAny() : 1;
>x.startsWith : Symbol(String.startsWith, Decl(lib.es2015.core.d.ts, --, --))
>x : Symbol(x, Decl(conditionalReturnExpression.ts, 6, 17))
>startsWith : Symbol(String.startsWith, Decl(lib.es2015.core.d.ts, --, --))
>getAny : Symbol(getAny, Decl(conditionalReturnExpression.ts, 2, 1))
}

function return3(x: string): string {
>return3 : Symbol(return3, Decl(conditionalReturnExpression.ts, 8, 1))
>x : Symbol(x, Decl(conditionalReturnExpression.ts, 10, 17))

return x.startsWith("a") ? "a" : x;
>x.startsWith : Symbol(String.startsWith, Decl(lib.es2015.core.d.ts, --, --))
>x : Symbol(x, Decl(conditionalReturnExpression.ts, 10, 17))
>startsWith : Symbol(String.startsWith, Decl(lib.es2015.core.d.ts, --, --))
>x : Symbol(x, Decl(conditionalReturnExpression.ts, 10, 17))
}

function return4(x: string): string {
>return4 : Symbol(return4, Decl(conditionalReturnExpression.ts, 12, 1))
>x : Symbol(x, Decl(conditionalReturnExpression.ts, 14, 17))

return (x.startsWith("a") ? getAny() : 1) as string;
>x.startsWith : Symbol(String.startsWith, Decl(lib.es2015.core.d.ts, --, --))
>x : Symbol(x, Decl(conditionalReturnExpression.ts, 14, 17))
>startsWith : Symbol(String.startsWith, Decl(lib.es2015.core.d.ts, --, --))
>getAny : Symbol(getAny, Decl(conditionalReturnExpression.ts, 2, 1))
}

const return5 = (x: string): string => x.startsWith("a") ? getAny() : 1;
>return5 : Symbol(return5, Decl(conditionalReturnExpression.ts, 18, 5))
>x : Symbol(x, Decl(conditionalReturnExpression.ts, 18, 17))
>x.startsWith : Symbol(String.startsWith, Decl(lib.es2015.core.d.ts, --, --))
>x : Symbol(x, Decl(conditionalReturnExpression.ts, 18, 17))
>startsWith : Symbol(String.startsWith, Decl(lib.es2015.core.d.ts, --, --))
>getAny : Symbol(getAny, Decl(conditionalReturnExpression.ts, 2, 1))

const return6 = (x: string): string => (x.startsWith("a") ? getAny() : 1) as string;
>return6 : Symbol(return6, Decl(conditionalReturnExpression.ts, 20, 5))
>x : Symbol(x, Decl(conditionalReturnExpression.ts, 20, 17))
>x.startsWith : Symbol(String.startsWith, Decl(lib.es2015.core.d.ts, --, --))
>x : Symbol(x, Decl(conditionalReturnExpression.ts, 20, 17))
>startsWith : Symbol(String.startsWith, Decl(lib.es2015.core.d.ts, --, --))
>getAny : Symbol(getAny, Decl(conditionalReturnExpression.ts, 2, 1))

167 changes: 167 additions & 0 deletions tests/baselines/reference/conditionalReturnExpression.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
//// [tests/cases/compiler/conditionalReturnExpression.ts] ////

=== conditionalReturnExpression.ts ===
function return1(x: boolean): 3 {
>return1 : (x: boolean) => 3
> : ^ ^^ ^^^^^
>x : boolean
> : ^^^^^^^

return (x ? (1) : 2);
>(x ? (1) : 2) : 1 | 2
> : ^^^^^
>x ? (1) : 2 : 1 | 2
> : ^^^^^
>x : boolean
> : ^^^^^^^
>(1) : 1
> : ^
>1 : 1
> : ^
>2 : 2
> : ^
}

declare function getAny(): any;
>getAny : () => any
> : ^^^^^^

function return2(x: string): string {
>return2 : (x: string) => string
> : ^ ^^ ^^^^^
>x : string
> : ^^^^^^

return x.startsWith("a") ? getAny() : 1;
>x.startsWith("a") ? getAny() : 1 : any
> : ^^^
>x.startsWith("a") : boolean
> : ^^^^^^^
>x.startsWith : (searchString: string, position?: number) => boolean
> : ^ ^^ ^^ ^^^ ^^^^^
>x : string
> : ^^^^^^
>startsWith : (searchString: string, position?: number) => boolean
> : ^ ^^ ^^ ^^^ ^^^^^
>"a" : "a"
> : ^^^
>getAny() : any
> : ^^^
>getAny : () => any
> : ^^^^^^
>1 : 1
> : ^
}

function return3(x: string): string {
>return3 : (x: string) => string
> : ^ ^^ ^^^^^
>x : string
> : ^^^^^^

return x.startsWith("a") ? "a" : x;
>x.startsWith("a") ? "a" : x : string
> : ^^^^^^
>x.startsWith("a") : boolean
> : ^^^^^^^
>x.startsWith : (searchString: string, position?: number) => boolean
> : ^ ^^ ^^ ^^^ ^^^^^
>x : string
> : ^^^^^^
>startsWith : (searchString: string, position?: number) => boolean
> : ^ ^^ ^^ ^^^ ^^^^^
>"a" : "a"
> : ^^^
>"a" : "a"
> : ^^^
>x : string
> : ^^^^^^
}

function return4(x: string): string {
>return4 : (x: string) => string
> : ^ ^^ ^^^^^
>x : string
> : ^^^^^^

return (x.startsWith("a") ? getAny() : 1) as string;
>(x.startsWith("a") ? getAny() : 1) as string : string
> : ^^^^^^
>(x.startsWith("a") ? getAny() : 1) : any
> : ^^^
>x.startsWith("a") ? getAny() : 1 : any
> : ^^^
>x.startsWith("a") : boolean
> : ^^^^^^^
>x.startsWith : (searchString: string, position?: number) => boolean
> : ^ ^^ ^^ ^^^ ^^^^^
>x : string
> : ^^^^^^
>startsWith : (searchString: string, position?: number) => boolean
> : ^ ^^ ^^ ^^^ ^^^^^
>"a" : "a"
> : ^^^
>getAny() : any
> : ^^^
>getAny : () => any
> : ^^^^^^
>1 : 1
> : ^
}

const return5 = (x: string): string => x.startsWith("a") ? getAny() : 1;
>return5 : (x: string) => string
> : ^ ^^ ^^^^^
>(x: string): string => x.startsWith("a") ? getAny() : 1 : (x: string) => string
> : ^ ^^ ^^^^^
>x : string
> : ^^^^^^
>x.startsWith("a") ? getAny() : 1 : any
> : ^^^
>x.startsWith("a") : boolean
> : ^^^^^^^
>x.startsWith : (searchString: string, position?: number) => boolean
> : ^ ^^ ^^ ^^^ ^^^^^
>x : string
> : ^^^^^^
>startsWith : (searchString: string, position?: number) => boolean
> : ^ ^^ ^^ ^^^ ^^^^^
>"a" : "a"
> : ^^^
>getAny() : any
> : ^^^
>getAny : () => any
> : ^^^^^^
>1 : 1
> : ^

const return6 = (x: string): string => (x.startsWith("a") ? getAny() : 1) as string;
>return6 : (x: string) => string
> : ^ ^^ ^^^^^
>(x: string): string => (x.startsWith("a") ? getAny() : 1) as string : (x: string) => string
> : ^ ^^ ^^^^^
>x : string
> : ^^^^^^
>(x.startsWith("a") ? getAny() : 1) as string : string
> : ^^^^^^
>(x.startsWith("a") ? getAny() : 1) : any
> : ^^^
>x.startsWith("a") ? getAny() : 1 : any
> : ^^^
>x.startsWith("a") : boolean
> : ^^^^^^^
>x.startsWith : (searchString: string, position?: number) => boolean
> : ^ ^^ ^^ ^^^ ^^^^^
>x : string
> : ^^^^^^
>startsWith : (searchString: string, position?: number) => boolean
> : ^ ^^ ^^ ^^^ ^^^^^
>"a" : "a"
> : ^^^
>getAny() : any
> : ^^^
>getAny : () => any
> : ^^^^^^
>1 : 1
> : ^

Loading