Skip to content

Fix issue with optional chaining and type inference in type guard #55613

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
merged 1 commit into from
Sep 13, 2023
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
9 changes: 6 additions & 3 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26109,7 +26109,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
function hasMatchingArgument(expression: CallExpression | NewExpression, reference: Node) {
if (expression.arguments) {
for (const argument of expression.arguments) {
if (isOrContainsMatchingReference(reference, argument)) {
if (isOrContainsMatchingReference(reference, argument) || optionalChainContainsReference(argument, reference)) {
return true;
}
}
Expand Down Expand Up @@ -28075,8 +28075,11 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
return getNarrowedType(type, predicate.type, assumeTrue, /*checkDerived*/ false);
}
if (
strictNullChecks && assumeTrue && optionalChainContainsReference(predicateArgument, reference) &&
!(hasTypeFacts(predicate.type, TypeFacts.EQUndefined))
strictNullChecks && optionalChainContainsReference(predicateArgument, reference) &&
(
assumeTrue && !(hasTypeFacts(predicate.type, TypeFacts.EQUndefined)) ||
!assumeTrue && everyType(predicate.type, isNullableType)
)
) {
type = getAdjustedTypeWithFacts(type, TypeFacts.NEUndefinedOrNull);
}
Expand Down
38 changes: 38 additions & 0 deletions tests/baselines/reference/typePredicatesOptionalChaining1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//// [tests/cases/compiler/typePredicatesOptionalChaining1.ts] ////

//// [typePredicatesOptionalChaining1.ts]
type X = {
y?: {
z?: string;
};
};
const x: X = {
y: {},
};
// type guard
function isNotNull<A>(x: A): x is NonNullable<A> {
return x !== null && x !== undefined;
}
// function which I want to call in the result of the expression
function title(str: string) {
return str.length > 0 ? "Dear " + str : "Dear nobody";
}

isNotNull(x?.y?.z) ? title(x.y.z) : null; // should not error


//// [typePredicatesOptionalChaining1.js]
"use strict";
var _a;
var x = {
y: {},
};
// type guard
function isNotNull(x) {
return x !== null && x !== undefined;
}
// function which I want to call in the result of the expression
function title(str) {
return str.length > 0 ? "Dear " + str : "Dear nobody";
}
isNotNull((_a = x === null || x === void 0 ? void 0 : x.y) === null || _a === void 0 ? void 0 : _a.z) ? title(x.y.z) : null; // should not error
63 changes: 63 additions & 0 deletions tests/baselines/reference/typePredicatesOptionalChaining1.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//// [tests/cases/compiler/typePredicatesOptionalChaining1.ts] ////

=== typePredicatesOptionalChaining1.ts ===
type X = {
>X : Symbol(X, Decl(typePredicatesOptionalChaining1.ts, 0, 0))

y?: {
>y : Symbol(y, Decl(typePredicatesOptionalChaining1.ts, 0, 10))

z?: string;
>z : Symbol(z, Decl(typePredicatesOptionalChaining1.ts, 1, 7))

};
};
const x: X = {
>x : Symbol(x, Decl(typePredicatesOptionalChaining1.ts, 5, 5))
>X : Symbol(X, Decl(typePredicatesOptionalChaining1.ts, 0, 0))

y: {},
>y : Symbol(y, Decl(typePredicatesOptionalChaining1.ts, 5, 14))

};
// type guard
function isNotNull<A>(x: A): x is NonNullable<A> {
>isNotNull : Symbol(isNotNull, Decl(typePredicatesOptionalChaining1.ts, 7, 2))
>A : Symbol(A, Decl(typePredicatesOptionalChaining1.ts, 9, 19))
>x : Symbol(x, Decl(typePredicatesOptionalChaining1.ts, 9, 22))
>A : Symbol(A, Decl(typePredicatesOptionalChaining1.ts, 9, 19))
>x : Symbol(x, Decl(typePredicatesOptionalChaining1.ts, 9, 22))
>NonNullable : Symbol(NonNullable, Decl(lib.es5.d.ts, --, --))
>A : Symbol(A, Decl(typePredicatesOptionalChaining1.ts, 9, 19))

return x !== null && x !== undefined;
>x : Symbol(x, Decl(typePredicatesOptionalChaining1.ts, 9, 22))
>x : Symbol(x, Decl(typePredicatesOptionalChaining1.ts, 9, 22))
>undefined : Symbol(undefined)
}
// function which I want to call in the result of the expression
function title(str: string) {
>title : Symbol(title, Decl(typePredicatesOptionalChaining1.ts, 11, 1))
>str : Symbol(str, Decl(typePredicatesOptionalChaining1.ts, 13, 15))

return str.length > 0 ? "Dear " + str : "Dear nobody";
>str.length : Symbol(String.length, Decl(lib.es5.d.ts, --, --))
>str : Symbol(str, Decl(typePredicatesOptionalChaining1.ts, 13, 15))
>length : Symbol(String.length, Decl(lib.es5.d.ts, --, --))
>str : Symbol(str, Decl(typePredicatesOptionalChaining1.ts, 13, 15))
}

isNotNull(x?.y?.z) ? title(x.y.z) : null; // should not error
>isNotNull : Symbol(isNotNull, Decl(typePredicatesOptionalChaining1.ts, 7, 2))
>x?.y?.z : Symbol(z, Decl(typePredicatesOptionalChaining1.ts, 1, 7))
>x?.y : Symbol(y, Decl(typePredicatesOptionalChaining1.ts, 0, 10))
>x : Symbol(x, Decl(typePredicatesOptionalChaining1.ts, 5, 5))
>y : Symbol(y, Decl(typePredicatesOptionalChaining1.ts, 0, 10))
>z : Symbol(z, Decl(typePredicatesOptionalChaining1.ts, 1, 7))
>title : Symbol(title, Decl(typePredicatesOptionalChaining1.ts, 11, 1))
>x.y.z : Symbol(z, Decl(typePredicatesOptionalChaining1.ts, 1, 7))
>x.y : Symbol(y, Decl(typePredicatesOptionalChaining1.ts, 0, 10))
>x : Symbol(x, Decl(typePredicatesOptionalChaining1.ts, 5, 5))
>y : Symbol(y, Decl(typePredicatesOptionalChaining1.ts, 0, 10))
>z : Symbol(z, Decl(typePredicatesOptionalChaining1.ts, 1, 7))

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

=== typePredicatesOptionalChaining1.ts ===
type X = {
>X : { y?: { z?: string | undefined; } | undefined; }

y?: {
>y : { z?: string | undefined; } | undefined

z?: string;
>z : string | undefined

};
};
const x: X = {
>x : X
>{ y: {},} : { y: {}; }

y: {},
>y : {}
>{} : {}

};
// type guard
function isNotNull<A>(x: A): x is NonNullable<A> {
>isNotNull : <A>(x: A) => x is NonNullable<A>
>x : A

return x !== null && x !== undefined;
>x !== null && x !== undefined : boolean
>x !== null : boolean
>x : A
>x !== undefined : boolean
>x : A & ({} | undefined)
>undefined : undefined
}
// function which I want to call in the result of the expression
function title(str: string) {
>title : (str: string) => string
>str : string

return str.length > 0 ? "Dear " + str : "Dear nobody";
>str.length > 0 ? "Dear " + str : "Dear nobody" : string
>str.length > 0 : boolean
>str.length : number
>str : string
>length : number
>0 : 0
>"Dear " + str : string
>"Dear " : "Dear "
>str : string
>"Dear nobody" : "Dear nobody"
}

isNotNull(x?.y?.z) ? title(x.y.z) : null; // should not error
>isNotNull(x?.y?.z) ? title(x.y.z) : null : string | null
>isNotNull(x?.y?.z) : boolean
>isNotNull : <A>(x: A) => x is NonNullable<A>
>x?.y?.z : string | undefined
>x?.y : { z?: string | undefined; } | undefined
>x : X
>y : { z?: string | undefined; } | undefined
>z : string | undefined
>title(x.y.z) : string
>title : (str: string) => string
>x.y.z : string
>x.y : { z?: string | undefined; }
>x : X
>y : { z?: string | undefined; }
>z : string

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

//// [typePredicatesOptionalChaining2.ts]
type Person = { name: string; }

const getName1 = (person?: Person): string => {
return typeof person?.name === 'string' ? person?.name : '';
};

const isString = (value: any): value is string => {
return typeof value === 'string';
};

const getName2 = (person?: Person): string => {
return isString(person?.name) ? person?.name : '';
};


//// [typePredicatesOptionalChaining2.js]
"use strict";
var getName1 = function (person) {
return typeof (person === null || person === void 0 ? void 0 : person.name) === 'string' ? person === null || person === void 0 ? void 0 : person.name : '';
};
var isString = function (value) {
return typeof value === 'string';
};
var getName2 = function (person) {
return isString(person === null || person === void 0 ? void 0 : person.name) ? person === null || person === void 0 ? void 0 : person.name : '';
};
48 changes: 48 additions & 0 deletions tests/baselines/reference/typePredicatesOptionalChaining2.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//// [tests/cases/compiler/typePredicatesOptionalChaining2.ts] ////

=== typePredicatesOptionalChaining2.ts ===
type Person = { name: string; }
>Person : Symbol(Person, Decl(typePredicatesOptionalChaining2.ts, 0, 0))
>name : Symbol(name, Decl(typePredicatesOptionalChaining2.ts, 0, 15))

const getName1 = (person?: Person): string => {
>getName1 : Symbol(getName1, Decl(typePredicatesOptionalChaining2.ts, 2, 5))
>person : Symbol(person, Decl(typePredicatesOptionalChaining2.ts, 2, 18))
>Person : Symbol(Person, Decl(typePredicatesOptionalChaining2.ts, 0, 0))

return typeof person?.name === 'string' ? person?.name : '';
>person?.name : Symbol(name, Decl(typePredicatesOptionalChaining2.ts, 0, 15))
>person : Symbol(person, Decl(typePredicatesOptionalChaining2.ts, 2, 18))
>name : Symbol(name, Decl(typePredicatesOptionalChaining2.ts, 0, 15))
>person?.name : Symbol(name, Decl(typePredicatesOptionalChaining2.ts, 0, 15))
>person : Symbol(person, Decl(typePredicatesOptionalChaining2.ts, 2, 18))
>name : Symbol(name, Decl(typePredicatesOptionalChaining2.ts, 0, 15))

};

const isString = (value: any): value is string => {
>isString : Symbol(isString, Decl(typePredicatesOptionalChaining2.ts, 6, 5))
>value : Symbol(value, Decl(typePredicatesOptionalChaining2.ts, 6, 18))
>value : Symbol(value, Decl(typePredicatesOptionalChaining2.ts, 6, 18))

return typeof value === 'string';
>value : Symbol(value, Decl(typePredicatesOptionalChaining2.ts, 6, 18))

};

const getName2 = (person?: Person): string => {
>getName2 : Symbol(getName2, Decl(typePredicatesOptionalChaining2.ts, 10, 5))
>person : Symbol(person, Decl(typePredicatesOptionalChaining2.ts, 10, 18))
>Person : Symbol(Person, Decl(typePredicatesOptionalChaining2.ts, 0, 0))

return isString(person?.name) ? person?.name : '';
>isString : Symbol(isString, Decl(typePredicatesOptionalChaining2.ts, 6, 5))
>person?.name : Symbol(name, Decl(typePredicatesOptionalChaining2.ts, 0, 15))
>person : Symbol(person, Decl(typePredicatesOptionalChaining2.ts, 10, 18))
>name : Symbol(name, Decl(typePredicatesOptionalChaining2.ts, 0, 15))
>person?.name : Symbol(name, Decl(typePredicatesOptionalChaining2.ts, 0, 15))
>person : Symbol(person, Decl(typePredicatesOptionalChaining2.ts, 10, 18))
>name : Symbol(name, Decl(typePredicatesOptionalChaining2.ts, 0, 15))

};

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

=== typePredicatesOptionalChaining2.ts ===
type Person = { name: string; }
>Person : { name: string; }
>name : string

const getName1 = (person?: Person): string => {
>getName1 : (person?: Person) => string
>(person?: Person): string => { return typeof person?.name === 'string' ? person?.name : '';} : (person?: Person) => string
>person : Person | undefined

return typeof person?.name === 'string' ? person?.name : '';
>typeof person?.name === 'string' ? person?.name : '' : string
>typeof person?.name === 'string' : boolean
>typeof person?.name : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
>person?.name : string | undefined
>person : Person | undefined
>name : string | undefined
>'string' : "string"
>person?.name : string
>person : Person
>name : string
>'' : ""

};

const isString = (value: any): value is string => {
>isString : (value: any) => value is string
>(value: any): value is string => { return typeof value === 'string';} : (value: any) => value is string
>value : any

return typeof value === 'string';
>typeof value === 'string' : boolean
>typeof value : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
>value : any
>'string' : "string"

};

const getName2 = (person?: Person): string => {
>getName2 : (person?: Person) => string
>(person?: Person): string => { return isString(person?.name) ? person?.name : '';} : (person?: Person) => string
>person : Person | undefined

return isString(person?.name) ? person?.name : '';
>isString(person?.name) ? person?.name : '' : string
>isString(person?.name) : boolean
>isString : (value: any) => value is string
>person?.name : string | undefined
>person : Person | undefined
>name : string | undefined
>person?.name : string
>person : Person
>name : string
>'' : ""

};

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

//// [typePredicatesOptionalChaining3.ts]
interface Animal {
breed?: Breed;
}
interface Breed {
size?: string;
}

declare function isNil(value: unknown): value is undefined | null;

function getBreedSizeWithoutFunction(animal: Animal): string | undefined {
if (animal?.breed?.size != null) {
return animal.breed.size;
} else {
return undefined;
}
}

function getBreedSizeWithFunction(animal: Animal): string | undefined {
if (!isNil(animal?.breed?.size)) {
return animal.breed.size;
} else {
return undefined;
}
}


//// [typePredicatesOptionalChaining3.js]
"use strict";
function getBreedSizeWithoutFunction(animal) {
var _a;
if (((_a = animal === null || animal === void 0 ? void 0 : animal.breed) === null || _a === void 0 ? void 0 : _a.size) != null) {
return animal.breed.size;
}
else {
return undefined;
}
}
function getBreedSizeWithFunction(animal) {
var _a;
if (!isNil((_a = animal === null || animal === void 0 ? void 0 : animal.breed) === null || _a === void 0 ? void 0 : _a.size)) {
return animal.breed.size;
}
else {
return undefined;
}
}
Loading