2017-05-08 73 views
1

在JavaScript我可以做以下動態參數類型,打字稿,類似的JavaScript變種

function testing(someObject){ 
    console.log(someObject.property1); 
    console.log(someObject.property2); 
    console.log(someObject.property3); 
} 

我想執行的打字稿爲SomeObject參數類似的東西。我不會被困擾宣佈一個班。我只是想有一個動態參數

我試過以下,但不工作。它不想接受任何類型的參數。有人可以請指教?也許我需要使用什麼樣的參數類型?謝謝。

export class testing{ 
    test(someObject: Any){ 

    } 
} 

回答

1

聲明是這樣的:

export class Testing{ 
    test(someObject: any){ 
     console.log(someObject); 
    } 
} 

你是對的,你只是錯過了小寫。

這是在角/打字稿使用非常普通的方法的一個示例:

private handleError(error: any) { 
    // In a real world app, we might use a remote logging infrastructure 
    // We'd also dig deeper into the error to get a better message 
    let errMsg = (error.message) ? error.message : 
     error.status ? `${error.status} - ${error.statusText}` : 'Server error'; 
    console.error(errMsg); // log to console instead 
    return Observable.throw(errMsg); 
} 

如果看到,error是然後獲取由該方法內的邏輯導航的參數。這個例子是在Http調用中處理錯誤的最常用的方法。