Skip to content

Fix control flow optional chains #34537

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
4 changes: 4 additions & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19324,6 +19324,10 @@ namespace ts {
if (propName === undefined) {
return type;
}
if (strictNullChecks && access.questionDotToken) {
type = getTypeWithFacts(type, TypeFacts.NEUndefinedOrNull)
}

const propType = getTypeOfPropertyOfType(type, propName);
const narrowedPropType = propType && narrowType(propType);
return propType === narrowedPropType ? type : filterType(type, t => isTypeComparableTo(getTypeOfPropertyOrIndexSignature(t, propName), narrowedPropType!));
Expand Down
32 changes: 32 additions & 0 deletions tests/baselines/reference/controlFlowOptionalChain1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//// [controlFlowOptionalChain1.ts]
type Shape =
| { type: 'rectangle', width: number, height: number }
| { type: 'circle', radius: number }

declare function assertUndefined(v: undefined): void

function getArea(shape?: Shape) {
switch (shape?.type) {
case 'circle':
return Math.PI * shape.radius ** 2
case 'rectangle':
return shape.width * shape.height
default:
return assertUndefined(shape)
}
}


//// [controlFlowOptionalChain1.js]
"use strict";
function getArea(shape) {
var _a;
switch ((_a = shape) === null || _a === void 0 ? void 0 : _a.type) {
case 'circle':
return Math.PI * Math.pow(shape.radius, 2);
case 'rectangle':
return shape.width * shape.height;
default:
return assertUndefined(shape);
}
}
52 changes: 52 additions & 0 deletions tests/baselines/reference/controlFlowOptionalChain1.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
=== tests/cases/conformance/controlFlow/controlFlowOptionalChain1.ts ===
type Shape =
>Shape : Symbol(Shape, Decl(controlFlowOptionalChain1.ts, 0, 0))

| { type: 'rectangle', width: number, height: number }
>type : Symbol(type, Decl(controlFlowOptionalChain1.ts, 1, 7))
>width : Symbol(width, Decl(controlFlowOptionalChain1.ts, 1, 26))
>height : Symbol(height, Decl(controlFlowOptionalChain1.ts, 1, 41))

| { type: 'circle', radius: number }
>type : Symbol(type, Decl(controlFlowOptionalChain1.ts, 2, 7))
>radius : Symbol(radius, Decl(controlFlowOptionalChain1.ts, 2, 23))

declare function assertUndefined(v: undefined): void
>assertUndefined : Symbol(assertUndefined, Decl(controlFlowOptionalChain1.ts, 2, 40))
>v : Symbol(v, Decl(controlFlowOptionalChain1.ts, 4, 33))

function getArea(shape?: Shape) {
>getArea : Symbol(getArea, Decl(controlFlowOptionalChain1.ts, 4, 52))
>shape : Symbol(shape, Decl(controlFlowOptionalChain1.ts, 6, 17))
>Shape : Symbol(Shape, Decl(controlFlowOptionalChain1.ts, 0, 0))

switch (shape?.type) {
>shape?.type : Symbol(type, Decl(controlFlowOptionalChain1.ts, 1, 7), Decl(controlFlowOptionalChain1.ts, 2, 7))
>shape : Symbol(shape, Decl(controlFlowOptionalChain1.ts, 6, 17))
>type : Symbol(type, Decl(controlFlowOptionalChain1.ts, 1, 7), Decl(controlFlowOptionalChain1.ts, 2, 7))

case 'circle':
return Math.PI * shape.radius ** 2
>Math.PI : Symbol(Math.PI, Decl(lib.es5.d.ts, --, --))
>Math : Symbol(Math, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>PI : Symbol(Math.PI, Decl(lib.es5.d.ts, --, --))
>shape.radius : Symbol(radius, Decl(controlFlowOptionalChain1.ts, 2, 23))
>shape : Symbol(shape, Decl(controlFlowOptionalChain1.ts, 6, 17))
>radius : Symbol(radius, Decl(controlFlowOptionalChain1.ts, 2, 23))

case 'rectangle':
return shape.width * shape.height
>shape.width : Symbol(width, Decl(controlFlowOptionalChain1.ts, 1, 26))
>shape : Symbol(shape, Decl(controlFlowOptionalChain1.ts, 6, 17))
>width : Symbol(width, Decl(controlFlowOptionalChain1.ts, 1, 26))
>shape.height : Symbol(height, Decl(controlFlowOptionalChain1.ts, 1, 41))
>shape : Symbol(shape, Decl(controlFlowOptionalChain1.ts, 6, 17))
>height : Symbol(height, Decl(controlFlowOptionalChain1.ts, 1, 41))

default:
return assertUndefined(shape)
>assertUndefined : Symbol(assertUndefined, Decl(controlFlowOptionalChain1.ts, 2, 40))
>shape : Symbol(shape, Decl(controlFlowOptionalChain1.ts, 6, 17))
}
}

60 changes: 60 additions & 0 deletions tests/baselines/reference/controlFlowOptionalChain1.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
=== tests/cases/conformance/controlFlow/controlFlowOptionalChain1.ts ===
type Shape =
>Shape : Shape

| { type: 'rectangle', width: number, height: number }
>type : "rectangle"
>width : number
>height : number

| { type: 'circle', radius: number }
>type : "circle"
>radius : number

declare function assertUndefined(v: undefined): void
>assertUndefined : (v: undefined) => void
>v : undefined

function getArea(shape?: Shape) {
>getArea : (shape?: { type: "rectangle"; width: number; height: number; } | { type: "circle"; radius: number; } | undefined) => number | void
>shape : { type: "rectangle"; width: number; height: number; } | { type: "circle"; radius: number; } | undefined

switch (shape?.type) {
>shape?.type : "rectangle" | "circle" | undefined
>shape : { type: "rectangle"; width: number; height: number; } | { type: "circle"; radius: number; } | undefined
>type : "rectangle" | "circle" | undefined

case 'circle':
>'circle' : "circle"

return Math.PI * shape.radius ** 2
>Math.PI * shape.radius ** 2 : number
>Math.PI : number
>Math : Math
>PI : number
>shape.radius ** 2 : number
>shape.radius : number
>shape : { type: "circle"; radius: number; }
>radius : number
>2 : 2

case 'rectangle':
>'rectangle' : "rectangle"

return shape.width * shape.height
>shape.width * shape.height : number
>shape.width : number
>shape : { type: "rectangle"; width: number; height: number; }
>width : number
>shape.height : number
>shape : { type: "rectangle"; width: number; height: number; }
>height : number

default:
return assertUndefined(shape)
>assertUndefined(shape) : void
>assertUndefined : (v: undefined) => void
>shape : never
}
}

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

type Shape =
| { type: 'rectangle', width: number, height: number }
| { type: 'circle', radius: number }

declare function assertUndefined(v: undefined): void

function getArea(shape?: Shape) {
switch (shape?.type) {
case 'circle':
return Math.PI * shape.radius ** 2
case 'rectangle':
return shape.width * shape.height
default:
return assertUndefined(shape)
}
}