2 回答

TA貢獻1815條經驗 獲得超10個贊
您可以使用構造函數簽名方法,但您需要添加您需要訪問的任何其他成員。
import {Person} from './Person';
let PersonConstructor: {
new(): Person
getDatabaseId(): string
} = Person;
// create a new instance but also access the static variables
const p: Person = new PersonConstructor();
PersonConstructor.getDatabaseId(); // "property does not exist"
或者,typeof Person如果您想使用與Person(構造函數簽名和所有靜態)完全相同的類型,則可以使用:
import {Person} from './Person';
let PersonConstructor: typeof Person = Person;
// create a new instance but also access the static variables
const p: Person = new PersonConstructor();
PersonConstructor.getDatabaseId(); // "property does not exist"

TA貢獻1860條經驗 獲得超9個贊
我不確定您的代碼的最終目標是什么。從TypeScript doc 開始,您嘗試實現的一種可能的語法用法是修改static類的成員并使用新的靜態成員創建新實例。
如果你打算這樣做,正確的代碼看起來像
let PersonConstructor: typeof Person = Person;
// create a new instance but also access the static variables
const p: Person = new PersonConstructor();
PersonConstructor.getDatabaseId(); // Also OK and you my change it. Original Person will not get it changed.
添加回答
舉報