Issue
Interface Type<T> is an interface which is extending global type Function. I found this in typescript documentation Function about global function.
What does the above code mean? I got this code from angular source code.
export const Type = Function;
export function isType(v: any): v is Type<any> {return typeof v === 'function';}
export interface Type<T> extends Function { new (...args: any[]): T; }
I went through Typescript documentation but not able to figure it out.
Solution
The definition
interface Type<T> extends Function { new (...args: any[]): T; }
means that a value of type Type<T> is a function which is also known to be a constructor for instances of type T. Since it extends the Function interface it is a Function, and the syntax { new (⋯): ⋯ } is a construct signature, so it's also a constructor.
That means if you have
declare const f: Type<Date>;
then f is a Function and has all Function's properties like length and bind:
f.apply; // okay
f.call; // okay
f.bind; // okay
f.length; // okay
but it is also a constructor for Date objects:
const d = new f();
// ^? const d: Date;
That being said, I don't know why it's defined this way. All constructor types automatically inherit from Function; you don't need to declare it. You can only use the new operator on functions anyway... classes are just special functions:
class Foo {};
console.log(typeof Foo) // "function"
That means the definition
interface Type<T> { new(...args: any[]): T; }
behaves the same as your version. Perhaps it's defined this way to make the relationship with Function explicit? Not sure. But that's out of scope for the question as asked.
Answered By - jcalz
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.