Skip to content

Check possible undefined on LHS IndexedType for logical OR short-circuit #29794

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
wants to merge 2 commits into from
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 @@ -22763,6 +22763,10 @@ namespace ts {
getUnionType([extractDefinitelyFalsyTypes(strictNullChecks ? leftType : getBaseTypeOfLiteralType(rightType)), rightType]) :
leftType;
case SyntaxKind.BarBarToken:
leftType = leftType.flags & TypeFlags.IndexedAccess ?
any(getPropertiesOfType((<IndexedAccessType>leftType).objectType), prop => !!(prop.flags & SymbolFlags.Optional)) ?
Copy link
Member

Choose a reason for hiding this comment

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

I don't think this is right - checking is any prop in the indexed access's type is optional and then returning never for its type if so seems very wrong - the potential type of the LHS is completely lost here, and it loses the nuance of the individual property accessed. This needs #29317 to be correctly solved - the LHS type should be leftType & not 0 & not null & not undefined & not "", which handles generics appropriately.

Copy link
Member

Choose a reason for hiding this comment

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

The fact that we don't have more tests this change, as-is, breaks is actually a little disturbing.

neverType : leftType :
leftType;
return getTypeFacts(leftType) & TypeFacts.Falsy ?
getUnionType([removeDefinitelyFalsyTypes(leftType), rightType], UnionReduction.Subtype) :
leftType;
Expand Down
12 changes: 12 additions & 0 deletions src/compiler/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,18 @@ namespace ts {
return undefined;
}

/**
* Returns true if any of the items in array satisfies predicate 'if provided' or is truthy
*/
export function any<T>(array: ReadonlyArray<T>, predicate?: (i: any) => boolean): boolean {
Copy link
Member

Choose a reason for hiding this comment

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

This function is built-in; it's Array#some

for (const item of array) {
if (!predicate ? item : predicate(item)) {
return true;
}
}
return false;
}

export function firstDefinedIterator<T, U>(iter: Iterator<T>, callback: (element: T) => U | undefined): U | undefined {
while (true) {
const { value, done } = iter.next();
Expand Down
28 changes: 28 additions & 0 deletions tests/baselines/reference/shortCircuitEvaluation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//// [shortCircuitEvaluation.ts]
let a = undefined || 1;
a++

let b = null || 'foo';
b.concat('bar')

type EventType = 'click' | 'dblclick'

const handlerMap: { [P in EventType]?: any[] } = {}

function addHandler<P extends EventType>(evType: P) {
const handlerList = handlerMap[evType] || <any[]>[]
handlerList.push({})
handlerMap[evType] = handlerList
}

//// [shortCircuitEvaluation.js]
var a = undefined || 1;
a++;
var b = null || 'foo';
b.concat('bar');
var handlerMap = {};
function addHandler(evType) {
var handlerList = handlerMap[evType] || [];
handlerList.push({});
handlerMap[evType] = handlerList;
}
46 changes: 46 additions & 0 deletions tests/baselines/reference/shortCircuitEvaluation.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
=== tests/cases/conformance/expressions/binaryOperators/logicalOrOperator/shortCircuitEvaluation.ts ===
let a = undefined || 1;
>a : Symbol(a, Decl(shortCircuitEvaluation.ts, 0, 3))
>undefined : Symbol(undefined)

a++
>a : Symbol(a, Decl(shortCircuitEvaluation.ts, 0, 3))

let b = null || 'foo';
>b : Symbol(b, Decl(shortCircuitEvaluation.ts, 3, 3))

b.concat('bar')
>b.concat : Symbol(String.concat, Decl(lib.es5.d.ts, --, --))
>b : Symbol(b, Decl(shortCircuitEvaluation.ts, 3, 3))
>concat : Symbol(String.concat, Decl(lib.es5.d.ts, --, --))

type EventType = 'click' | 'dblclick'
>EventType : Symbol(EventType, Decl(shortCircuitEvaluation.ts, 4, 15))

const handlerMap: { [P in EventType]?: any[] } = {}
>handlerMap : Symbol(handlerMap, Decl(shortCircuitEvaluation.ts, 8, 5))
>P : Symbol(P, Decl(shortCircuitEvaluation.ts, 8, 21))
>EventType : Symbol(EventType, Decl(shortCircuitEvaluation.ts, 4, 15))

function addHandler<P extends EventType>(evType: P) {
>addHandler : Symbol(addHandler, Decl(shortCircuitEvaluation.ts, 8, 51))
>P : Symbol(P, Decl(shortCircuitEvaluation.ts, 10, 20))
>EventType : Symbol(EventType, Decl(shortCircuitEvaluation.ts, 4, 15))
>evType : Symbol(evType, Decl(shortCircuitEvaluation.ts, 10, 41))
>P : Symbol(P, Decl(shortCircuitEvaluation.ts, 10, 20))

const handlerList = handlerMap[evType] || <any[]>[]
>handlerList : Symbol(handlerList, Decl(shortCircuitEvaluation.ts, 11, 7))
>handlerMap : Symbol(handlerMap, Decl(shortCircuitEvaluation.ts, 8, 5))
>evType : Symbol(evType, Decl(shortCircuitEvaluation.ts, 10, 41))

handlerList.push({})
>handlerList.push : Symbol(Array.push, Decl(lib.es5.d.ts, --, --))
>handlerList : Symbol(handlerList, Decl(shortCircuitEvaluation.ts, 11, 7))
>push : Symbol(Array.push, Decl(lib.es5.d.ts, --, --))

handlerMap[evType] = handlerList
>handlerMap : Symbol(handlerMap, Decl(shortCircuitEvaluation.ts, 8, 5))
>evType : Symbol(evType, Decl(shortCircuitEvaluation.ts, 10, 41))
>handlerList : Symbol(handlerList, Decl(shortCircuitEvaluation.ts, 11, 7))
}
58 changes: 58 additions & 0 deletions tests/baselines/reference/shortCircuitEvaluation.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
=== tests/cases/conformance/expressions/binaryOperators/logicalOrOperator/shortCircuitEvaluation.ts ===
let a = undefined || 1;
>a : number
>undefined || 1 : 1
>undefined : undefined
>1 : 1

a++
>a++ : number
>a : number

let b = null || 'foo';
>b : string
>null || 'foo' : "foo"
>null : null
>'foo' : "foo"

b.concat('bar')
>b.concat('bar') : string
>b.concat : (...strings: string[]) => string
>b : string
>concat : (...strings: string[]) => string
>'bar' : "bar"

type EventType = 'click' | 'dblclick'
>EventType : EventType

const handlerMap: { [P in EventType]?: any[] } = {}
>handlerMap : { click?: any[] | undefined; dblclick?: any[] | undefined; }
>{} : {}

function addHandler<P extends EventType>(evType: P) {
>addHandler : <P extends EventType>(evType: P) => void
>evType : P

const handlerList = handlerMap[evType] || <any[]>[]
>handlerList : any[]
>handlerMap[evType] || <any[]>[] : any[]
>handlerMap[evType] : { click?: any[] | undefined; dblclick?: any[] | undefined; }[P]
>handlerMap : { click?: any[] | undefined; dblclick?: any[] | undefined; }
>evType : P
><any[]>[] : any[]
>[] : never[]

handlerList.push({})
>handlerList.push({}) : number
>handlerList.push : (...items: any[]) => number
>handlerList : any[]
>push : (...items: any[]) => number
>{} : {}

handlerMap[evType] = handlerList
>handlerMap[evType] = handlerList : any[]
>handlerMap[evType] : { click?: any[] | undefined; dblclick?: any[] | undefined; }[P]
>handlerMap : { click?: any[] | undefined; dblclick?: any[] | undefined; }
>evType : P
>handlerList : any[]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// @strictNullChecks: true
let a = undefined || 1;
a++

let b = null || 'foo';
b.concat('bar')

type EventType = 'click' | 'dblclick'

const handlerMap: { [P in EventType]?: any[] } = {}

function addHandler<P extends EventType>(evType: P) {
const handlerList = handlerMap[evType] || <any[]>[]
handlerList.push({})
handlerMap[evType] = handlerList
}