You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Suggestion: Syntax for constructor signature #4775
which in my opinion is much more expressive. Notice that if we used typeof SomeClass compiler would accept it. So why not also accept typeof T?
Here's an example:
declareclassEventAggregator{subscribe<T>(event: typeofT,callback: (message: T)=>void): void;// subscribe<T>(event: { new(...any): T; }, callback: (message: T) => void): void;publish<T>(event: T): void;}// class with zero argument constructorclassSomeMessage{constructor(){}data: string;}// class with argument constructorclassSomeMessage2{constructor(publicdata: string){}}varea=newEventAggregator();// CASE 1: okay - no errorea.subscribe(SomeMessage,message=>{console.log(message.data);});// CASE 2: okay - no errorea.subscribe(SomeMessage2,message=>{console.log(message.data);});// CASE 3: errors out - good, that is expected. I want the method to// take only classes (types) and not instances of classesea.subscribe(newSomeMessage(),message=>{console.log(message.data);});ea.publish(newSomeMessage());
Workarounds that don't work really
Using generic type of T extends Function or Function instead of T breaks type inference of T
Then that would seem "auto-magic" to me and surprising... "if you have this one signature you can use this one keyword in these scenarios, but if you have anything else..."
The typeof operator in a type position is used to obtain the type of a variable. I think it would be really odd for it to also be notation for a constructor function type. Plus it would be a breaking change:
varx: number;functionfoo<x>(b: typeofx){}
I would suggest defining a generic type that represents a constructor:
Kitson Kelly (@kitsonk) that was the point of my suggestion, to define type of constructor with arbitrary arguments.
Anders Hejlsberg (@ahejlsberg) the Constructor interface you suggested is what I needed. Thank you! I noticed I had suggested this before in issue #204 but didn't come up with a solution I liked.
However, I wonder what typeof ClassX means below? Isn't it denoting the constructor type of ClassX? If the compiler is accepting typeof ClassX then why it's not complaining on foo(ClassY)?
However, I wonder what typeof ClassX means below? Isn't it denoting the constructor type of ClassX? If the compiler is accepting typeof ClassX then why it's not complaining on foo(ClassY)?
You have no members in ClassX and ClassY so they are structurally equivalent. Add a member to each and you'll get an error:
An easy rule of thumb is: When typeof is used in a type position, the argument must be a valid expression and the operator obtains the type of that expression.
Activity
[-]Suggestion: Syntax sugar for constructor signature[/-][+]Suggestion: Syntax for constructor signature[/+]kitsonk commentedon Sep 14, 2015
What about constructors that have a defined argument signature?
omidkrad commentedon Sep 14, 2015
Kitson Kelly (@kitsonk) in that case you can use exact constructor signature like
{ new(a: string, b: string): T }.kitsonk commentedon Sep 14, 2015
Then that would seem "auto-magic" to me and surprising... "if you have this one signature you can use this one keyword in these scenarios, but if you have anything else..."
ahejlsberg commentedon Sep 14, 2015
The
typeofoperator in a type position is used to obtain the type of a variable. I think it would be really odd for it to also be notation for a constructor function type. Plus it would be a breaking change:I would suggest defining a generic type that represents a constructor:
omidkrad commentedon Sep 14, 2015
Kitson Kelly (@kitsonk) that was the point of my suggestion, to define type of constructor with arbitrary arguments.
Anders Hejlsberg (@ahejlsberg) the
Constructorinterface you suggested is what I needed. Thank you! I noticed I had suggested this before in issue #204 but didn't come up with a solution I liked.However, I wonder what
typeof ClassXmeans below? Isn't it denoting the constructor type ofClassX? If the compiler is acceptingtypeof ClassXthen why it's not complaining onfoo(ClassY)?ahejlsberg commentedon Sep 14, 2015
You have no members in
ClassXandClassYso they are structurally equivalent. Add a member to each and you'll get an error:omidkrad commentedon Sep 14, 2015
OK, that makes sense. Now what makes
typeof ClassXvalid below but nottypeof T?ahejlsberg commentedon Sep 14, 2015
typeof ClassXis valid becauseClassXdenotes a named value (i.e. the constructor function). It also denotes a type, but that's irrelevant here.typeof Tis an error becauseTdoesn't denote a named value.ahejlsberg commentedon Sep 14, 2015
An easy rule of thumb is: When
typeofis used in a type position, the argument must be a valid expression and the operator obtains the type of that expression.