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 on obj.prop doesn't propagate to obj #57290

Closed
kevinjwz opened this issue Feb 4, 2024 · 3 comments
Closed

Narrowing on obj.prop doesn't propagate to obj #57290

kevinjwz opened this issue Feb 4, 2024 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@kevinjwz
Copy link

kevinjwz commented Feb 4, 2024

πŸ”Ž Search Terms

narrowing intersection property type guard assertion

πŸ•— Version & Regression Information

occurs on:
5.3.3
5.4.0-dev.20240204

⏯ Playground Link

https://www.typescriptlang.org/play?ts=5.4.0-dev.20240204#code/JYOwLgpgTgZghgYwgAgILIN4Chm+QDwH4AuZEAVwFsAjaAbiwF8tRJZEUAhZCfSEACYBnNJhx58pCjXpMsWGORAIwwAPYhkMABRxSqAJRi8yYDGTawATwAOENebgA6fMgC8H5ACJptKF4NsExNyIQgAOSo-XRcDOmQAegTkNQBrcWDkUIhOXTjE5OgoNSgM5GZmBSUVdU1s3OpSTiNsOSrlVQ0ssMiZKG1JMijoQPlKpNNwaDCaroA3adqsaztkAGF3YwkSIb6GZhWUABFNjYAyLdxB31lKxQ7arQBmbQRSNZayswtDh2QEFzuTw+Yb+UaZXDZXrRAH4fITNJlEJhI6veGFKDFUomCrye6zOoo7QCUhHT5yIA

πŸ’» Code

interface A {
    x?: number;
}
interface B extends A {
    x: number;
}

function f(a: A) {
    if (typeof a.x === "number"){
        useNumber(a.x); // ok
        useB(a); // error
    }
}

function useB(b: B) {}

function useNumber(x: number) {}

πŸ™ Actual behavior

Cannot assign a to parameter of type B. It seems type narrowing works independently on a.x and a.

πŸ™‚ Expected behavior

a should be assignable to parameter of type B.

Additional information about the issue

No response

@MartinJohns
Copy link
Contributor

Duplicate of #42384 and many many others.

@fatcerberus
Copy link

oops @MartinJohns beat me to it this time

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Feb 5, 2024
@typescript-bot
Copy link
Collaborator

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

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Feb 8, 2024
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

5 participants