简体   繁体   中英

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.

In one area of code, I had something like this:

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)

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

...
<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 {} .

I tried adding a name property to Tenant but that did not seem to help.

Is there a way to get around this?

It seems that aurelia-validation does not have typings. 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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM