Skip to content

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
Closed
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
22 changes: 21 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12919,6 +12919,24 @@ namespace ts {
isUnitType(type);
}

function isNullableDiscriminable(type: Type): boolean {
if (type.flags & TypeFlags.Union) {
let hasDiscriminant = false;
for (const current of (<UnionType>type).types) {
if (current.flags & TypeFlags.Nullable) {
hasDiscriminant = true;
continue;
}
else if (current.flags & (TypeFlags.DisjointDomains | TypeFlags.Object)) {
Copy link
Collaborator Author

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.

continue;
}
return false;
}
return hasDiscriminant;
}
return false;
}

function getBaseTypeOfLiteralType(type: Type): Type {
return type.flags & TypeFlags.EnumLiteral ? getBaseTypeOfEnumLiteralType(<LiteralType>type) :
type.flags & TypeFlags.StringLiteral ? stringType :
Expand Down Expand Up @@ -14217,7 +14235,9 @@ namespace ts {
const prop = getUnionOrIntersectionProperty(<UnionType>type, name);
if (prop && getCheckFlags(prop) & CheckFlags.SyntheticProperty) {
if ((<TransientSymbol>prop).isDiscriminantProperty === undefined) {
(<TransientSymbol>prop).isDiscriminantProperty = !!((<TransientSymbol>prop).checkFlags & CheckFlags.HasNonUniformType) && isLiteralType(getTypeOfSymbol(prop));
let propType: Type;
(<TransientSymbol>prop).isDiscriminantProperty = !!((<TransientSymbol>prop).checkFlags & CheckFlags.HasNonUniformType) &&
(propType = getTypeOfSymbol(prop), (isLiteralType(propType) || isNullableDiscriminable(propType)));
}
return !!(<TransientSymbol>prop).isDiscriminantProperty;
}
Expand Down
118 changes: 118 additions & 0 deletions tests/baselines/reference/nullableDiscriminant.errors.txt
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
}




154 changes: 154 additions & 0 deletions tests/baselines/reference/nullableDiscriminant.js
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;
}
Loading