Skip to content

Array.prototype.find does not narrow return typeΒ #61238

Closed as not planned
Closed as not planned
@EvgenyMuryshkin

Description

@EvgenyMuryshkin

πŸ”Ž Search Terms

Array.prototype.find

Something similar might be here
#19456

πŸ•— Version & Regression Information

Currently migrating codebase strictNullChecks on 5.7.3. Potentially an old issue.

⏯ Playground Link

https://www.typescriptlang.org/play/?#code/GYVwdgxgLglg9mABBOBDANgUwM4UwCgDpiYpMBbbALkX2ygCcYwBzRAH0TBHXQ8XAATTMGaZBASgDaAXQk16TVvyEixgxAG8AUIj2IGmKCAZJSFbIVFhB+GIgC8APkT2AhA4dcefAGS-XRA8vVWtxRH9Aj0QAIhiJAG5tAF8gA

πŸ’» Code

function coalesce(...items: (string | null | undefined)[]): string | undefined {
    return items.find(i => i !== null && i !== undefined && i != "");
}

πŸ™ Actual behavior

Type 'string | null | undefined' is not assignable to type 'string | undefined'.
  Type 'null' is not assignable to type 'string | undefined'.(2322)

πŸ™‚ Expected behavior

null values are filtered out, so return type should exclude null

Additional information about the issue

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions