diff --git a/tests/baselines/reference/conditionalReturnExpression.errors.txt b/tests/baselines/reference/conditionalReturnExpression.errors.txt new file mode 100644 index 00000000000..52dba11d81e --- /dev/null +++ b/tests/baselines/reference/conditionalReturnExpression.errors.txt @@ -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; \ No newline at end of file diff --git a/tests/baselines/reference/conditionalReturnExpression.symbols b/tests/baselines/reference/conditionalReturnExpression.symbols new file mode 100644 index 00000000000..266619261ad --- /dev/null +++ b/tests/baselines/reference/conditionalReturnExpression.symbols @@ -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)) + diff --git a/tests/baselines/reference/conditionalReturnExpression.types b/tests/baselines/reference/conditionalReturnExpression.types new file mode 100644 index 00000000000..2dc26aa8140 --- /dev/null +++ b/tests/baselines/reference/conditionalReturnExpression.types @@ -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 +> : ^ + diff --git a/tests/cases/compiler/conditionalReturnExpression.ts b/tests/cases/compiler/conditionalReturnExpression.ts new file mode 100644 index 00000000000..8c1aefe8d5e --- /dev/null +++ b/tests/cases/compiler/conditionalReturnExpression.ts @@ -0,0 +1,24 @@ +// @noEmit: true +// @target: esnext + +function return1(x: boolean): 3 { + return (x ? (1) : 2); +} + +declare function getAny(): any; + +function return2(x: string): string { + return x.startsWith("a") ? getAny() : 1; +} + +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; + +const return6 = (x: string): string => (x.startsWith("a") ? getAny() : 1) as string; \ No newline at end of file