我非常新的打字稿,當我從它的官方網站閱讀打字稿手冊,有一個例子:
Difference between static/instance side of class When working with classes and interfaces, it helps to keep in mind that a class has two types: the type of the static side and the type of the instance side. You may notice that if you create an interface with a construct signature and try to create a class that implements this interface you get an error:
interface ClockInterface {
new (hour: number, minute: number);
}
class Clock implements ClockInterface {
currentTime: Date;
constructor(h: number, m: number) { }
}
This is because when a class implements an interface, only the instance side of the class is checked. Since the constructor sits in the static side, it is not included in this check.
Instead, you would need to work with the 'static' side of the class directly. In this example, we work with the class directly:
interface ClockStatic {
new (hour: number, minute: number);
}
class Clock {
currentTime: Date;
constructor(h: number, m: number) { }
}
var cs: ClockStatic = Clock;
var newClock = new cs(7, 30);
我只是不能讓自己明白爲什麼最後一部分工作:
var cs: ClockStatic = Clock;
var newClock = new cs(7, 30);
我認爲cs是一個變量,它將引用ClockStatic類型的對象,但爲什麼它可以訪問類名?我認爲Clock類型與ClockStatic無關,爲什麼它可以提供給cs?
感謝
謝謝,雖然這可能需要一些時間讓我理解,但我想它改變了我的一些概念誤解。但是現在我不太明白的是「類Clock」,如果聲明一個類就像定義一個帶有類體的變量作爲該變量類型的結構,那麼「Clock」只是一個沒有任何實際對象引用的變量名,它怎麼能被賦予另一個變量?就像在JS中:var a; var b; b = a;這讓我很難理解 – Kuan