如何在TypeScript或Javascript中处理类型转换?
假设我有以下TypeScript代码:
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";
}
}
}
Run Code Online (Sandbox Code Playgroud)
哪里SymbolInfo是基类.如何从处理类型转换SymbolInfo到MarkerSymbolInfo以打字稿或Javascript?
我是 Reactive Forms 的新手,我习惯于使用模板驱动的表单。
我正在关注这里的教程:https : //angular-templates.io/tutorials/about/angular-forms-and-validations
我有一个用户类:
export class User {
public pseudo: string;
public email: string;
public password: string;
public constructor(init?: User) {
Object.assign(this, init);
}
}
Run Code Online (Sandbox Code Playgroud)
我在一个组件中得到了我的 FormGroups:
this.matching_passwords_group = new FormGroup(
{
password: new FormControl(
'',
Validators.compose([
Validators.minLength(5),
Validators.required,
Validators.pattern(
'^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])[a-zA-Z0-9]+$'
)
])
),
confirm_password: new FormControl('')
},
(formGroup: FormGroup) => {
return PasswordValidator.areEqual(formGroup);
}
);
// user details form validations
this.userDetailsForm = this.fb.group({
pseudo: new FormControl('', Validators.required),
email: new FormControl(
'',
Validators.compose([
Validators.required,
Validators.pattern('^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+.[a-zA-Z0-9-.]+$') …Run Code Online (Sandbox Code Playgroud)