microsoft / microsoft/TypeScript

Mixin classes don't allow constructors of _generic_ 'object' types

Open
#16,390 5 comments 24 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

TypeScript Version: 2.3.4, 2.4.0-dev.20170609

Code:

type Constructor<T> = new (...args: any[]) => T;

const Timestamped = <I extends object, CT extends Constructor<I>>(Base: CT) => {
  return class extends Base {
    timestamp = new Date();
  };
};

Expected behavior:

Should be able to use the generic I as an argument to Constructor.

Actual behavior:

Compiling this yields the following error:

error TS2509: Base constructor return type 'I' is not a class or interface type.

Note:

This is a variation on https://github.com/Microsoft/TypeScript/issues/13805 where the following compiles successfully:

type Constructor<T> = new (...args: any[]) => T;

const Timestamped = <CT extends Constructor<object>>(Base: CT) => {
  return class extends Base {
    timestamp = new Date();
  };
};

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the TypeScript compiler's handling of generic constructor return types and reproduce the example from the issue using TypeScript 2.3.4 or the referenced development version. Done means the generic I can be used as the Constructor argument without TS2509, while the object variant and existing mixin behavior continue to compile.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.