我的代碼一般是這樣的: func BulkInsert(docs interface{}) {
switch data := docs.(type) {
case map[string] *model.SnapshotByConv, map[string] *model.UserSnapshotsMap:
for ver, _ := range data {
我正在循環訪問數組中的數據,並希望將我的循環項目投射到擴展接口(它有一個額外的標籤字段)。我可以重鑄什麼?到「PersonLabel」? for (const person of people) {
person.label = `${person.namespace}:${person.name}`;
this.peopleList.push(person);
}
我試
只要我知道,在Typescript中有兩種類型的語法。 只需使用<和>投 const a = <A>b;
使用as語句投 const a = b as A;
我猜產生的1碼或2完全一樣。 而且,當我開始使用Typescript時,沒有這樣的語法來使用as聲明。我猜as是打字稿中的新語法。 但是,應該有一些理由來使新的語法。爲什麼他們需要這些語法?如果只有1個演員的語法,是否有不方便的地方?
我試圖通過將類型傳入函數來實現類型斷言。換句話說,我想實現這樣的事情: // Note that this is pseudocode, because Type isn't the valid thing to use here
func myfunction(mystring string, mytype Type) {
...
someInterface := tra
我有一些服務器代碼向端點發送請求並接收存儲在類型爲空接口的對象中的JSON響應。我必須解析出這些信息並將其存儲在一個「Resource」對象中,Resource是一個接口。本例中的JSON數據表示一個「Position」對象,它滿足Resource接口。所以基本上,這些代碼是這樣的: // Resource interface type
type Resource interface {