"Pick" or "Exclude" constructor from class type
See original GitHub issueSearch 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.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:10
- Comments:9 (1 by maintainers)
Top Results From Across the Web
How to create a type excluding instance methods from a class ...
Show activity on this post. Given a class, containing both properties and methods, I'd like to derive a type that just contains its...
Read more >Documentation - Utility Types - TypeScript
Omit<Type, Keys> Constructs a type by picking all properties from Type and then removing Keys (string literal or union of string literals).
Read more >Using 'Pick' in TypeScript to simplify Types and Interfaces
In this post you'll learn how to use Pick in TypeScript to help clean up your codebase and make your type management far...
Read more >Overriding Inheritance in New Types Using Omit and Keyof in ...
Deno the new JavaScript and TypeScript runtime has first class support ... It means, Pick the properties of generic type T and exclude...
Read more >typescript-cheatsheet - GitHub Pages
The cheatsheet contains references to types, classes, decorators, and many other ... As a final note, we can also omit (by either picking...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Ahhh so you want to be able to pull the signatures out of a type.
bump - need this.
I’d use the
never
type, but the auto-completions still show up for the property, and I don’t want that.Also, once I want to create a new class & the parameter I want gone is not provided, typescript errors, because the parameter is required, even though it’s
never
…I’d also like to be able to do something like:
to then explicitly create a unique id for the unique lesson.
BUT I still have the
nonUniqueId
auto-completion shenaningans etc.