microsoft / microsoft/TypeScript

"Pick" or "Exclude" constructor from class type

Open
#29,261 9 comments 10 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Search Terms

intersect, type, intersect constructor, pick new, pick constructor

Suggestion

class C1 {
  static S1 = 'foo';
  bar = 'bar';
  constructor(p1, p2, p3) {}
}
type T1 = Pick<typeof C1, new>;
// could simply be also:
type Only<T, K extends keyof T> = Pick<T, { [P in keyof T]: P extends K ? P : never }[keyof T]>;
type T2 = Only<typeof C1, 'constructor'>;

const clazz: T1 = null; // `null` could be anything
new clazz(); // should throw error because of missing params
new clazz(null, null, null); // should work
new clazz(null, null, null).bar; // should throw error because `bar` doesn't exist
clazz.S1; // should throw error because `S1` doesn't exist

Use Cases

It could be useful in class type intersection and/or for multiple inheritance (like PHP Trait, or Java default methods in interface).
And make mixins more "clean" imho.
One of my goal is to make a "type mixin" without a "value mixin".

Examples

For example:

class C1 {
    public bar: string;
    constructor(p1: string) {}
    public foo() {}
}

class C2 {
    public constructor() {}
    public baz() {}
}

type T1 = typeof C1 & typeof C2;

const a: T1 = null;
a.prototype.foo(); // ok
a.prototype.bar; // ok
a.prototype.baz(); // ok
new a(); // ok (return C2)
new a(''); // ok (return C1)
new a().foo() // error
new a('').baz(); // error

type T2 = typeof C1 & Exclude<typeof C2, new>

const b: T2 = null;
b.prototype.foo(); // ok
b.prototype.bar; // ok
b.prototype.baz(); // ok
new a(); // error <<<
new a(''); // ok (return C1 with C2 prototype)
new a('').foo() // ok (from C1)
new a('').baz(); // ok (from C2) <<<

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

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 type aliases and constructor-intersection examples in the issue, then trace how TypeScript represents constructor and instance types. Define the intended behavior for selecting or excluding constructors, including the shown valid and invalid calls and property accesses. Done means the examples type-check or error as specified, with coverage for the proposed behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.