Skip to content

Fixed narrowing based on aliased discriminants coming from destructured parameters #56860

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
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
5 changes: 4 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,7 @@ import {
isCallOrNewExpression,
isCallSignatureDeclaration,
isCatchClause,
isCatchClauseVariableDeclaration,
isCatchClauseVariableDeclarationOrBindingElement,
isCheckJsEnabledForFile,
isClassDeclaration,
Expand Down Expand Up @@ -27461,7 +27462,9 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
case SyntaxKind.ObjectBindingPattern:
case SyntaxKind.ArrayBindingPattern:
const rootDeclaration = getRootDeclaration(node.parent);
return isVariableDeclaration(rootDeclaration) && isVarConstLike(rootDeclaration);
return isParameter(rootDeclaration) || isCatchClauseVariableDeclaration(rootDeclaration)
? !isSomeSymbolAssigned(rootDeclaration)
: isVariableDeclaration(rootDeclaration) && isVarConstLike(rootDeclaration);
}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,14 @@ controlFlowAliasedDiscriminants.ts(98,19): error TS1360: Type 'string | number'
resp.resp.data satisfies string;
}
}

function bindingPatternInParameter({ data: data1, isSuccess: isSuccess1 }: UseQueryResult<number>) {
const { data: data2, isSuccess: isSuccess2 } = useQuery();

const areSuccess = isSuccess1 && isSuccess2;
if (areSuccess) {
data1.toExponential();
data2.toExponential();
}
}

20 changes: 20 additions & 0 deletions tests/baselines/reference/controlFlowAliasedDiscriminants.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,20 @@ type Nested = {
resp.resp.data satisfies string;
}
}

function bindingPatternInParameter({ data: data1, isSuccess: isSuccess1 }: UseQueryResult<number>) {
const { data: data2, isSuccess: isSuccess2 } = useQuery();

const areSuccess = isSuccess1 && isSuccess2;
if (areSuccess) {
data1.toExponential();
data2.toExponential();
}
}


//// [controlFlowAliasedDiscriminants.js]
"use strict";
function useQuery() {
return {
isSuccess: false,
Expand Down Expand Up @@ -180,3 +191,12 @@ if (areSuccess) {
resp.resp.data;
}
}
function bindingPatternInParameter(_a) {
var data1 = _a.data, isSuccess1 = _a.isSuccess;
var _b = useQuery(), data2 = _b.data, isSuccess2 = _b.isSuccess;
var areSuccess = isSuccess1 && isSuccess2;
if (areSuccess) {
data1.toExponential();
data2.toExponential();
}
}
35 changes: 35 additions & 0 deletions tests/baselines/reference/controlFlowAliasedDiscriminants.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -325,3 +325,38 @@ type Nested = {
}
}

function bindingPatternInParameter({ data: data1, isSuccess: isSuccess1 }: UseQueryResult<number>) {
>bindingPatternInParameter : Symbol(bindingPatternInParameter, Decl(controlFlowAliasedDiscriminants.ts, 102, 1))
>data : Symbol(data, Decl(controlFlowAliasedDiscriminants.ts, 1, 21), Decl(controlFlowAliasedDiscriminants.ts, 4, 20))
>data1 : Symbol(data1, Decl(controlFlowAliasedDiscriminants.ts, 104, 36))
>isSuccess : Symbol(isSuccess, Decl(controlFlowAliasedDiscriminants.ts, 0, 26), Decl(controlFlowAliasedDiscriminants.ts, 3, 5))
>isSuccess1 : Symbol(isSuccess1, Decl(controlFlowAliasedDiscriminants.ts, 104, 49))
>UseQueryResult : Symbol(UseQueryResult, Decl(controlFlowAliasedDiscriminants.ts, 0, 0))

const { data: data2, isSuccess: isSuccess2 } = useQuery();
>data : Symbol(data, Decl(controlFlowAliasedDiscriminants.ts, 1, 21), Decl(controlFlowAliasedDiscriminants.ts, 4, 20))
>data2 : Symbol(data2, Decl(controlFlowAliasedDiscriminants.ts, 105, 9))
>isSuccess : Symbol(isSuccess, Decl(controlFlowAliasedDiscriminants.ts, 0, 26), Decl(controlFlowAliasedDiscriminants.ts, 3, 5))
>isSuccess2 : Symbol(isSuccess2, Decl(controlFlowAliasedDiscriminants.ts, 105, 22))
>useQuery : Symbol(useQuery, Decl(controlFlowAliasedDiscriminants.ts, 6, 2))

const areSuccess = isSuccess1 && isSuccess2;
>areSuccess : Symbol(areSuccess, Decl(controlFlowAliasedDiscriminants.ts, 107, 7))
>isSuccess1 : Symbol(isSuccess1, Decl(controlFlowAliasedDiscriminants.ts, 104, 49))
>isSuccess2 : Symbol(isSuccess2, Decl(controlFlowAliasedDiscriminants.ts, 105, 22))

if (areSuccess) {
>areSuccess : Symbol(areSuccess, Decl(controlFlowAliasedDiscriminants.ts, 107, 7))

data1.toExponential();
>data1.toExponential : Symbol(Number.toExponential, Decl(lib.es5.d.ts, --, --))
>data1 : Symbol(data1, Decl(controlFlowAliasedDiscriminants.ts, 104, 36))
>toExponential : Symbol(Number.toExponential, Decl(lib.es5.d.ts, --, --))

data2.toExponential();
>data2.toExponential : Symbol(Number.toExponential, Decl(lib.es5.d.ts, --, --))
>data2 : Symbol(data2, Decl(controlFlowAliasedDiscriminants.ts, 105, 9))
>toExponential : Symbol(Number.toExponential, Decl(lib.es5.d.ts, --, --))
}
}

38 changes: 38 additions & 0 deletions tests/baselines/reference/controlFlowAliasedDiscriminants.types
Original file line number Diff line number Diff line change
Expand Up @@ -376,3 +376,41 @@ type Nested = {
}
}

function bindingPatternInParameter({ data: data1, isSuccess: isSuccess1 }: UseQueryResult<number>) {
>bindingPatternInParameter : ({ data: data1, isSuccess: isSuccess1 }: UseQueryResult<number>) => void
>data : any
>data1 : number | undefined
>isSuccess : any
>isSuccess1 : boolean

const { data: data2, isSuccess: isSuccess2 } = useQuery();
>data : any
>data2 : number | undefined
>isSuccess : any
>isSuccess2 : boolean
>useQuery() : UseQueryResult<number>
>useQuery : () => UseQueryResult<number>

const areSuccess = isSuccess1 && isSuccess2;
>areSuccess : boolean
>isSuccess1 && isSuccess2 : boolean
>isSuccess1 : boolean
>isSuccess2 : boolean

if (areSuccess) {
>areSuccess : boolean

data1.toExponential();
>data1.toExponential() : string
>data1.toExponential : (fractionDigits?: number | undefined) => string
>data1 : number
>toExponential : (fractionDigits?: number | undefined) => string

data2.toExponential();
>data2.toExponential() : string
>data2.toExponential : (fractionDigits?: number | undefined) => string
>data2 : number
>toExponential : (fractionDigits?: number | undefined) => string
}
}

13 changes: 11 additions & 2 deletions tests/cases/compiler/controlFlowAliasedDiscriminants.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// @strictNullChecks: true
// @noImplicitAny: true
// @strict: true

type UseQueryResult<T> = {
isSuccess: false;
Expand Down Expand Up @@ -104,3 +103,13 @@ type Nested = {
resp.resp.data satisfies string;
}
}

function bindingPatternInParameter({ data: data1, isSuccess: isSuccess1 }: UseQueryResult<number>) {
const { data: data2, isSuccess: isSuccess2 } = useQuery();

const areSuccess = isSuccess1 && isSuccess2;
if (areSuccess) {
data1.toExponential();
data2.toExponential();
}
}