Skip to content
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

Narrowing for nested property in lost when parent object given as function parameter #52155

Closed
TruffeCendree opened this issue Jan 9, 2023 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@TruffeCendree
Copy link

TruffeCendree commented Jan 9, 2023

Bug Report

🔎 Search Terms

Narrowing nested property given as function argument

🕗 Version & Regression Information

It seems the bug was present for a long time (all 4.x versions). Last checked version is 5.0.0 nightly. I checked the FAQ without finding it as a known issue.

⏯ Playground Link

Playground link with relevant code

💻 Code

import * as React from 'react'

interface Widget {
    data: { type: 'kpi' } | { type: 'text' }
}

function DashboardWidgetComponent ({ widget }: { widget: Widget }) {
    if (widget.data.type === 'text') return null

    // OK: return <DashboardWidgetKpiComponent widget={{ ...widget, data: widget.data }} />
    return <DashboardWidgetKpiComponent widget={ widget } />
}

function DashboardWidgetKpiComponent ({ widget }: { widget: Widget & { data: { type: 'kpi' } } }) {
    return <div>...</div>
}

🙁 Actual behavior

When calling DashboardWidgetKpiComponent, widget type Widget is concidered in place of Widget & { data: { type: 'kpi' }.

But on other side, directly referencing widget.data works fine, but lose memory identity (that matters in react).

🙂 Expected behavior

The code should compile without error.

@fatcerberus
Copy link

Similar to #18758, but Widget is not itself a discriminated union so I think this is actually a duplicate of #42384.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Jan 13, 2023
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants