-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Allow non-unit types in union discriminants #27695
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
Merged
+775
−7
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d069875
Allow discriminant property to contain some non-unit types
ahejlsberg c110f57
Discriminant must include at least one unit type and no instantiable …
ahejlsberg 7737cd5
Accept new baselines
ahejlsberg 22907bf
Add tests
ahejlsberg 6cdba6d
Accept new baselines
ahejlsberg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
tests/baselines/reference/discriminatedUnionTypes2.errors.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
tests/cases/conformance/types/union/discriminatedUnionTypes2.ts(27,30): error TS2322: Type '{ a: null; b: string; c: number; }' is not assignable to type '{ a: null; b: string; } | { a: string; c: number; }'. | ||
Object literal may only specify known properties, and 'c' does not exist in type '{ a: null; b: string; }'. | ||
tests/cases/conformance/types/union/discriminatedUnionTypes2.ts(32,11): error TS2339: Property 'b' does not exist on type '{ a: 0; b: string; } | { a: T; c: number; }'. | ||
Property 'b' does not exist on type '{ a: T; c: number; }'. | ||
|
||
|
||
==== tests/cases/conformance/types/union/discriminatedUnionTypes2.ts (2 errors) ==== | ||
function f10(x : { kind: false, a: string } | { kind: true, b: string } | { kind: string, c: string }) { | ||
if (x.kind === false) { | ||
x.a; | ||
} | ||
else if (x.kind === true) { | ||
x.b; | ||
} | ||
else { | ||
x.c; | ||
} | ||
} | ||
|
||
function f11(x : { kind: false, a: string } | { kind: true, b: string } | { kind: string, c: string }) { | ||
switch (x.kind) { | ||
case false: | ||
x.a; | ||
break; | ||
case true: | ||
x.b; | ||
break; | ||
default: | ||
x.c; | ||
} | ||
} | ||
|
||
function f13(x: { a: null; b: string } | { a: string, c: number }) { | ||
x = { a: null, b: "foo", c: 4}; // Error | ||
~~~~ | ||
!!! error TS2322: Type '{ a: null; b: string; c: number; }' is not assignable to type '{ a: null; b: string; } | { a: string; c: number; }'. | ||
!!! error TS2322: Object literal may only specify known properties, and 'c' does not exist in type '{ a: null; b: string; }'. | ||
} | ||
|
||
function f14<T>(x: { a: 0; b: string } | { a: T, c: number }) { | ||
if (x.a === 0) { | ||
x.b; // Error | ||
~ | ||
!!! error TS2339: Property 'b' does not exist on type '{ a: 0; b: string; } | { a: T; c: number; }'. | ||
!!! error TS2339: Property 'b' does not exist on type '{ a: T; c: number; }'. | ||
} | ||
} | ||
|
||
type Result<T> = { error?: undefined, value: T } | { error: Error }; | ||
|
||
function f15(x: Result<number>) { | ||
if (!x.error) { | ||
x.value; | ||
} | ||
else { | ||
x.error.message; | ||
} | ||
} | ||
|
||
f15({ value: 10 }); | ||
f15({ error: new Error("boom") }); | ||
|
||
// Repro from #24193 | ||
|
||
interface WithError { | ||
error: Error | ||
data: null | ||
} | ||
|
||
interface WithoutError<Data> { | ||
error: null | ||
data: Data | ||
} | ||
|
||
type DataCarrier<Data> = WithError | WithoutError<Data> | ||
|
||
function f20<Data>(carrier: DataCarrier<Data>) { | ||
if (carrier.error === null) { | ||
const error: null = carrier.error | ||
const data: Data = carrier.data | ||
} else { | ||
const error: Error = carrier.error | ||
const data: null = carrier.data | ||
} | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
//// [discriminatedUnionTypes2.ts] | ||
function f10(x : { kind: false, a: string } | { kind: true, b: string } | { kind: string, c: string }) { | ||
if (x.kind === false) { | ||
x.a; | ||
} | ||
else if (x.kind === true) { | ||
x.b; | ||
} | ||
else { | ||
x.c; | ||
} | ||
} | ||
|
||
function f11(x : { kind: false, a: string } | { kind: true, b: string } | { kind: string, c: string }) { | ||
switch (x.kind) { | ||
case false: | ||
x.a; | ||
break; | ||
case true: | ||
x.b; | ||
break; | ||
default: | ||
x.c; | ||
} | ||
} | ||
|
||
function f13(x: { a: null; b: string } | { a: string, c: number }) { | ||
x = { a: null, b: "foo", c: 4}; // Error | ||
} | ||
|
||
function f14<T>(x: { a: 0; b: string } | { a: T, c: number }) { | ||
if (x.a === 0) { | ||
x.b; // Error | ||
} | ||
} | ||
|
||
type Result<T> = { error?: undefined, value: T } | { error: Error }; | ||
|
||
function f15(x: Result<number>) { | ||
if (!x.error) { | ||
x.value; | ||
} | ||
else { | ||
x.error.message; | ||
} | ||
} | ||
|
||
f15({ value: 10 }); | ||
f15({ error: new Error("boom") }); | ||
|
||
// Repro from #24193 | ||
|
||
interface WithError { | ||
error: Error | ||
data: null | ||
} | ||
|
||
interface WithoutError<Data> { | ||
error: null | ||
data: Data | ||
} | ||
|
||
type DataCarrier<Data> = WithError | WithoutError<Data> | ||
|
||
function f20<Data>(carrier: DataCarrier<Data>) { | ||
if (carrier.error === null) { | ||
const error: null = carrier.error | ||
const data: Data = carrier.data | ||
} else { | ||
const error: Error = carrier.error | ||
const data: null = carrier.data | ||
} | ||
} | ||
|
||
|
||
//// [discriminatedUnionTypes2.js] | ||
"use strict"; | ||
function f10(x) { | ||
if (x.kind === false) { | ||
x.a; | ||
} | ||
else if (x.kind === true) { | ||
x.b; | ||
} | ||
else { | ||
x.c; | ||
} | ||
} | ||
function f11(x) { | ||
switch (x.kind) { | ||
case false: | ||
x.a; | ||
break; | ||
case true: | ||
x.b; | ||
break; | ||
default: | ||
x.c; | ||
} | ||
} | ||
function f13(x) { | ||
x = { a: null, b: "foo", c: 4 }; // Error | ||
} | ||
function f14(x) { | ||
if (x.a === 0) { | ||
x.b; // Error | ||
} | ||
} | ||
function f15(x) { | ||
if (!x.error) { | ||
x.value; | ||
} | ||
else { | ||
x.error.message; | ||
} | ||
} | ||
f15({ value: 10 }); | ||
f15({ error: new Error("boom") }); | ||
function f20(carrier) { | ||
if (carrier.error === null) { | ||
var error = carrier.error; | ||
var data = carrier.data; | ||
} | ||
else { | ||
var error = carrier.error; | ||
var data = carrier.data; | ||
} | ||
} |
227 changes: 227 additions & 0 deletions
227
tests/baselines/reference/discriminatedUnionTypes2.symbols
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,227 @@ | ||
=== tests/cases/conformance/types/union/discriminatedUnionTypes2.ts === | ||
function f10(x : { kind: false, a: string } | { kind: true, b: string } | { kind: string, c: string }) { | ||
>f10 : Symbol(f10, Decl(discriminatedUnionTypes2.ts, 0, 0)) | ||
>x : Symbol(x, Decl(discriminatedUnionTypes2.ts, 0, 13)) | ||
>kind : Symbol(kind, Decl(discriminatedUnionTypes2.ts, 0, 18)) | ||
>a : Symbol(a, Decl(discriminatedUnionTypes2.ts, 0, 31)) | ||
>kind : Symbol(kind, Decl(discriminatedUnionTypes2.ts, 0, 47)) | ||
>b : Symbol(b, Decl(discriminatedUnionTypes2.ts, 0, 59)) | ||
>kind : Symbol(kind, Decl(discriminatedUnionTypes2.ts, 0, 75)) | ||
>c : Symbol(c, Decl(discriminatedUnionTypes2.ts, 0, 89)) | ||
|
||
if (x.kind === false) { | ||
>x.kind : Symbol(kind, Decl(discriminatedUnionTypes2.ts, 0, 18), Decl(discriminatedUnionTypes2.ts, 0, 47), Decl(discriminatedUnionTypes2.ts, 0, 75)) | ||
>x : Symbol(x, Decl(discriminatedUnionTypes2.ts, 0, 13)) | ||
>kind : Symbol(kind, Decl(discriminatedUnionTypes2.ts, 0, 18), Decl(discriminatedUnionTypes2.ts, 0, 47), Decl(discriminatedUnionTypes2.ts, 0, 75)) | ||
|
||
x.a; | ||
>x.a : Symbol(a, Decl(discriminatedUnionTypes2.ts, 0, 31)) | ||
>x : Symbol(x, Decl(discriminatedUnionTypes2.ts, 0, 13)) | ||
>a : Symbol(a, Decl(discriminatedUnionTypes2.ts, 0, 31)) | ||
} | ||
else if (x.kind === true) { | ||
>x.kind : Symbol(kind, Decl(discriminatedUnionTypes2.ts, 0, 47), Decl(discriminatedUnionTypes2.ts, 0, 75)) | ||
>x : Symbol(x, Decl(discriminatedUnionTypes2.ts, 0, 13)) | ||
>kind : Symbol(kind, Decl(discriminatedUnionTypes2.ts, 0, 47), Decl(discriminatedUnionTypes2.ts, 0, 75)) | ||
|
||
x.b; | ||
>x.b : Symbol(b, Decl(discriminatedUnionTypes2.ts, 0, 59)) | ||
>x : Symbol(x, Decl(discriminatedUnionTypes2.ts, 0, 13)) | ||
>b : Symbol(b, Decl(discriminatedUnionTypes2.ts, 0, 59)) | ||
} | ||
else { | ||
x.c; | ||
>x.c : Symbol(c, Decl(discriminatedUnionTypes2.ts, 0, 89)) | ||
>x : Symbol(x, Decl(discriminatedUnionTypes2.ts, 0, 13)) | ||
>c : Symbol(c, Decl(discriminatedUnionTypes2.ts, 0, 89)) | ||
} | ||
} | ||
|
||
function f11(x : { kind: false, a: string } | { kind: true, b: string } | { kind: string, c: string }) { | ||
>f11 : Symbol(f11, Decl(discriminatedUnionTypes2.ts, 10, 1)) | ||
>x : Symbol(x, Decl(discriminatedUnionTypes2.ts, 12, 13)) | ||
>kind : Symbol(kind, Decl(discriminatedUnionTypes2.ts, 12, 18)) | ||
>a : Symbol(a, Decl(discriminatedUnionTypes2.ts, 12, 31)) | ||
>kind : Symbol(kind, Decl(discriminatedUnionTypes2.ts, 12, 47)) | ||
>b : Symbol(b, Decl(discriminatedUnionTypes2.ts, 12, 59)) | ||
>kind : Symbol(kind, Decl(discriminatedUnionTypes2.ts, 12, 75)) | ||
>c : Symbol(c, Decl(discriminatedUnionTypes2.ts, 12, 89)) | ||
|
||
switch (x.kind) { | ||
>x.kind : Symbol(kind, Decl(discriminatedUnionTypes2.ts, 12, 18), Decl(discriminatedUnionTypes2.ts, 12, 47), Decl(discriminatedUnionTypes2.ts, 12, 75)) | ||
>x : Symbol(x, Decl(discriminatedUnionTypes2.ts, 12, 13)) | ||
>kind : Symbol(kind, Decl(discriminatedUnionTypes2.ts, 12, 18), Decl(discriminatedUnionTypes2.ts, 12, 47), Decl(discriminatedUnionTypes2.ts, 12, 75)) | ||
|
||
case false: | ||
x.a; | ||
>x.a : Symbol(a, Decl(discriminatedUnionTypes2.ts, 12, 31)) | ||
>x : Symbol(x, Decl(discriminatedUnionTypes2.ts, 12, 13)) | ||
>a : Symbol(a, Decl(discriminatedUnionTypes2.ts, 12, 31)) | ||
|
||
break; | ||
case true: | ||
x.b; | ||
>x.b : Symbol(b, Decl(discriminatedUnionTypes2.ts, 12, 59)) | ||
>x : Symbol(x, Decl(discriminatedUnionTypes2.ts, 12, 13)) | ||
>b : Symbol(b, Decl(discriminatedUnionTypes2.ts, 12, 59)) | ||
|
||
break; | ||
default: | ||
x.c; | ||
>x.c : Symbol(c, Decl(discriminatedUnionTypes2.ts, 12, 89)) | ||
>x : Symbol(x, Decl(discriminatedUnionTypes2.ts, 12, 13)) | ||
>c : Symbol(c, Decl(discriminatedUnionTypes2.ts, 12, 89)) | ||
} | ||
} | ||
|
||
function f13(x: { a: null; b: string } | { a: string, c: number }) { | ||
>f13 : Symbol(f13, Decl(discriminatedUnionTypes2.ts, 23, 1)) | ||
>x : Symbol(x, Decl(discriminatedUnionTypes2.ts, 25, 13)) | ||
>a : Symbol(a, Decl(discriminatedUnionTypes2.ts, 25, 17)) | ||
>b : Symbol(b, Decl(discriminatedUnionTypes2.ts, 25, 26)) | ||
>a : Symbol(a, Decl(discriminatedUnionTypes2.ts, 25, 42)) | ||
>c : Symbol(c, Decl(discriminatedUnionTypes2.ts, 25, 53)) | ||
|
||
x = { a: null, b: "foo", c: 4}; // Error | ||
>x : Symbol(x, Decl(discriminatedUnionTypes2.ts, 25, 13)) | ||
>a : Symbol(a, Decl(discriminatedUnionTypes2.ts, 26, 9)) | ||
>b : Symbol(b, Decl(discriminatedUnionTypes2.ts, 26, 18)) | ||
>c : Symbol(c, Decl(discriminatedUnionTypes2.ts, 26, 28)) | ||
} | ||
|
||
function f14<T>(x: { a: 0; b: string } | { a: T, c: number }) { | ||
>f14 : Symbol(f14, Decl(discriminatedUnionTypes2.ts, 27, 1)) | ||
>T : Symbol(T, Decl(discriminatedUnionTypes2.ts, 29, 13)) | ||
>x : Symbol(x, Decl(discriminatedUnionTypes2.ts, 29, 16)) | ||
>a : Symbol(a, Decl(discriminatedUnionTypes2.ts, 29, 20)) | ||
>b : Symbol(b, Decl(discriminatedUnionTypes2.ts, 29, 26)) | ||
>a : Symbol(a, Decl(discriminatedUnionTypes2.ts, 29, 42)) | ||
>T : Symbol(T, Decl(discriminatedUnionTypes2.ts, 29, 13)) | ||
>c : Symbol(c, Decl(discriminatedUnionTypes2.ts, 29, 48)) | ||
|
||
if (x.a === 0) { | ||
>x.a : Symbol(a, Decl(discriminatedUnionTypes2.ts, 29, 20), Decl(discriminatedUnionTypes2.ts, 29, 42)) | ||
>x : Symbol(x, Decl(discriminatedUnionTypes2.ts, 29, 16)) | ||
>a : Symbol(a, Decl(discriminatedUnionTypes2.ts, 29, 20), Decl(discriminatedUnionTypes2.ts, 29, 42)) | ||
|
||
x.b; // Error | ||
>x : Symbol(x, Decl(discriminatedUnionTypes2.ts, 29, 16)) | ||
} | ||
} | ||
|
||
type Result<T> = { error?: undefined, value: T } | { error: Error }; | ||
>Result : Symbol(Result, Decl(discriminatedUnionTypes2.ts, 33, 1)) | ||
>T : Symbol(T, Decl(discriminatedUnionTypes2.ts, 35, 12)) | ||
>error : Symbol(error, Decl(discriminatedUnionTypes2.ts, 35, 18)) | ||
>value : Symbol(value, Decl(discriminatedUnionTypes2.ts, 35, 37)) | ||
>T : Symbol(T, Decl(discriminatedUnionTypes2.ts, 35, 12)) | ||
>error : Symbol(error, Decl(discriminatedUnionTypes2.ts, 35, 52)) | ||
>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) | ||
|
||
function f15(x: Result<number>) { | ||
>f15 : Symbol(f15, Decl(discriminatedUnionTypes2.ts, 35, 68)) | ||
>x : Symbol(x, Decl(discriminatedUnionTypes2.ts, 37, 13)) | ||
>Result : Symbol(Result, Decl(discriminatedUnionTypes2.ts, 33, 1)) | ||
|
||
if (!x.error) { | ||
>x.error : Symbol(error, Decl(discriminatedUnionTypes2.ts, 35, 52), Decl(discriminatedUnionTypes2.ts, 35, 18)) | ||
>x : Symbol(x, Decl(discriminatedUnionTypes2.ts, 37, 13)) | ||
>error : Symbol(error, Decl(discriminatedUnionTypes2.ts, 35, 52), Decl(discriminatedUnionTypes2.ts, 35, 18)) | ||
|
||
x.value; | ||
>x.value : Symbol(value, Decl(discriminatedUnionTypes2.ts, 35, 37)) | ||
>x : Symbol(x, Decl(discriminatedUnionTypes2.ts, 37, 13)) | ||
>value : Symbol(value, Decl(discriminatedUnionTypes2.ts, 35, 37)) | ||
} | ||
else { | ||
x.error.message; | ||
>x.error.message : Symbol(Error.message, Decl(lib.es5.d.ts, --, --)) | ||
>x.error : Symbol(error, Decl(discriminatedUnionTypes2.ts, 35, 52)) | ||
>x : Symbol(x, Decl(discriminatedUnionTypes2.ts, 37, 13)) | ||
>error : Symbol(error, Decl(discriminatedUnionTypes2.ts, 35, 52)) | ||
>message : Symbol(Error.message, Decl(lib.es5.d.ts, --, --)) | ||
} | ||
} | ||
|
||
f15({ value: 10 }); | ||
>f15 : Symbol(f15, Decl(discriminatedUnionTypes2.ts, 35, 68)) | ||
>value : Symbol(value, Decl(discriminatedUnionTypes2.ts, 46, 5)) | ||
|
||
f15({ error: new Error("boom") }); | ||
>f15 : Symbol(f15, Decl(discriminatedUnionTypes2.ts, 35, 68)) | ||
>error : Symbol(error, Decl(discriminatedUnionTypes2.ts, 47, 5)) | ||
>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) | ||
|
||
// Repro from #24193 | ||
|
||
interface WithError { | ||
>WithError : Symbol(WithError, Decl(discriminatedUnionTypes2.ts, 47, 34)) | ||
|
||
error: Error | ||
>error : Symbol(WithError.error, Decl(discriminatedUnionTypes2.ts, 51, 21)) | ||
>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) | ||
|
||
data: null | ||
>data : Symbol(WithError.data, Decl(discriminatedUnionTypes2.ts, 52, 16)) | ||
} | ||
|
||
interface WithoutError<Data> { | ||
>WithoutError : Symbol(WithoutError, Decl(discriminatedUnionTypes2.ts, 54, 1)) | ||
>Data : Symbol(Data, Decl(discriminatedUnionTypes2.ts, 56, 23)) | ||
|
||
error: null | ||
>error : Symbol(WithoutError.error, Decl(discriminatedUnionTypes2.ts, 56, 30)) | ||
|
||
data: Data | ||
>data : Symbol(WithoutError.data, Decl(discriminatedUnionTypes2.ts, 57, 15)) | ||
>Data : Symbol(Data, Decl(discriminatedUnionTypes2.ts, 56, 23)) | ||
} | ||
|
||
type DataCarrier<Data> = WithError | WithoutError<Data> | ||
>DataCarrier : Symbol(DataCarrier, Decl(discriminatedUnionTypes2.ts, 59, 1)) | ||
>Data : Symbol(Data, Decl(discriminatedUnionTypes2.ts, 61, 17)) | ||
>WithError : Symbol(WithError, Decl(discriminatedUnionTypes2.ts, 47, 34)) | ||
>WithoutError : Symbol(WithoutError, Decl(discriminatedUnionTypes2.ts, 54, 1)) | ||
>Data : Symbol(Data, Decl(discriminatedUnionTypes2.ts, 61, 17)) | ||
|
||
function f20<Data>(carrier: DataCarrier<Data>) { | ||
>f20 : Symbol(f20, Decl(discriminatedUnionTypes2.ts, 61, 55)) | ||
>Data : Symbol(Data, Decl(discriminatedUnionTypes2.ts, 63, 13)) | ||
>carrier : Symbol(carrier, Decl(discriminatedUnionTypes2.ts, 63, 19)) | ||
>DataCarrier : Symbol(DataCarrier, Decl(discriminatedUnionTypes2.ts, 59, 1)) | ||
>Data : Symbol(Data, Decl(discriminatedUnionTypes2.ts, 63, 13)) | ||
|
||
if (carrier.error === null) { | ||
>carrier.error : Symbol(error, Decl(discriminatedUnionTypes2.ts, 51, 21), Decl(discriminatedUnionTypes2.ts, 56, 30)) | ||
>carrier : Symbol(carrier, Decl(discriminatedUnionTypes2.ts, 63, 19)) | ||
>error : Symbol(error, Decl(discriminatedUnionTypes2.ts, 51, 21), Decl(discriminatedUnionTypes2.ts, 56, 30)) | ||
|
||
const error: null = carrier.error | ||
>error : Symbol(error, Decl(discriminatedUnionTypes2.ts, 65, 13)) | ||
>carrier.error : Symbol(WithoutError.error, Decl(discriminatedUnionTypes2.ts, 56, 30)) | ||
>carrier : Symbol(carrier, Decl(discriminatedUnionTypes2.ts, 63, 19)) | ||
>error : Symbol(WithoutError.error, Decl(discriminatedUnionTypes2.ts, 56, 30)) | ||
|
||
const data: Data = carrier.data | ||
>data : Symbol(data, Decl(discriminatedUnionTypes2.ts, 66, 13)) | ||
>Data : Symbol(Data, Decl(discriminatedUnionTypes2.ts, 63, 13)) | ||
>carrier.data : Symbol(WithoutError.data, Decl(discriminatedUnionTypes2.ts, 57, 15)) | ||
>carrier : Symbol(carrier, Decl(discriminatedUnionTypes2.ts, 63, 19)) | ||
>data : Symbol(WithoutError.data, Decl(discriminatedUnionTypes2.ts, 57, 15)) | ||
|
||
} else { | ||
const error: Error = carrier.error | ||
>error : Symbol(error, Decl(discriminatedUnionTypes2.ts, 68, 13)) | ||
>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) | ||
>carrier.error : Symbol(WithError.error, Decl(discriminatedUnionTypes2.ts, 51, 21)) | ||
>carrier : Symbol(carrier, Decl(discriminatedUnionTypes2.ts, 63, 19)) | ||
>error : Symbol(WithError.error, Decl(discriminatedUnionTypes2.ts, 51, 21)) | ||
|
||
const data: null = carrier.data | ||
>data : Symbol(data, Decl(discriminatedUnionTypes2.ts, 69, 13)) | ||
>carrier.data : Symbol(WithError.data, Decl(discriminatedUnionTypes2.ts, 52, 16)) | ||
>carrier : Symbol(carrier, Decl(discriminatedUnionTypes2.ts, 63, 19)) | ||
>data : Symbol(WithError.data, Decl(discriminatedUnionTypes2.ts, 52, 16)) | ||
} | ||
} | ||
|
241 changes: 241 additions & 0 deletions
241
tests/baselines/reference/discriminatedUnionTypes2.types
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,241 @@ | ||
=== tests/cases/conformance/types/union/discriminatedUnionTypes2.ts === | ||
function f10(x : { kind: false, a: string } | { kind: true, b: string } | { kind: string, c: string }) { | ||
>f10 : (x: { kind: false; a: string; } | { kind: true; b: string; } | { kind: string; c: string; }) => void | ||
>x : { kind: false; a: string; } | { kind: true; b: string; } | { kind: string; c: string; } | ||
>kind : false | ||
>false : false | ||
>a : string | ||
>kind : true | ||
>true : true | ||
>b : string | ||
>kind : string | ||
>c : string | ||
|
||
if (x.kind === false) { | ||
>x.kind === false : boolean | ||
>x.kind : string | boolean | ||
>x : { kind: false; a: string; } | { kind: true; b: string; } | { kind: string; c: string; } | ||
>kind : string | boolean | ||
>false : false | ||
|
||
x.a; | ||
>x.a : string | ||
>x : { kind: false; a: string; } | ||
>a : string | ||
} | ||
else if (x.kind === true) { | ||
>x.kind === true : boolean | ||
>x.kind : string | true | ||
>x : { kind: true; b: string; } | { kind: string; c: string; } | ||
>kind : string | true | ||
>true : true | ||
|
||
x.b; | ||
>x.b : string | ||
>x : { kind: true; b: string; } | ||
>b : string | ||
} | ||
else { | ||
x.c; | ||
>x.c : string | ||
>x : { kind: string; c: string; } | ||
>c : string | ||
} | ||
} | ||
|
||
function f11(x : { kind: false, a: string } | { kind: true, b: string } | { kind: string, c: string }) { | ||
>f11 : (x: { kind: false; a: string; } | { kind: true; b: string; } | { kind: string; c: string; }) => void | ||
>x : { kind: false; a: string; } | { kind: true; b: string; } | { kind: string; c: string; } | ||
>kind : false | ||
>false : false | ||
>a : string | ||
>kind : true | ||
>true : true | ||
>b : string | ||
>kind : string | ||
>c : string | ||
|
||
switch (x.kind) { | ||
>x.kind : string | boolean | ||
>x : { kind: false; a: string; } | { kind: true; b: string; } | { kind: string; c: string; } | ||
>kind : string | boolean | ||
|
||
case false: | ||
>false : false | ||
|
||
x.a; | ||
>x.a : string | ||
>x : { kind: false; a: string; } | ||
>a : string | ||
|
||
break; | ||
case true: | ||
>true : true | ||
|
||
x.b; | ||
>x.b : string | ||
>x : { kind: true; b: string; } | ||
>b : string | ||
|
||
break; | ||
default: | ||
x.c; | ||
>x.c : string | ||
>x : { kind: string; c: string; } | ||
>c : string | ||
} | ||
} | ||
|
||
function f13(x: { a: null; b: string } | { a: string, c: number }) { | ||
>f13 : (x: { a: null; b: string; } | { a: string; c: number; }) => void | ||
>x : { a: null; b: string; } | { a: string; c: number; } | ||
>a : null | ||
>null : null | ||
>b : string | ||
>a : string | ||
>c : number | ||
|
||
x = { a: null, b: "foo", c: 4}; // Error | ||
>x = { a: null, b: "foo", c: 4} : { a: null; b: string; c: number; } | ||
>x : { a: null; b: string; } | { a: string; c: number; } | ||
>{ a: null, b: "foo", c: 4} : { a: null; b: string; c: number; } | ||
>a : null | ||
>null : null | ||
>b : string | ||
>"foo" : "foo" | ||
>c : number | ||
>4 : 4 | ||
} | ||
|
||
function f14<T>(x: { a: 0; b: string } | { a: T, c: number }) { | ||
>f14 : <T>(x: { a: 0; b: string; } | { a: T; c: number; }) => void | ||
>x : { a: 0; b: string; } | { a: T; c: number; } | ||
>a : 0 | ||
>b : string | ||
>a : T | ||
>c : number | ||
|
||
if (x.a === 0) { | ||
>x.a === 0 : boolean | ||
>x.a : 0 | T | ||
>x : { a: 0; b: string; } | { a: T; c: number; } | ||
>a : 0 | T | ||
>0 : 0 | ||
|
||
x.b; // Error | ||
>x.b : any | ||
>x : { a: 0; b: string; } | { a: T; c: number; } | ||
>b : any | ||
} | ||
} | ||
|
||
type Result<T> = { error?: undefined, value: T } | { error: Error }; | ||
>Result : Result<T> | ||
>error : undefined | ||
>value : T | ||
>error : Error | ||
|
||
function f15(x: Result<number>) { | ||
>f15 : (x: Result<number>) => void | ||
>x : Result<number> | ||
|
||
if (!x.error) { | ||
>!x.error : boolean | ||
>x.error : Error | undefined | ||
>x : Result<number> | ||
>error : Error | undefined | ||
|
||
x.value; | ||
>x.value : number | ||
>x : { error?: undefined; value: number; } | ||
>value : number | ||
} | ||
else { | ||
x.error.message; | ||
>x.error.message : string | ||
>x.error : Error | ||
>x : { error: Error; } | ||
>error : Error | ||
>message : string | ||
} | ||
} | ||
|
||
f15({ value: 10 }); | ||
>f15({ value: 10 }) : void | ||
>f15 : (x: Result<number>) => void | ||
>{ value: 10 } : { value: number; } | ||
>value : number | ||
>10 : 10 | ||
|
||
f15({ error: new Error("boom") }); | ||
>f15({ error: new Error("boom") }) : void | ||
>f15 : (x: Result<number>) => void | ||
>{ error: new Error("boom") } : { error: Error; } | ||
>error : Error | ||
>new Error("boom") : Error | ||
>Error : ErrorConstructor | ||
>"boom" : "boom" | ||
|
||
// Repro from #24193 | ||
|
||
interface WithError { | ||
error: Error | ||
>error : Error | ||
|
||
data: null | ||
>data : null | ||
>null : null | ||
} | ||
|
||
interface WithoutError<Data> { | ||
error: null | ||
>error : null | ||
>null : null | ||
|
||
data: Data | ||
>data : Data | ||
} | ||
|
||
type DataCarrier<Data> = WithError | WithoutError<Data> | ||
>DataCarrier : DataCarrier<Data> | ||
|
||
function f20<Data>(carrier: DataCarrier<Data>) { | ||
>f20 : <Data>(carrier: DataCarrier<Data>) => void | ||
>carrier : DataCarrier<Data> | ||
|
||
if (carrier.error === null) { | ||
>carrier.error === null : boolean | ||
>carrier.error : Error | null | ||
>carrier : DataCarrier<Data> | ||
>error : Error | null | ||
>null : null | ||
|
||
const error: null = carrier.error | ||
>error : null | ||
>null : null | ||
>carrier.error : null | ||
>carrier : WithoutError<Data> | ||
>error : null | ||
|
||
const data: Data = carrier.data | ||
>data : Data | ||
>carrier.data : Data | ||
>carrier : WithoutError<Data> | ||
>data : Data | ||
|
||
} else { | ||
const error: Error = carrier.error | ||
>error : Error | ||
>carrier.error : Error | ||
>carrier : WithError | ||
>error : Error | ||
|
||
const data: null = carrier.data | ||
>data : null | ||
>null : null | ||
>carrier.data : null | ||
>carrier : WithError | ||
>data : null | ||
} | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
tests/cases/conformance/types/union/discriminatedUnionTypes2.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// @strict: true | ||
|
||
function f10(x : { kind: false, a: string } | { kind: true, b: string } | { kind: string, c: string }) { | ||
if (x.kind === false) { | ||
x.a; | ||
} | ||
else if (x.kind === true) { | ||
x.b; | ||
} | ||
else { | ||
x.c; | ||
} | ||
} | ||
|
||
function f11(x : { kind: false, a: string } | { kind: true, b: string } | { kind: string, c: string }) { | ||
switch (x.kind) { | ||
case false: | ||
x.a; | ||
break; | ||
case true: | ||
x.b; | ||
break; | ||
default: | ||
x.c; | ||
} | ||
} | ||
|
||
function f13(x: { a: null; b: string } | { a: string, c: number }) { | ||
x = { a: null, b: "foo", c: 4}; // Error | ||
} | ||
|
||
function f14<T>(x: { a: 0; b: string } | { a: T, c: number }) { | ||
if (x.a === 0) { | ||
x.b; // Error | ||
} | ||
} | ||
|
||
type Result<T> = { error?: undefined, value: T } | { error: Error }; | ||
|
||
function f15(x: Result<number>) { | ||
if (!x.error) { | ||
x.value; | ||
} | ||
else { | ||
x.error.message; | ||
} | ||
} | ||
|
||
f15({ value: 10 }); | ||
f15({ error: new Error("boom") }); | ||
|
||
// Repro from #24193 | ||
|
||
interface WithError { | ||
error: Error | ||
data: null | ||
} | ||
|
||
interface WithoutError<Data> { | ||
error: null | ||
data: Data | ||
} | ||
|
||
type DataCarrier<Data> = WithError | WithoutError<Data> | ||
|
||
function f20<Data>(carrier: DataCarrier<Data>) { | ||
if (carrier.error === null) { | ||
const error: null = carrier.error | ||
const data: Data = carrier.data | ||
} else { | ||
const error: Error = carrier.error | ||
const data: null = carrier.data | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could it be worth adding a test that includes constrained generics? My reasoning being that in the future it might be worth considering discriminants that have constrained parameters because they are comparable to literals in some form. Having a test that would flag any changes might be helpful. Something like: