Skip to content

Type containing intersection of object with optional property allows unsafe assignment  #22255

Closed
@kpdonn

Description

@kpdonn

TypeScript Version: 2.8.0-dev.20180228

Search Terms:

  • optional intersection
  • partial intersection
  • unsafe assignment
  • incompatible assignment
  • invalid assignment
  • incorrect assignment
  • unsound assignment

Code

// A *self-contained* demonstration of the problem follows...

declare const a:  {x?: string} & {y: any}
const example1: {x?: number, y: any} = a // Expected error but didn't get one
const example2: {x?: number} = a // Got a TS2322 error as expected if the y parameter is excluded

declare const b:  {x?: string, y: any} 
const example3: {x?: number, y: any} = b // Now get a TS2322 error as expected

type MappedIdentity<T> = { [K in keyof T]: T[K] }
declare const c: MappedIdentity<{x?: string} & {y: any}>
const example4: {x?: number, y: any} = c // TS2322 error as expected when going through identity mapping

Expected behavior:
Error for example1.

Actual behavior:
No error for example1. The other examples are working as expected I just added them for context.

Playground Link:
https://www.typescriptlang.org/play/#src=declare%20const%20a%3A%20%20%7Bx%3F%3A%20string%7D%20%26%20%7By%3A%20any%7D%0Aconst%20example1%3A%20%7Bx%3F%3A%20number%2C%20y%3A%20any%7D%20%3D%20a%20%2F%2F%20Expected%20error%20but%20didn't%20get%20one%0Aconst%20example2%3A%20%7Bx%3F%3A%20number%7D%20%3D%20a%20%2F%2F%20Got%20a%20TS2322%20error%20as%20expected%20if%20the%20y%20parameter%20is%20excluded%0A%0Adeclare%20const%20b%3A%20%20%7Bx%3F%3A%20string%2C%20y%3A%20any%7D%20%0Aconst%20example3%3A%20%7Bx%3F%3A%20number%2C%20y%3A%20any%7D%20%3D%20b%20%2F%2F%20Now%20get%20a%20TS2322%20error%20as%20expected%0A%0Atype%20MappedIdentity%3CT%3E%20%3D%20%7B%20%5BK%20in%20keyof%20T%5D%3A%20T%5BK%5D%20%7D%0Adeclare%20const%20c%3A%20MappedIdentity%3C%7Bx%3F%3A%20string%7D%20%26%20%7By%3A%20any%7D%3E%0Aconst%20example4%3A%20%7Bx%3F%3A%20number%2C%20y%3A%20any%7D%20%3D%20c%20%2F%2F%20TS2322%20error%20as%20expected%20when%20going%20through%20identity%20mapping

Related Issues:
There are obviously a lot of issues about unsafe assignments but after looking through all the search terms above and checking the issues that have titles that sounded related, the only one that really seemed similar was #19927 which I opened a few months ago.

This case seems different to me though (specifically because of example2 above, which is the most similar to #19927 but has different behavior) which is why I'm creating the separate issue. But they're both about unsafe assignments that are allowed when there are weird combinations of optional properties and intersections so maybe there is a single root cause behind them both.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions