microsoft / TypeScript

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
https://www.typescriptlang.org
Apache License 2.0
101.21k stars 12.51k forks source link

Map not satisfied from earlier Boolean() check #60509

Closed Netail closed 1 week ago

Netail commented 1 week ago

πŸ”Ž Search Terms

Boolean, null/undefined

πŸ•— Version & Regression Information

⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.6.3#code/MYewdgzgLgBAhgJwXAngLhtBBLMBzAbQF0YAfGAVzABMBTAM11urJjAoBsOYBeGAgORw4AknAjwwKANwAoWaEixsEAGpwO2FnwBCIEB1pwwACkTIUMAGRX4SVADpD+KAAsYAPhgAGAJRzZbHoYExV1TWpfOwsHAFs4AAcTWl4vRQgDWicQPFC1DS1ffxgAehKYAFEkEAQAGhgAd1dsQ0aUjQQjaksAN1ocRmYYNxTaAA8VKGNgFNtnPDcYACMGGtogA

πŸ’» Code

// Cast to any to simulate incoming API value
const array: string[] | undefined | null = ['aa'] as any;

const isValid = Boolean(array && array.length > 0);

// Error on `array.map` for possible null or undefined, while we already verified the existence & length before
if (isValid) array.map(e => console.log(isValid)); 

πŸ™ Actual behavior

Gives an error for undefined/null, while we already verified the existence & length before

πŸ™‚ Expected behavior

Pass typings check as we already established it's existance

Additional information about the issue

Honestly don't know how to describe such check for better tags

jcalz commented 1 week ago

Duplicate #16655, relevant comment at https://github.com/microsoft/TypeScript/issues/50387#issuecomment-2037968462

Netail commented 1 week ago

Duplicate