Skip to content

Covariance in intersectionΒ #62049

Open
Open
@f3oall

Description

@f3oall

πŸ”Ž Search Terms

covariance

πŸ•— Version & Regression Information

Checked on 5.8.3 and nightly, showing error.

⏯ Playground Link

https://www.typescriptlang.org/play/#code/KYDwDg9gTgLgBDAnmYcCCATDBFArsAZxgEsIA7AWQEMoBrAgHgBU5QZgyMC4IAjAK2ABjGAD44AXjgBvAFBw4AbQDScYmTi1giCADM4LKt1ydgu9cAysQ7TtyYqAunAD8cMsABuwKHABccMqOAQ5BsgC+cABkMvJKquqa2noGcEZwJhhmFlZsHFwGTq6B-u5ePo4uIU4RsnWgkLBwQgA2RtwAygAWVCgMELjwAPLWtgV8giLicgpZrTSoAPoDMGCDASO1sgD02+hYeIQk5NR03GBQEBi4QoTNEJ40xFRkMAA0cLyD9wC2YMQtHxwDAQQhkADk8BgXXUtDgBAgsga0Hg8wI3DQ-W+HVG+U6PRQ0ziczaUCWKzWMACmBw+CIpEoNHoDA6inBy0GlPBjlEESAA

πŸ’» Code

export type AddQuestionMarks<T extends object> = {
  [K in keyof T as undefined extends T[K] ? never : K]: T[K]
} & {
  [K in keyof T as undefined extends T[K] ? K : never]?: T[K]
}


export class Shape<out O extends object> {
  declare _output: O 
}

// AddQuestionMarks produces covariant, but compiler doesn't think so
export class A<out S extends Shape> {
  declare _output: AddQuestionMarks<S['_output']>
}

πŸ™ Actual behavior

I'm new to the whole variance thing, but it seems like compiler can't properly determine it in this case, AddQuestionMarks ruins covariance, despite it shouldn't.

If I'm wrong, any explanation would be great. If you have any suggestions on how to implement AddQuestionMarks in different way without ruining covariance, let me know as well.

πŸ™‚ Expected behavior

Result of AddQuestionMarks should be compatible with covariance or we should be able to explicitly set out on this type.

Additional information about the issue

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions