-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Fix #24193: Allow nullable discriminants #27631
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
Closed
jack-williams
wants to merge
1
commit into
microsoft:master
from
jack-williams:nullable-discriminant
Closed
Changes from all commits
Commits
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
118 changes: 118 additions & 0 deletions
118
tests/baselines/reference/nullableDiscriminant.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,118 @@ | ||
tests/cases/compiler/nullableDiscriminant.ts(62,25): error TS2339: Property 'message' does not exist on type 'ErrorOrSuccess<T>'. | ||
Property 'message' does not exist on type '{ value: T; }'. | ||
tests/cases/compiler/nullableDiscriminant.ts(90,13): error TS2339: Property 'bar' does not exist on type 'A | C'. | ||
Property 'bar' does not exist on type 'C'. | ||
|
||
|
||
==== tests/cases/compiler/nullableDiscriminant.ts (2 errors) ==== | ||
// Repro. from #24193 | ||
|
||
interface WithError { | ||
error: Error | ||
data: null | ||
} | ||
|
||
interface WithoutError<Data> { | ||
error: null | ||
data: Data | ||
} | ||
|
||
type DataCarrier<Data> = WithError | WithoutError<Data> | ||
|
||
function test<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 | ||
} | ||
} | ||
|
||
// Repro. from #24479 | ||
|
||
export interface Errored { | ||
error: Error | ||
value: null | ||
} | ||
|
||
export interface Succeeded<Value> { | ||
error: null | ||
value: Value | ||
} | ||
|
||
type Result<T> = Succeeded<T> | Errored; | ||
|
||
declare function getVal<T>(x :T): Result<T> | ||
|
||
let x = getVal("hello"); | ||
|
||
if (x.error === null) { | ||
x.value.toUpperCase(); | ||
} | ||
|
||
type ErrorOrSuccess<T> = | { value: null, message: string } | { value: T }; | ||
|
||
declare function getErrorOrSuccess<T>(x :T): ErrorOrSuccess<T> | ||
let y = getErrorOrSuccess({y: "foo"}); | ||
|
||
if(y.value === null) { | ||
// ok | ||
"message: " + y.message; | ||
} | ||
|
||
function genericNarrow<T>(x: T): true { | ||
let y = getErrorOrSuccess(x); | ||
if(y.value === null) { | ||
// not ok because T could be null | ||
"message: " + y.message; | ||
~~~~~~~ | ||
!!! error TS2339: Property 'message' does not exist on type 'ErrorOrSuccess<T>'. | ||
!!! error TS2339: Property 'message' does not exist on type '{ value: T; }'. | ||
} | ||
return true; | ||
} | ||
|
||
interface A { | ||
f?: number; | ||
bar: string; | ||
} | ||
|
||
interface B { | ||
f: number; | ||
} | ||
|
||
declare const aOrB: A | B; | ||
if (aOrB.f === undefined) { | ||
// ok | ||
aOrB.bar | ||
} | ||
|
||
interface C { | ||
f: null; | ||
baz: string; | ||
} | ||
|
||
declare const aOrBorC: A | B | C; | ||
if (aOrBorC.f == null) { | ||
// not ok | ||
aOrBorC.bar | ||
~~~ | ||
!!! error TS2339: Property 'bar' does not exist on type 'A | C'. | ||
!!! error TS2339: Property 'bar' does not exist on type 'C'. | ||
} | ||
|
||
if (aOrBorC.f === null) { | ||
// ok | ||
aOrBorC.baz | ||
} | ||
|
||
if (aOrBorC.f === undefined) { | ||
// ok | ||
aOrBorC.bar | ||
} | ||
|
||
|
||
|
||
|
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,154 @@ | ||
//// [nullableDiscriminant.ts] | ||
// Repro. from #24193 | ||
|
||
interface WithError { | ||
error: Error | ||
data: null | ||
} | ||
|
||
interface WithoutError<Data> { | ||
error: null | ||
data: Data | ||
} | ||
|
||
type DataCarrier<Data> = WithError | WithoutError<Data> | ||
|
||
function test<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 | ||
} | ||
} | ||
|
||
// Repro. from #24479 | ||
|
||
export interface Errored { | ||
error: Error | ||
value: null | ||
} | ||
|
||
export interface Succeeded<Value> { | ||
error: null | ||
value: Value | ||
} | ||
|
||
type Result<T> = Succeeded<T> | Errored; | ||
|
||
declare function getVal<T>(x :T): Result<T> | ||
|
||
let x = getVal("hello"); | ||
|
||
if (x.error === null) { | ||
x.value.toUpperCase(); | ||
} | ||
|
||
type ErrorOrSuccess<T> = | { value: null, message: string } | { value: T }; | ||
|
||
declare function getErrorOrSuccess<T>(x :T): ErrorOrSuccess<T> | ||
let y = getErrorOrSuccess({y: "foo"}); | ||
|
||
if(y.value === null) { | ||
// ok | ||
"message: " + y.message; | ||
} | ||
|
||
function genericNarrow<T>(x: T): true { | ||
let y = getErrorOrSuccess(x); | ||
if(y.value === null) { | ||
// not ok because T could be null | ||
"message: " + y.message; | ||
} | ||
return true; | ||
} | ||
|
||
interface A { | ||
f?: number; | ||
bar: string; | ||
} | ||
|
||
interface B { | ||
f: number; | ||
} | ||
|
||
declare const aOrB: A | B; | ||
if (aOrB.f === undefined) { | ||
// ok | ||
aOrB.bar | ||
} | ||
|
||
interface C { | ||
f: null; | ||
baz: string; | ||
} | ||
|
||
declare const aOrBorC: A | B | C; | ||
if (aOrBorC.f == null) { | ||
// not ok | ||
aOrBorC.bar | ||
} | ||
|
||
if (aOrBorC.f === null) { | ||
// ok | ||
aOrBorC.baz | ||
} | ||
|
||
if (aOrBorC.f === undefined) { | ||
// ok | ||
aOrBorC.bar | ||
} | ||
|
||
|
||
|
||
|
||
|
||
//// [nullableDiscriminant.js] | ||
"use strict"; | ||
// Repro. from #24193 | ||
exports.__esModule = true; | ||
function test(carrier) { | ||
if (carrier.error === null) { | ||
var error = carrier.error; | ||
var data = carrier.data; | ||
} | ||
else { | ||
var error = carrier.error; | ||
var data = carrier.data; | ||
} | ||
} | ||
var x = getVal("hello"); | ||
if (x.error === null) { | ||
x.value.toUpperCase(); | ||
} | ||
var y = getErrorOrSuccess({ y: "foo" }); | ||
if (y.value === null) { | ||
// ok | ||
"message: " + y.message; | ||
} | ||
function genericNarrow(x) { | ||
var y = getErrorOrSuccess(x); | ||
if (y.value === null) { | ||
// not ok because T could be null | ||
"message: " + y.message; | ||
} | ||
return true; | ||
} | ||
if (aOrB.f === undefined) { | ||
// ok | ||
aOrB.bar; | ||
} | ||
if (aOrBorC.f == null) { | ||
// not ok | ||
aOrBorC.bar; | ||
} | ||
if (aOrBorC.f === null) { | ||
// ok | ||
aOrBorC.baz; | ||
} | ||
if (aOrBorC.f === undefined) { | ||
// ok | ||
aOrBorC.bar; | ||
} |
Oops, something went wrong.
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.
As a signpost for @ahejlsberg, I wasn't sure about this flag combination
(TypeFlags.DisjointDomains | TypeFlags.Object)
. I was mostly concerned about avoiding unions that had a generic parameter that might be instantiated to something nullable, but felt it was safer to be positive about what is accepted, rather than negative about what is not. I also considered whether it would be just sufficient to check the union has at least one nullable and at least one known non-nullable.