2
在打字稿中定義自定義類型時我想說如果未定義內部元素,則將使用默認值。如何在打字稿類型中定義默認值
因此,例如:
function custom(a:{required:string, optional='haha'}) {
}
這不工作:(
我不能使用type
因爲這句法也不工作這將是很好的找到正確的語法使用。
在打字稿中定義自定義類型時我想說如果未定義內部元素,則將使用默認值。如何在打字稿類型中定義默認值
因此,例如:
function custom(a:{required:string, optional='haha'}) {
}
這不工作:(
我不能使用type
因爲這句法也不工作這將是很好的找到正確的語法使用。
打字稿支持這一點,我想代碼將最好的解釋:
//You can set defaults for variables
var defaultingString: string = "Default"
//You can create custom types through classes
var myCustomType: TestClass;
class TestClass {
//You can set defaults for variables in classes too
testVarA: string = "Default";
//You can handle it in a class constructor
//(which can also have defaults in it's parameters)
testVarB: string;
constructor(public someParam: string = "DefaultParam") {
if (!this.testVarB){
this.testVarB = someParam;
}
}
//Works in regular functions both for parameters and return type
testFoo (someOtherParam: string = this.testVarA): string {
return someOtherParam;
}
}
myCustomType不可用。另外hte問題在解構之內:( –