Skip to content

[TS2.5 regression] generic type argument derivation with ": this" types #21117

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

Closed
mprobst opened this issue Jan 10, 2018 · 0 comments
Closed
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@mprobst
Copy link
Contributor

mprobst commented Jan 10, 2018

TypeScript Version: Version 2.7.0-dev.20180110

Code

interface MsgConstructor<T extends Message> {
  new(data: Array<{}>): T;
}
class Message {
  clone(): this {
    return this;
  }
}
interface MessageList<T extends Message> extends Message {
  methodOnMessageList(): T[];
}

function fetchMsg<V extends Message>(protoCtor: MsgConstructor<V>): V {
  return null!;
}

class DataProvider<T extends Message, U extends MessageList<T>> {
  constructor(
      private readonly message: MsgConstructor<T>,
      private readonly messageList: MsgConstructor<U>,
  ) {}

  fetch() {
    const messageList = fetchMsg(this.messageList);
    messageList.methodOnMessageList();
  }
}

Expected behavior:

Compiles, because V is derived to be a subtype of MessageList. This is what it did in TS 2.5.

Actual behavior:

With TS2.6, derivation works differently:

repro.ts(25,17): error TS2339: Property 'methodOnMessageList' does not exist on type 'Message'.

If you explicitly pass fetchMsg<U>(...), you can see why:

repro.ts(24,34): error TS2344: Type 'U' does not satisfy the constraint 'Message'.
  Type 'MessageList<T>' is not assignable to type 'Message'.
    Types of property 'clone' are incompatible.
      Type '() => MessageList<T>' is not assignable to type '() => U'.
        Type 'MessageList<T>' is not assignable to type 'U'.

This seems wrong - MessageList inherits from Message, so any MessageList should satisfy the constraint Message.

@mhegazy mhegazy added the Bug A bug in TypeScript label Jan 10, 2018
@mhegazy mhegazy added this to the TypeScript 2.7.1 milestone Jan 10, 2018
@mhegazy mhegazy added the Fixed A PR has been merged for this issue label Jan 19, 2018
@mhegazy mhegazy modified the milestones: TypeScript 2.8, TypeScript 2.9 Mar 9, 2018
@microsoft microsoft locked and limited conversation to collaborators Jul 31, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

3 participants