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 narrowing on properties of object not respected when destructuring into new object #46910

Closed
tubocurarin opened this issue Nov 23, 2021 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@tubocurarin
Copy link

Bug Report

It appears, that when a type is narrowed, the narrowing is ignored when assigning to a technically compatible type.

I skimmed the issue tracker rather quickly, I hope I haven't missed an already known issue with a ticket.

🔎 Search Terms

  • narrow(ed)
  • destructuring / spread

🕗 Version & Regression Information

  • TypeScript v4.3.2 & v.4.5.2

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about 10 minutes

⏯ Playground Link

Playground link with relevant code

💻 Code

Happens when:

type X = {
    a: string
    b: string
    c: string | null
}

type Y = {
    a: string
    b: string
    c: string
}

const x: X = {
    a: 'a',
    b: 'b',
    c: 'c',
}

if (x.c !== null) {
    const y: Y = {
        ...x,  // ERROR
    }
}
error TS2322: Type '{ a: string; b: string; c: string | null; }' is not assignable to type 'Y'.
  Types of property 'c' are incompatible.
    Type 'string | null' is not assignable to type 'string'.
      Type 'null' is not assignable to type 'string'.

20     const y: Y = {
             ~

When explicitly passing the field again, it works:

type X = {
    a: string
    b: string
    c: string | null
}

type Y = {
    a: string
    b: string
    c: string
}

const x: X = {
    a: 'a',
    b: 'b',
    c: 'c',
}

if (x.c !== null) {
    const y: Y = {
        ...x,
        c: x.c,  // WORKS!
    }
}

🙁 Actual behavior

See above, error is thrown although the "problematic" property was narrowed down to be compatible with the target type in the assignment.

🙂 Expected behavior

Should not throw a type error and should not require the workaround in the above code example with the explicit assignment from the narrowed-down field: c: x.c

@fatcerberus
Copy link

Narrowing individual properties of an object won’t narrow the containing type except in certain situations, because it’s generally difficult (from a technical standpoint) to do so; if this were possible in general we wouldn’t need to have discriminated unions.

duplicate of #30506.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Nov 24, 2021
@RyanCavanaugh
Copy link
Member

See #26645

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

3 participants