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

Type guard fails to make an array of full objects assignable to an array of partial objects #60167

Closed
KSXGitHub opened this issue Oct 8, 2024 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@KSXGitHub
Copy link
Contributor

πŸ”Ž Search Terms

array of full objects assignable to an array of partial objects.

πŸ•— Version & Regression Information

(not a regression)

⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.6.2#code/JYOwLgpgTgZghgYwgAgJIhNA8gIwFYQJjIDeAUMpcjAPY0BcyIArgLY7QDaAuhVTnCiMAzmCigA5jzIBfMmVCRYiFFmZLcBIqT6VQmKJsJhhAfkboDRotLkLw0eEmQBlZjjUb8xnVWT7sbyJhRgBBKCg4AE8AHgAFYAQAaxjLQK0wABpkAHJaGhyAPkLZeRhmECJgGhBkMDgkiDcPdWgACgB9YXdPdONGZt7DILAASkYANxpgABNSOwgADwAHGihicsqwatrluGFhABFgKGMAGyi2mlbhjMYh6zHJ6bnyP2AYZCubx4A6ANuxmEo1IyAA9GDkPthNBiGAABZwYgAx7CfxokA0YgVGYQGCgCAzXR+OoNJo9G7fLwZEEQ5DQKBrRgAFSiyxQOTSgJs3GQAB9kDi8QSZjl0UwsVCDsAJCA4DgzigwDQ6myOQlkqkMH0iNkAET5PWFHg5YlyBYrNYbCpVGrIU4IU5IiDM+EQEBxaFUnVge4-EbjZBTWa+KgfL7XanGf7a7kmEEkcGQ6GwuqI5Gx1HizHYkC4-GYIkkqj1RqDSmJlEjELISM+mNWavIGS0yEI4BovYHCDCM2lIA

πŸ’» Code

interface InnerObject {
    foo: number[]
    bar: string[]
}

interface OuterObject {
    innerObjects?: InnerObject[]
}

interface SubOuterObject {
    innerObjects: Array<Pick<InnerObject, 'foo'>>
}

function takeSubOuter(_subOuterObject: SubOuterObject): void {}

export function passDirectly(outerObject: OuterObject): void {
    if (outerObject.innerObjects) { // assert that innerObjects is not undefined
        takeSubOuter(outerObject) // error: Type 'InnerObject[] | undefined' is not assignable to type 'Pick<InnerObject, "foo">[]'
    }
}

export function recreateThenPass(outerObject: OuterObject): void {
    if (outerObject.innerObjects) { // assert that innerObjects is not undefined
        takeSubOuter({ innerObjects: outerObject.innerObjects }) // this passes
    }
}

πŸ™ Actual behavior

In passDirectly, theif type guard fails to make outerObject compatible with SubOuterObject, even though it should have proven that outerObject.innerObjects is not undefined.

πŸ™‚ Expected behavior

There should be no compiler errors.

Additional information about the issue

No response

@MartinJohns
Copy link
Contributor

Duplicate of #42384.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Oct 8, 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 Oct 11, 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

4 participants