Skip to content
Open
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
7 changes: 6 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29297,6 +29297,11 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {

function narrowTypeByCallExpression(type: Type, callExpression: CallExpression, assumeTrue: boolean): Type {
if (hasMatchingArgument(callExpression, reference)) {
// Ordinarily we don't need to to this in control flow analysis because the Binder breaks this down.
// However, we may encounter the need to narrow this down here when analyzing aliased conditional expressions.
Comment on lines +29300 to +29301
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A need to do some extra narrowings when dealing with aliased conditions isn't new, see: https://github.dev/microsoft/TypeScript/blob/3163fe7e3898c1f48cd9bc097b96e3426cd2a453/src/compiler/checker.ts#L28886-L28888

if (inlineLevel !== 0 && isOptionalChain(callExpression)) {
type = narrowTypeByOptionality(type, callExpression.expression, assumeTrue);
}
const signature = assumeTrue || !isCallChain(callExpression) ? getEffectsSignature(callExpression) : undefined;
const predicate = signature && getTypePredicateOfSignature(signature);
if (predicate && (predicate.kind === TypePredicateKind.This || predicate.kind === TypePredicateKind.Identifier)) {
Expand Down Expand Up @@ -29393,7 +29398,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}

function narrowTypeByOptionality(type: Type, expr: Expression, assumePresent: boolean): Type {
if (isMatchingReference(reference, expr)) {
if (isMatchingReference(reference, expr) || optionalChainContainsReference(expr, reference)) {
return getAdjustedTypeWithFacts(type, assumePresent ? TypeFacts.NEUndefinedOrNull : TypeFacts.EQUndefinedOrNull);
}
const access = getDiscriminantPropertyAccess(expr, type);
Expand Down
39 changes: 39 additions & 0 deletions tests/baselines/reference/controlFlowOptionalChain5.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//// [tests/cases/conformance/controlFlow/controlFlowOptionalChain5.ts] ////

=== controlFlowOptionalChain5.ts ===
// https://github.com/microsoft/TypeScript/issues/59145

function resolve1(id: string | undefined) {
>resolve1 : Symbol(resolve1, Decl(controlFlowOptionalChain5.ts, 0, 0))
>id : Symbol(id, Decl(controlFlowOptionalChain5.ts, 2, 18))

const isBundled = id?.includes("_bundled");
>isBundled : Symbol(isBundled, Decl(controlFlowOptionalChain5.ts, 3, 7))
>id?.includes : Symbol(String.includes, Decl(lib.es2015.core.d.ts, --, --))
>id : Symbol(id, Decl(controlFlowOptionalChain5.ts, 2, 18))
>includes : Symbol(String.includes, Decl(lib.es2015.core.d.ts, --, --))

if (isBundled) {
>isBundled : Symbol(isBundled, Decl(controlFlowOptionalChain5.ts, 3, 7))

const str: string = id;
>str : Symbol(str, Decl(controlFlowOptionalChain5.ts, 5, 9))
>id : Symbol(id, Decl(controlFlowOptionalChain5.ts, 2, 18))
}
}

function resolve2(id: string | undefined) {
>resolve2 : Symbol(resolve2, Decl(controlFlowOptionalChain5.ts, 7, 1))
>id : Symbol(id, Decl(controlFlowOptionalChain5.ts, 9, 18))

if (id?.includes("_bundled")) {
>id?.includes : Symbol(String.includes, Decl(lib.es2015.core.d.ts, --, --))
>id : Symbol(id, Decl(controlFlowOptionalChain5.ts, 9, 18))
>includes : Symbol(String.includes, Decl(lib.es2015.core.d.ts, --, --))

const str: string = id;
>str : Symbol(str, Decl(controlFlowOptionalChain5.ts, 11, 9))
>id : Symbol(id, Decl(controlFlowOptionalChain5.ts, 9, 18))
}
}

63 changes: 63 additions & 0 deletions tests/baselines/reference/controlFlowOptionalChain5.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//// [tests/cases/conformance/controlFlow/controlFlowOptionalChain5.ts] ////

=== controlFlowOptionalChain5.ts ===
// https://github.com/microsoft/TypeScript/issues/59145

function resolve1(id: string | undefined) {
>resolve1 : (id: string | undefined) => void
> : ^ ^^ ^^^^^^^^^
>id : string | undefined
> : ^^^^^^^^^^^^^^^^^^

const isBundled = id?.includes("_bundled");
>isBundled : boolean | undefined
> : ^^^^^^^^^^^^^^^^^^^
>id?.includes("_bundled") : boolean | undefined
> : ^^^^^^^^^^^^^^^^^^^
>id?.includes : ((searchString: string, position?: number) => boolean) | undefined
> : ^^ ^^ ^^ ^^^ ^^^^^ ^^^^^^^^^^^^^
>id : string | undefined
> : ^^^^^^^^^^^^^^^^^^
>includes : ((searchString: string, position?: number) => boolean) | undefined
> : ^^ ^^ ^^ ^^^ ^^^^^ ^^^^^^^^^^^^^
>"_bundled" : "_bundled"
> : ^^^^^^^^^^

if (isBundled) {
>isBundled : boolean | undefined
> : ^^^^^^^^^^^^^^^^^^^

const str: string = id;
>str : string
> : ^^^^^^
>id : string
> : ^^^^^^
}
}

function resolve2(id: string | undefined) {
>resolve2 : (id: string | undefined) => void
> : ^ ^^ ^^^^^^^^^
>id : string | undefined
> : ^^^^^^^^^^^^^^^^^^

if (id?.includes("_bundled")) {
>id?.includes("_bundled") : boolean | undefined
> : ^^^^^^^^^^^^^^^^^^^
>id?.includes : ((searchString: string, position?: number) => boolean) | undefined
> : ^^ ^^ ^^ ^^^ ^^^^^ ^^^^^^^^^^^^^
>id : string | undefined
> : ^^^^^^^^^^^^^^^^^^
>includes : ((searchString: string, position?: number) => boolean) | undefined
> : ^^ ^^ ^^ ^^^ ^^^^^ ^^^^^^^^^^^^^
>"_bundled" : "_bundled"
> : ^^^^^^^^^^

const str: string = id;
>str : string
> : ^^^^^^
>id : string
> : ^^^^^^
}
}

18 changes: 18 additions & 0 deletions tests/cases/conformance/controlFlow/controlFlowOptionalChain5.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// @strict: true
// @lib: esnext
// @noEmit: true

// https://github.com/microsoft/TypeScript/issues/59145

function resolve1(id: string | undefined) {
const isBundled = id?.includes("_bundled");
if (isBundled) {
const str: string = id;
}
}

function resolve2(id: string | undefined) {
if (id?.includes("_bundled")) {
const str: string = id;
}
}