TypeScript interface to describe class
Asked Answered
A

3

1

Is there an interface in TypeScript to describe a class declaration?

function module(name: string, classDeclaration: IClass) {
     this.classHash[name] = classDeclaration; //example use of class declaration
}
Aruspex answered 18/12, 2015 at 8:29 Comment(0)
M
0

No. I would have expected Function (or FunctionConstructor) to be such an interface, but, no.

What is troubling is that, as expected, in the code below the type returned by typeof is function ... but function (lower case f) is neither a type nor an interface in Typescript. So the answer has to be no.

"use strict";

class Hello {

    private s: string

    constructor(s: string) {
        this.s= s
    }

    public sayHello() {
        console.log(`Hello ${this.s}`)
    }    
}


function instantiate<T>(clazz: any, name: string): T {
    console.log(`Type of clazz: ${typeof clazz}`)
    return new clazz(name) as any as T
}


instantiate<Hello>(Hello, 'John').sayHello()
Modulus answered 18/12, 2015 at 11:38 Comment(1)
After more digging I have to agree with you, that there is no such interface for Classes in TypeScript.Valero
I
1

In JavaScript, there are only functions. The class keyword in TypeScript & EcmaScript 6 is (for the purposes of this question) sugar for creating a constructor function and filling out its prototype. Therefore, a generic interface for a “class” in TypeScript is the same as an interface for any constructor function:

interface Class<T> {
  new (...args: any[]): T;
  prototype: T;
}
Isaiasisak answered 18/12, 2015 at 20:49 Comment(0)
B
0

Don't name your function module, it is a keyword in TypeScript. AFAIK there is no such interface or class but you can create your own interface IClass which I would strongly recommend.

An example is:

interface IClass {}

class MyClass implements IClass {}
class MySecondClass implements IClass {}

function myModule(name: string, classDeclaration: IClass) {
     this.classHash[name] = classDeclaration; //example use of class declaration
}

myModule("1", MyClass);
myModule("2", new MyClass());
myModule("3", MySecondClass);
Beckiebeckley answered 18/12, 2015 at 9:8 Comment(0)
M
0

No. I would have expected Function (or FunctionConstructor) to be such an interface, but, no.

What is troubling is that, as expected, in the code below the type returned by typeof is function ... but function (lower case f) is neither a type nor an interface in Typescript. So the answer has to be no.

"use strict";

class Hello {

    private s: string

    constructor(s: string) {
        this.s= s
    }

    public sayHello() {
        console.log(`Hello ${this.s}`)
    }    
}


function instantiate<T>(clazz: any, name: string): T {
    console.log(`Type of clazz: ${typeof clazz}`)
    return new clazz(name) as any as T
}


instantiate<Hello>(Hello, 'John').sayHello()
Modulus answered 18/12, 2015 at 11:38 Comment(1)
After more digging I have to agree with you, that there is no such interface for Classes in TypeScript.Valero

© 2022 - 2024 — McMap. All rights reserved.