如何在stypescript中指定变量可以是Car
或Bar
类型?
class Car {
wheels: number;
}
class Bar {
drinks: number;
}
let foo: Car | Bar;
// now foo has to have the fields of Car AND Bar.
// I would like foo to be of type Car, OR Bar
// foo = { wheels: 4} should not raise an error
// foo = { drinks: 4} should not raise an error
答案 0 :(得分:0)
您必须将您的用法包装在Sub SavePicToFile(namefile)
Selection.CopyPicture xlScreen, xlBitmap
Application.DisplayAlerts = False
Set tmp = Charts.Add
On Error Resume Next
With tmp
.SeriesCollection(1).Delete
.Width = Selection.Width
.Height = Selection.Height
.Paste
.Export filename:=namefile, Filtername:="jpeg"
.Delete
End With
End Sub
foto = Application.ActiveWorkbook.Path & "\Foto" & ".jpeg"
ActiveWorkbook.Sheets(1).Range("A1:Z30").Select
SavePicToFile (foto)
:
instanceof
打字稿需要知道,否则它不会阻止您尝试访问class Car {
wheels: number;
}
class Bar {
drinks: number;
}
let foo: Car | Bar;
if (foo instanceof Car) {
foo.wheels;
}
if (foo instanceof Bar) {
foo.drinks;
}
对象上的wheels
,这会使它变得不那么有用。
根据您的示例,分配:Bar
似乎没有在typescript playground中抱怨。