[英]Aurelia ValidationRules.ensure using TypeScript
I had an Aurelia project that was created with ES6 and Babel and I'm trying to convert it over to TypeScript. 我有一个用ES6和Babel创建的Aurelia项目,我正在尝试将其转换为TypeScript。
In one area of code, I had something like this: 在代码的一个方面,我有这样的事情:
tenant.js tenant.js
import {ValidationRules} from 'aurelia-validation'
export class Tenant{
constructor(tenantObject){
Object.assign(this, tenantObject);
}
}
ValidationRules
.ensure(o => o.name).displayName("Tenant Name").required()
...
.on(Tenant);
system.js (VM) system.js(VM)
import {ValidationController} from 'aurelia-validation'
import {NewInstance} from 'aurelia-framework'
import {Tenant} from '../models/tenant'
import {BootstrapValidationRenderer} from 'elements/bootstrap-validation-renderer'
export class System{
static inject = [DataService, NewInstance.of(ValidationController)]
constructor(dataService, validationController){
this.dataService = dataService
this.validationController = validationController
this.validationController.addRenderer(new BootstrapValidationRenderer())
}
async activate() {
let data = await this.dataService.getTenant(1);
this.tenant = new Tenant(data);
}
...
}
system.html system.html
...
<div class="form-group name" >
<label for="pName" class="form-control-sm">Tenant Name</label>
<input id="pName" type="text" autofocus class="form-control form-control-sm" value.bind="tenant.name & validate" />
</div>
However, I now get an error in tenant.ts
stating that property 'name' does not exist on type {}
. 但是,我现在在tenant.ts
中tenant.ts
错误,指出property 'name' does not exist on type {}
。
I tried adding a name
property to Tenant
but that did not seem to help. 我尝试向Tenant
添加name
属性,但这似乎无济于事。
Is there a way to get around this? 有办法解决这个问题吗?
It seems that aurelia-validation
does not have typings. 看来aurelia-validation
没有类型。 Hence you have to assert types like: 因此,您必须声明如下类型:
ValidationRules
.ensure((o: Tenant) => o.name).displayName("Tenant Name").required()
...
.on(Tenant);
But before that you should add name
property to Tenant
class: 但是在此之前,您应该将name
属性添加到Tenant
类中:
export class Tenant{
public name: string = ''
constructor(tenantObject){
Object.assign(this, tenantObject);
}
}
PS fwiw even if aurelia-validation
had typings, you would still have to assert type because it accepts class name in the very end of the methods chain. PS即使aurelia aurelia-validation
有类型,您仍然必须断言type,因为它在方法链的最后接受类名。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.