Skip to content

Array access possibly undefined with noUncheckedIndexedAccess despite findIndexΒ #61388

Description

@Sainan

πŸ”Ž Search Terms

noUncheckedIndexedAccess, findIndex, undefined

πŸ•— Version & Regression Information

Tried 5.8.2 and nightly (5.9.0-dev.20250310).

⏯ Playground Link

https://www.typescriptlang.org/play/?noUncheckedIndexedAccess=true&ts=5.8.2#code/MYewdgzgLgBAhgJwQLhmArgWwEYFMEDaAujALwwECMANDAEy0DMRA3AFCiSwCWAJgB5l4SAHQAzbmF4BJKbn4AKQaQB8MZeToBKdtzEwFfQQEJyAWkpaYAbzYx7MTtHWoMOfEMSEjrOw6cgADa4IoEgAOZKOmwAvmxAA

πŸ’» Code

constarr: number[]=[1,2,3];constidx=arr.findIndex(x=>x==2);if(idx!=-1){constx: number=arr[idx];console.log(x);}

πŸ™ Actual behavior

Type 'number | undefined' is not assignable to type 'number'.
Type 'undefined' is not assignable to type 'number'.

πŸ™‚ Expected behavior

Code 'compiles' without errors.

Additional information about the issue

No response

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions