本文介绍了TypeScript或JavaScript类型转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在TypeScript或Javascript中处理类型转换?
说我有以下TypeScript代码:
export class SymbolFactory {
createStyle(symbolInfo:SymbolInfo):any {
if(symbolInfo == null)
{
return空值;
}
if(symbolInfo.symbolShapeType ===marker){
//如何强制转换为MarkerSymbolInfo
返回this.createMarkerStyle (MarkerSymbolInfo)symbolInfo);
}
}
createMarkerStyle(markerSymbol:MarkerSymbolInfo):any {
throwcreateMarkerStyle not implemented;
}
}
}
code> SymbolInfo 是一个基类。如何在TypeScript或Javascript中处理从 SymbolInfo
到 MarkerSymbolInfo
的类型转换?
解决方案
您可以这样投:
return this.createMarkerStyle < MarkerSymbolInfo> symbolInfo);
请记住,这是一个编译时强制转换,
How does one handle type casting in TypeScript or Javascript?
Say I have the following TypeScript code:
module Symbology {
export class SymbolFactory {
createStyle( symbolInfo : SymbolInfo) : any {
if (symbolInfo == null)
{
return null;
}
if (symbolInfo.symbolShapeType === "marker") {
// how to cast to MarkerSymbolInfo
return this.createMarkerStyle((MarkerSymbolInfo) symbolInfo);
}
}
createMarkerStyle(markerSymbol : MarkerSymbolInfo ): any {
throw "createMarkerStyle not implemented";
}
}
}
where SymbolInfo
is a base class. How do I handle typecasting from SymbolInfo
to MarkerSymbolInfo
in TypeScript or Javascript?
解决方案
You can cast like this:
return this.createMarkerStyle(<MarkerSymbolInfo> symbolInfo);
Just remember that this is a compile-time cast, and not a runtime cast.
这篇关于TypeScript或JavaScript类型转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!