Skip to content

(Supposedly) non-empty array causes "Source provides no match for required element at position 0 in target.(2345)" #53698

@twiddler

Description

@twiddler

Bug Report

🔎 Search Terms

map source provides no match nonempty

🕗 Version & Regression Information

This happens in nightly, 5.0.3, 3.3.3 (similar error message) and many more versions available in the playground.

I reviewed the FAQ for entries about Common "Bugs" That Aren't Bugs.

⏯ Playground Link

Playground link with relevant code

💻 Code

type NonEmptyArray<T> = [T, ...T[]]

const a = [1, 2, 3] as const

function fn<T>(a: NonEmptyArray<T>) {
    return a
}

fn(a.map((v) => v)) // Argument of type '(1 | 2 | 3)[]' is not assignable to parameter of type 'NonEmptyArray<1 | 2 | 3>'. Source provides no match for required element at position 0 in target.(2345)

🙁 Actual behavior

TypeScript complains that a.map((v) => v)) is not a valid input for fn as it might be missing an element at position 0.

🙂 Expected behavior

I might be mistaken, but I understand that map guarantees an array of equal length, which in this case would guarantee an element at position 0.

Thanks for your help. 🙏

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions