镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Unexpected intersection type constraint error related to union, generic type and private field #38236

Description

@vilicvane

TypeScript Version: 3.9.1-rc/4.0.0-dev.20200428

3.9.0-beta seems to be fine.

Search Terms: intersection,private,generic,constraint,union

Code

type Type = {foo: string} | {bar: number};

class SyncableObject {
  private foo: unknown;
}

interface SyncableRef<T extends ISyncableObject> {
}

interface ISyncableObject<T = object> extends SyncableObject {
}

type __ValueDescriptorType<
  T extends Type | undefined
> = T extends ISyncableObject
  ? SyncableRef<T>
  : T;

Expected behavior: No error

Actual behavior:

Type 'ISyncableObject<object> & T' does not satisfy the constraint 'ISyncableObject<object>'.
  Property 'foo' is missing in type 'ISyncableObject<object> & T' but required in type 'ISyncableObject<object>'.(2344)

Playground Link: Playground

Related Issues:

Activity

  1. DanielRosenwasser commented on Apr 29, 2020

    @DanielRosenwasser
    Member

    I'm guessing related to #37762.

  2. changed the title [-]Unexpected intersection type constraint error related to primitive union, generic type and private field[/-] [+]Unexpected intersection type constraint error related to union, generic type and private field[/+] on Apr 30, 2020
  3. ahejlsberg commented on Apr 30, 2020

    @ahejlsberg
    Member

    Actually, looks to be caused by #37537. We currently say that unions of object types never contain private or protected members. But that isn't quite right. A union of object types should contain any private or protected member that is present in every constituent and originates in exactly the same declaration. This rarely matters, but in this particular case we end up structurally relating the union ISyncableObject & object | ISyncableObject & string to ISyncableObject, and that fails (but shouldn't) because of the private member.

  4. locked as resolved and limited conversation to collaborators on Oct 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions