我有一个非常简单的课程
structure(list(soc_sec = c("AA2105480", "AA2105480", "AB4378973",
"AB4990257", "AB7777777", "AB7777777", "AB7777777", "AC4285291",
"AC4285291", "AC6039874", "AC6039874", "AC6039874", "AC6039874",
"AC6547645", "AC6547645", "AC6547645", "AD1418577", "AD1418577",
"AD1418577", "AD1418577"), group_count = c(5L, 7L, 1L, 2L, 5L,
6L, 5L, 2L, 1L, 9L, 7L, 8L, 7L, 7L, 6L, 1L, 7L, 8L, 6L, 7L),
total_creds = c(14, 17, 0, 1, 12, 15, 15, 3, 3, 17.5, 16,
12.5, 13.5, 18, 17, 2, 13, 16, 15, 13), group_start = structure(c(12792,
12656, 12438, 16314, 12438, 12656, 12792, 16314, 16447, 14615,
14979, 14852, 15217, 12792, 12656, 12893, 15714, 15945, 16078,
16673), class = "Date"), group_end = structure(c(12919, 12762,
12545, 16418, 12864, 12762, 12915, 16418, 16540, 14735, 15093,
14964, 15329, 12915, 12762, 12935, 15842, 16052, 16195, 16784
), class = "Date")), class = c("tbl_df", "data.frame"), row.names = c(NA,
-20L), .Names = c("soc_sec", "group_count", "total_creds", "group_start",
"group_end"))
我从服务器获取此类的元素并将json转换为此类:
export class Foo {
name: string;
index: number;
toFullString(): string {
return `${this.name} | ${this.index}`;
}
}
现在发生的事情是,在我调用.map(response => response.json() as Foo)
方法后,它失败了,因为这不是"真正的" {C}的toFullString()
对象。
那么实现真正的Foo
对象的正确方法是什么,最好不需要编写自己的构造函数等等。
TypeScript中的类型安全有时真的是个笑话。
答案 0 :(得分:1)
您可以创建Object并为其指定JSON值。
.map(res => this.onResponse(res))
-
function onResponse(res:any){
this.foo = new Foo();
this.foo.name = res['name'];
...
}
或者将JSON值赋给Foo的构造函数
this.foo = new Foo(res['name'], ...);
答案 1 :(得分:1)
要么您必须编写自己的构造函数,要么必须使用返回的obejcts并使用# Get the License SkuId from a template user that we want to apply to the new user
$licensedUser = Get-AzureADUser -ObjectId "TemplateUser@contoso.com"
# Get the new User we want to apply the license too
$user = Get-AzureADUser -ObjectId "newuser@contoso.com"
# Create the new License object
$license = New-Object -TypeName Microsoft.Open.AzureAD.Model.AssignedLicense
$license.SkuId = $licensedUser.AssignedLicenses.SkuId
# Create the Licenses Table and add the license from above
$licenses = New-Object -TypeName Microsoft.Open.AzureAD.Model.AssignedLicenses
$licenses.AddLicenses = $license
# Apply the license to the new user
Set-AzureADUserLicense -ObjectId $user.ObjectId -AssignedLicenses $licenses
附加函数(技术上合法的JS)。一个你不想要的,另一个有点粗略。
您已经看过的构造函数方法,但我将在此重复:
for(let obj of responseObjects) { obj['toFullString'] = function ... }
你也可以在地图上做同样的事情。
constructor(public name, public index) {
this.name = name;
this.index = index;
}
自动映射仅适用于数据对象,这是一个限制,因为JS是它的基础语言。
这是一个示例将该函数附加到Plain Old JavaScript Object的plunker: http://plnkr.co/edit/BBOthl0rzjfEq3UjD68I?p=preview