-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
"Pick" or "Exclude" constructor from class type #29261
Comments
Ahhh so you want to be able to pull the signatures out of a type. |
Well it could be a way to see it. But blindly extract a signature out of a function could be a nice feature to! |
Technically |
Yeah sure, that's why I don't talk about |
To be fair, you did say:
😉 But I know what you meant, no worries. |
Right now any type mapping completely destroys any call or construct signatures. This is probably related to TypeScript not quite having higher order function types. You can get at the constructor itself by using (also, it would be nice indeed if TypeScript would synthesize a |
Yeah, because Typescript prefer returning the Function object instead of the actual signature. They are basically the same in js but not in ts: let A = function(foo: string) {
this.bar = function(n: number) {
return 'baz';
}
}
let B: typeof A; // B type recognize as "(foo: string) => void"
let B2: InstanceType<typeof B>; // error
let C = class {
constructor(foo: string) {}
bar(n: number) {
return 'baz';
}
}
let D: typeof C; // D type is recognize as "typeof C"
let D2: InstanceType<typeof D>; // ok
let bInstance = new B('qux');
bInstance.bar(); // should have an error but bInstance is any because constructor of A and B is new()=>any be default
let dInstance = new D('quw');
dInstance.bar(); // error but it's ok If a class is really a form of type by itself, it should be tweakable. We should be able to use |
bump - need this. I'd use the 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 I'd also like to be able to do something like: export class UniqueLesson extends NonUniqueLesson implements Omit<NonUniqueLesson, "nonUniqueId"> {
uniqueId: string;
} to then explicitly create a unique id for the unique lesson. BUT I still have the |
For those that are looking for a solution to patch / replace a constructor in a class, this works for me. interface Cat {}
class Duck {
/* This should make a Duck instance when constructed */
public static quack: string = "QUACK!";
}
type NotDuck = (new() => Cat) & typeof Duck; // The order here matters
const nonDuck: NotDuck = <NotDuck>Duck;
const soundsLike = nonDuck.quack; // We can still see Duck's static
const whoami = new nonDuck(); // but TypeScript sees this instance as Cat If you want to add Cat to Duck you can also just interface Cat {}
class Duck {
/* This should make a Duck instance when constructed */
}
type NotDuck = (new() => Cat & Duck) & typeof Duck; // The order here matters
const nonDuck: NotDuck = <NotDuck>Duck;
const whoami = new nonDuck(); // TypeScript sees this as Cat & Duck |
Search Terms
intersect
,type
,intersect constructor
,pick new
,pick constructor
Suggestion
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:
Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: